Invoke-FormatQuery.psm1
<#
.Synopsis Return PsCustumObject form a query in sccm .Description Run & Format a SCCM query to a powershell object usable whit all command & module (like Out-GridView) .Parameter Query_ID The ID of SCCM query .Parameter Property_List A list of property must be return by Invoke-FormatQuery .Parameter SiteCode The Site Code of SCCM Site .Parameter ProviderMachineName The ProviderMachineName of SCCM Site .Parameter limit_ID Id of collection to limit the query .Example Invoke-FormatQuery "CO100567" @("Name,Site,Version") "SITE" "PROVIDER" "CO100342" #> function Invoke-FormatQuery{ param( [Parameter(Mandatory=$true)] [String]$Query_ID, [Parameter(Mandatory=$true)] $Property_List, [Parameter(Mandatory=$true)] [String]$SiteCode, [Parameter(Mandatory=$true)] [String]$ProviderMachineName, [Parameter(Mandatory=$false)] [String]$limit_ID ) $initParams = @{} if($null -eq (Get-Module ConfigurationManager)) { Import-Module "$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1" @initParams } if($null -eq (Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue)) { New-PSDrive -Name $SiteCode -PSProvider CMSite -Root $ProviderMachineName @initParams } Set-Location "$($SiteCode):\" @initParams #get query data in variable & clear/set base variable if($limit_ID.Length -gt 2) { $data = Invoke-CMQuery -Id $Query_ID -LimitToCollectionId $limit_ID } else { $data = Invoke-CMQuery -Id $Query_ID } $table_line = "" #temporary line contains property/value $table = @() #main table $table_temp = "" #temporary variable contain pre-formated string for hash table convertion $tot = $data.Count #nombre total des ligne a traité foreach($line in $data) #read all line of query result { $table_line = "" #clear temp variable contain all property/value in on line $i++ #add 1 to display $i/$tot $per = [math]::Round(($i*100)/$tot,2) #calculer un pourcentage avec $i/$tot foreach($Property in $Property_List) #read all line of property list (variable past from function parameter) { Write-Progress -PercentComplete $per -Activity "Formating SCCM Query Data $per`%" -Status "Formating the line $i`/$tot at Property: $Property " #display progress bar and update information if($Property -like "*SystemOUName*") { $text1 = "$Property = {" #text for regex 1 parti $text2 = "`"};" #text for regex 2 parti } else { $text1 = "$Property = `"" #text for regex 1 parti $text2 = "`";" #text for regex 2 parti } $Regex = [Regex]::new("(?<=$text1`)(.*)(?=$text2`)") #regex tool for select 1 string between 2 other $Match = $Regex.Match($line) if($Property -like "*NetbiosName*"){$netname = $Match} $table_line += "$Property=$Match " } $table_temp = @" $table_line "@ #prepare line to be convert to hash table $table_hash = ConvertFrom-StringData -StringData $table_temp #convert the curent line into a hash table -> @{NetBiosName=BEAH-C;OSBuild=19044;AdSite=RDL} $table += [Pscustomobject]$table_hash #convert hash table to a custum object and add in to main table variable } return $table #return complette array of custum object (one per line) } function Out-FormatQuery($data,$Property_List){ $table_line = "" #temporary line contains property/value $table = @() #main table $table_temp = "" #temporary variable contain pre-formated string for hash table convertion $tot = $data.Count #nombre total des ligne a traité foreach($line in $data) #read all line of query result { $table_line = "" #clear temp variable contain all property/value in on line $i++ #add 1 to display $i/$tot $per = [math]::Round(($i*100)/$tot,2) #calculer un pourcentage avec $i/$tot foreach($Property in $Property_List) #read all line of property list (variable past from function parameter) { Write-Progress -PercentComplete $per -Activity "Formating SCCM Query Data $per`%" -Status "Formating the line $i`/$tot at Property: $Property " #display progress bar and update information if($Property -like "*SystemOUName*") { $text1 = "$Property = {" #text for regex 1 parti $text2 = "`"};" #text for regex 2 parti } else { $text1 = "$Property = `"" #text for regex 1 parti $text2 = "`";" #text for regex 2 parti } $Regex = [Regex]::new("(?<=$text1`)(.*)(?=$text2`)") #regex tool for select 1 string between 2 other $Match = $Regex.Match($line) if($Property -like "*NetbiosName*"){$netname = $Match} $table_line += "$Property=$Match " } $table_temp = @" $table_line "@ #prepare line to be convert to hash table $table_hash = ConvertFrom-StringData -StringData $table_temp #convert the curent line into a hash table -> @{NetBiosName=BEAH-C;OSBuild=19044;AdSite=RDL} $table += [Pscustomobject]$table_hash #convert hash table to a custum object and add in to main table variable } return $table #return complette array of custum object (one per line) } # Export functions Export-ModuleMember -Function @('Invoke-FormatQuery','Out-FormatQuery') |