Instantly share code, notes, and snippets.

Embed
What would you like to do?
Azure Function to retrieve MA Stats from the MIM Sync Server
$in = Get-Content $req -Raw | ConvertFrom-Json
# Username for connection to MIM Service via Function Application Settings
$username = $env:MIMSyncCredUser
# Password for connection to MIM Service via Function Application Settings
$pw = $env:MIMSyncCredPassword
# Credentials password (encrypted)
$keypath = 'D:\home\site\wwwroot\MyFunctionApp\keys\MyKey.key'
$password = $pw | ConvertTo-SecureString -key (Get-Content $keypath)
# Created PS Creds
$credentials = New-Object System.Management.Automation.PSCredential $Username,$password
# Connect to the FIM Sync Server
# Will require an inbound rule for TCP 5786 (or your MIM Sync Firewall) in you Resource Group Network Security Group Config
$options = New-PsSessionOption –SkipCACheck -SkipCNCheck
# Setup scriptblock
$scriptblock = {
# StyleSheet for nice pretty output
$htmlcss = "<style>
h1, h2, th { text-align: center; }
table { margin: auto; font-family: Segoe UI; box-shadow: 10px 10px 5px #888; border: thin ridge grey; }
th { background: #0046c3; color: #fff; max-width: 400px; padding: 5px 10px; }
td { font-size: 11px; padding: 5px 20px; color: #000; }
tr { background: #b8d1f3; }
tr:nth-child(even) { background: #dae5f4; }
tr:nth-child(odd) { background: #b8d1f3; }
</style>"
# Import LithnetMIISAutomation for MIM Sync Server Config Exports
Import-Module lithnetmiisautomation;
# Query MAs
$MAs = Get-ManagementAgent
$intTotalConnectors = 0
$intTotalObjects = 0
$MAStats = @()
if ($MAs) {
foreach ($ma in $MAs) {
$objects = $ma.Statistics.Total
$connectors = $ma.Statistics.TotalConnectors
$lastrun = Get-LastRunDetails -MA $ma | select StartTime, EndTime
$lastrunEnd = $lastrun.EndTime
$intTotalConnectors += $connectors
$intTotalObjects += $objects
# Output Stats for the report
$maAttr = New-Object -TypeName PSObject
$maAttr | Add-Member -Type NoteProperty -Name "Management Agent" -Value $ma.Name
$maAttr | Add-Member -Type NoteProperty -Name "Total Objects" -Value $objects
$maAttr | Add-Member -Type NoteProperty -Name "Connectors" -Value $connectors
$maAttr | Add-Member -Type NoteProperty -Name "Last Run" -Value $lastrunEnd
$MAStats += $maAttr
}
# Totals
$maAttr = New-Object -TypeName PSObject
$maAttr | Add-Member -Type NoteProperty -Name "Management Agent" -Value "Total"
$maAttr | Add-Member -Type NoteProperty -Name "Total Objects" -Value $intTotalObjects
$maAttr | Add-Member -Type NoteProperty -Name "Connectors" -Value $intTotalConnectors
# Sort by MA Name
$MAStats = $MAStats | Sort-Object -Property "Management Agent"
# Then add the Total to the end
$MAStats += $maAttr
# Convert to HTML
$MAStatsSummary = $MAStats | ConvertTo-Html -Fragment
# Export HTML payload
$htmlreport = ConvertTo-HTML -Body "$htmlcss<h1>Microsoft Identity Manager Statistics Report</h1></br> $MAStatsSummary" -Title "MIM Stats Report"
$htmlreport
}
else
{
# write-Output "Connection Failed"
$htmlreport = ConvertTo-HTML -Body "$htmlcss<h1>MIM Stats Report</h1><h2><center/>Query</h2><b><center>MIM Status could not be obtained. Is the MIM Sync Server online?</center></b></br><center>" -Title "MIM Stats Report"
$htmlreport
}
}
# Connect to MIM Sync Server and get the object
$results = Invoke-Command $scriptblock -computer mymimsyncserver.australiaeast.cloudapp.azure.com -useSSL -credential $credentials -SessionOption $options
Out-File -Encoding Ascii -FilePath $res -inputObject $results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment