-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathGet-WQLObject.ps1
63 lines (63 loc) · 3.58 KB
/
Get-WQLObject.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
param(
# WQL formatted query to perform
[Parameter(Mandatory = $true, ParameterSetName = 'CustomQuery')]
[string]
$Query,
# SMS Provider to query against
[Parameter(Mandatory = $true)]
[string]
$SMSProvider,
# Optional PSCredential (unfortunately I can't figure out how to use this cred in the DynamicParam WMI queries without providing info outside the function)
[Parameter(Mandatory = $false, ParameterSetName = 'CustomQuery')]
[pscredential]
$Credential
)
DynamicParam {
if (($SMSProvider = $PSBoundParameters['SMSProvider'])) {
$ParameterName = 'SCCMQuery'
$RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
$ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
$ParameterAttribute.Mandatory = $true
$ParameterAttribute.ParameterSetName = 'ExistingQuery'
$ParameterAttribute.HelpMessage = 'Specify the name of a query that already exists in your ConfigMgr environment'
$AttributeCollection.Add($ParameterAttribute)
$SiteCode = (Get-WmiObject -Namespace "root\sms" -ClassName "__Namespace" -ComputerName $SMSProvider).Name.Substring(5, 3)
$Namespace = [string]::Format("root\sms\site_{0}", $SiteCode)
$arrSet = Get-WmiObject -ComputerName $SMSProvider -Namespace $Namespace -Query "SELECT Name FROM SMS_Query WHERE Expression not like '%##PRM:%'" | Select-Object -ExpandProperty Name
$ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)
$AttributeCollection.Add($ValidateSetAttribute)
$RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection)
$RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)
return $RuntimeParameterDictionary
}
}
Begin {
$SCCMQuery = $PsBoundParameters[$ParameterName]
if ($PSBoundParameters.ContainsKey('Credential') -and -not $PSDefaultParameterValues.ContainsKey("Get-WmiObject:Credential")) {
$AddedDefaultParam = $true
$PSDefaultParameterValues.Add("Get-WmiObject:Credential", $Credential)
}
$SiteCode = (Get-WmiObject -Namespace "root\sms" -ClassName "__Namespace" -ComputerName $SMSProvider).Name.Substring(5, 3)
$Namespace = [string]::Format("root\sms\site_{0}", $SiteCode)
if ($PSCmdlet.ParameterSetName -eq 'ExistingQuery') {
$Query = Get-WmiObject -ComputerName $SMSProvider -Namespace $Namespace -Query "SELECT Expression FROM SMS_Query WHERE Name ='$SCCMQuery'" | Select-Object -ExpandProperty Expression
}
}
Process {
$RawResults = Get-WmiObject -ComputerName $SMSProvider -Namespace $Namespace -Query $Query
$PropertySelectors = $RawResults | Get-Member -MemberType Property | Where-Object { -not $_.Name.StartsWith('__') } | Select-Object -ExpandProperty name | ForEach-Object {
$Class = $_
$Properties = $RawResults.$Class | Get-Member -MemberType Property | Where-Object { -not $_.Name.StartsWith('__') } | Select-Object -ExpandProperty name
foreach ($Property in $Properties) {
[string]::Format("@{{Label='{1}.{0}';Expression = {{`$_.{1}.{0}}}}}", $Property, $Class)
}
}
}
end {
if ($AddedDefaultParam) {
$PSDefaultParameterValues.Remove("Get-WmiObject:Credential")
}
$PropertySelector = [scriptblock]::Create($($PropertySelectors -join ','))
$RawResults | Select-Object -Property $(. $PropertySelector)
}