Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Prompts to build query to search Microsoft Identity Metaverse with the Lithnet MIIS Powershell Module. Associated blog post https://blog.darrenjrobinson.com/scripting-queries-for-lithnet-get-mvobject-searches-into-the-microsoft-identity-manager-metaverse/
# Lithnet MIIS Automation Module
#https://github.com/lithnet/miis-powershell
Import-Module LithnetMiisAutomation
# Choose Operator for Metaverse Query
Function ChooseOperator ($attribute){
$title = "Search Attribute Operator Selection"
$message = "Which operator do you want to use for $attribute ?"
# Build the choices menu
$choices = @()
$choices += "StartsWith"
$choices += "EndsWith"
$choices += "IsPresent"
$choices += "IsNotPresent"
$choices += "Equals"
$choices += "Contains"
$choices += "NotContains"
$options = [System.Management.Automation.Host.ChoiceDescription[]]$choices
[int[]]$DefaultChoice = @(4)
$result = $host.ui.PromptForChoice($title, $message, $options, $DefaultChoice )
return $operator = $choices[$result]
}
# GET the Schema from the MV
$schema = Get-MVSchema
# Retreive Object Classes
$objectClasses = @()
foreach($class in $schema.ObjectClasses.Keys){
# Output MV Attributes for the report
$mvobjclass = New-Object -TypeName PSObject
$mvobjclass | Add-Member -Type NoteProperty -Name MVObjectClass -Value $class
$objectClasses += $mvobjclass
}
# Get the Object Class
If($objectClasses.Count -gt 1){
$title = "Metaverse Object Type Selection"
$message = "Which objecttype are you looking for?"
# Build the choices menu
$choices = @()
For([int]$index = 0; $index -lt $objectClasses.Count; $index++){
$choices += New-Object System.Management.Automation.Host.ChoiceDescription $objectClasses[$index].MVObjectClass, ($objectClasses[$index]).Name
}
$options = [System.Management.Automation.Host.ChoiceDescription[]]$choices
[int[]]$DefaultChoice = @(5)
$result = $host.ui.PromptForChoice($title, $message, $options, $DefaultChoice)
$targetObjectClass = $objectClasses[$result]
}
# Object Class Chosen
$targetObjectClass.MVObjectClass
# Get Attributes from the Object Class selected above
$attributes = Get-MVSchema -ObjectType $targetObjectClass.MVObjectClass | Select Attributes
$objectClassesAttributes = @()
foreach($attr in $attributes.Attributes.Keys){
$mvobjattr = New-Object -TypeName PSObject
$mvobjattr | Add-Member -Type NoteProperty -Name MVObjectAttr -Value $attr
$objectClassesAttributes += $mvobjattr
}
# prompt for attributes to be used in query to find object
If($objectClassesAttributes.Count -gt 1){
$title = "Metaverse Attribute Query Selection"
$message = "Which attribute(s) do you want to query?"
# Build the choices menu
$choices = @()
For([int]$index = 0; $index -lt $objectClassesAttributes.Count; $index++){
$choices += New-Object System.Management.Automation.Host.ChoiceDescription $objectClassesAttributes[$index].MVObjectAttr, $objectClassesAttributes[$index].MVObjectAttr
}
$options = [System.Management.Automation.Host.ChoiceDescription[]]$choices
[int[]]$DefaultChoice = @(5)
$result = $host.ui.PromptForChoice($title, $message, $options, $DefaultChoice )
$targetObjectAttribute = $objectClassesAttributes[$result]
}
# Attribute(s) to search on
$targetObjectAttribute.MVObjectAttr
# Build the query based on attributes and operators
# Resultant Query
$queries = @()
$querytxt = @"
`r`n
$([char]0x24)queries = @() `r`n
"@
# Format the query. Also check if the search value contain spaces and deal with syntax
foreach ($attrchoice in $targetObjectAttribute.MVObjectAttr){
$operator = ChooseOperator($attrchoice)
[string]$input = Read-Host -Prompt "Enter value to search for in attribute <$attrchoice>"
$queries += New-MVQuery -Attribute $attrchoice -Operator $operator -Value "$input"
$querytxt += @"
$([char]0x24)queries += New-MVQuery -Attribute $attrchoice -Operator $operator -Value $input `r`n
"@
}
$querytxt += "Get-MVObject -ObjectType $($targetObjectClass.MVObjectClass) -Queries " +'$queries'
$querytxt += "`r`n `r`n"
# Execute the Query
$query = Get-MVObject -ObjectType $targetObjectClass.MVObjectClass -Queries $queries
If ($query){
clear
write-host "Your query was: "
write-host ($querytxt)
write-host "The query returned the following"
$query.Attributes
$query | Show-Object
}
else{
write-host "Your Query returned no results. Here is what you were trying to look for:"
write-host ($querytxt)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.