Skip to content

Instantly share code, notes, and snippets.

@darrenjrobinson
Created June 9, 2017 00:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darrenjrobinson/d8af54b4cd2225aca52b04d36cf37f22 to your computer and use it in GitHub Desktop.
Save darrenjrobinson/d8af54b4cd2225aca52b04d36cf37f22 to your computer and use it in GitHub Desktop.
PowerShell Azure Function Trigger App - FIM/MIM Userlist Lookup
$in = Get-Content $req -Raw | ConvertFrom-Json
if ($in.objectType){
$inputObjectType = $in.objectType
write-output "====ObjectType====" $in.objectType
}
else{
$inputObjectType = 'person'
write-output "====ObjectType====" $inputObjectType
}
# 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\YOURFUNCTIONAPPPATH\keys\MIMSync.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 = {
param($objectType)
# Import LithnetMIISAutomation for MIM Sync Server Config Exports
Import-Module lithnetmiisautomation;
# Query for active accounts in the Accounts Domain
$queries = @();
$queries += New-MVQuery -Attribute accountName -Operator IsPresent
$queries += New-MVQuery -Attribute domain -Operator equals 'YOURADDOMAINNAME'
$queries += New-MVQuery -Attribute userAccountControl -Operator equals 512
# Get the Userlist
$userslist = Get-MVObject -ObjectType $objectType -Queries $queries | select Attributes
If ($userslist){
$names =@()
foreach ($uname in $userslist){
$names += $uname.Attributes.accountName.Values.valuestring
}
# Return a list of names as JSON
$output = $names | ConvertTo-Json
$output
}
else{
# "Search Failed"
$output = "Connection to MIM failed, or search returned no results"
$output
}
}
# Connect to MIM Sync Server and get the object
$results = Invoke-Command $scriptblock -computer PUBLICDNSNAMEOFYOURMIMSYNCSERVER -useSSL -credential $credentials -SessionOption $options -argumentlist $inputObjectType
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