-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update WMI adapter to perform query based on provided properties #548
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f90b257
Update WMI adapter to perform query based on provided properties
1ec50fc
add filtering based on property values
4721a10
add TODO note on validating properties
79a03d6
Merge branch 'main' into demo
SteveL-MSFT f2da909
add comment on trap
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json | ||
resources: | ||
- name: WMI | ||
type: Microsoft.Windows/WMI | ||
properties: | ||
resources: | ||
- name: computer system | ||
type: root.cimv2/Win32_ComputerSystem | ||
properties: | ||
name: | ||
domain: | ||
totalphysicalmemory: | ||
model: | ||
manufacturer: | ||
- name: operating system | ||
type: root.cimv2/Win32_OperatingSystem | ||
properties: | ||
caption: | ||
version: | ||
osarchitecture: | ||
oslanguage: | ||
- name: system enclosure | ||
type: root.cimv2/Win32_SystemEnclosure | ||
properties: | ||
manufacturer: | ||
model: | ||
serialnumber: | ||
- name: bios | ||
type: root.cimv2/Win32_BIOS | ||
properties: | ||
manufacturer: | ||
version: | ||
serialnumber: | ||
- name: network adapter | ||
type: root.cimv2/Win32_NetworkAdapter | ||
properties: | ||
name: | ||
macaddress: | ||
adaptertype: | ||
netconnectionid: | ||
serviceName: | ||
netconnectionstatus: 2 | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,10 +9,28 @@ param( | |
$stdinput | ||
) | ||
|
||
trap { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be good to have a comment about what this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, will add comment |
||
Write-Trace -Level Error -message $_.Exception.Message | ||
exit 1 | ||
} | ||
|
||
$ProgressPreference = 'Ignore' | ||
$WarningPreference = 'Ignore' | ||
$VerbosePreference = 'Ignore' | ||
|
||
function Write-Trace { | ||
param( | ||
[string]$message, | ||
[string]$level = 'Error' | ||
) | ||
|
||
$trace = [pscustomobject]@{ | ||
$level = $message | ||
} | ConvertTo-Json -Compress | ||
|
||
$host.ui.WriteErrorLine($trace) | ||
} | ||
|
||
function IsConfiguration($obj) { | ||
if ($null -ne $obj.metadata -and $null -ne $obj.metadata.'Microsoft.DSC' -and $obj.metadata.'Microsoft.DSC'.context -eq 'Configuration') { | ||
return $true | ||
|
@@ -29,7 +47,6 @@ if ($Operation -eq 'List') | |
{ | ||
$version_string = ""; | ||
$author_string = ""; | ||
$moduleName = ""; | ||
|
||
$propertyList = @() | ||
foreach ($p in $r.CimClassProperties) | ||
|
@@ -79,17 +96,69 @@ elseif ($Operation -eq 'Get') | |
$wmi_namespace = $type_fields[0].Replace('.','\') | ||
$wmi_classname = $type_fields[1] | ||
|
||
#TODO: add filtering based on supplied properties of $r | ||
$wmi_instances = Get-CimInstance -Namespace $wmi_namespace -ClassName $wmi_classname | ||
# TODO: identify key properties and add WHERE clause to the query | ||
if ($r.properties) | ||
{ | ||
$query = "SELECT $($r.properties.psobject.properties.name -join ',') FROM $wmi_classname" | ||
$where = " WHERE " | ||
$useWhere = $false | ||
$first = $true | ||
foreach ($property in $r.properties.psobject.properties) | ||
{ | ||
# TODO: validate property against the CIM class to give better error message | ||
if ($null -ne $property.value) | ||
{ | ||
$useWhere = $true | ||
if ($first) | ||
{ | ||
$first = $false | ||
} | ||
else | ||
{ | ||
$where += " AND " | ||
} | ||
|
||
if ($property.TypeNameOfValue -eq "System.String") | ||
{ | ||
$where += "$($property.Name) = '$($property.Value)'" | ||
} | ||
else | ||
{ | ||
$where += "$($property.Name) = $($property.Value)" | ||
} | ||
} | ||
} | ||
if ($useWhere) | ||
{ | ||
$query += $where | ||
} | ||
Write-Trace -Level Trace -message "Query: $query" | ||
$wmi_instances = Get-CimInstance -Namespace $wmi_namespace -Query $query -ErrorAction Stop | ||
} | ||
else | ||
{ | ||
$wmi_instances = Get-CimInstance -Namespace $wmi_namespace -ClassName $wmi_classname -ErrorAction Stop | ||
} | ||
|
||
if ($wmi_instances) | ||
{ | ||
$instance_result = @{} | ||
# TODO: for a `Get`, they key property must be provided so a specific instance is returned rather than just the first | ||
$wmi_instance = $wmi_instances[0] # for 'Get' we return just first matching instance; for 'export' we return all instances | ||
$wmi_instance.psobject.properties | %{ | ||
if (($_.Name -ne "type") -and (-not $_.Name.StartsWith("Cim"))) | ||
{ | ||
$instance_result[$_.Name] = $_.Value | ||
if ($r.properties) | ||
{ | ||
if ($r.properties.psobject.properties.name -contains $_.Name) | ||
{ | ||
$instance_result[$_.Name] = $_.Value | ||
} | ||
} | ||
else | ||
{ | ||
$instance_result[$_.Name] = $_.Value | ||
} | ||
} | ||
} | ||
|
||
|
@@ -98,7 +167,7 @@ elseif ($Operation -eq 'Get') | |
else | ||
{ | ||
$errmsg = "Can not find type " + $r.type + "; please ensure that Get-CimInstance returns this resource type" | ||
Write-Error $errmsg | ||
Write-Trace $errmsg | ||
exit 1 | ||
} | ||
} | ||
|
@@ -110,11 +179,12 @@ elseif ($Operation -eq 'Get') | |
$wmi_classname = $type_fields[1] | ||
|
||
#TODO: add filtering based on supplied properties of $inputobj_pscustomobj | ||
$wmi_instances = Get-CimInstance -Namespace $wmi_namespace -ClassName $wmi_classname | ||
$wmi_instances = Get-CimInstance -Namespace $wmi_namespace -ClassName $wmi_classname -ErrorAction Stop | ||
|
||
if ($wmi_instances) | ||
{ | ||
$wmi_instance = $wmi_instances[0] # for 'Get' we return just first matching instance; for 'export' we return all instances | ||
# TODO: there's duplicate code here between configuration and non-configuration execution and should be refactored into a helper | ||
$wmi_instance = $wmi_instances[0] | ||
$result = @{} | ||
$wmi_instance.psobject.properties | %{ | ||
if (($_.Name -ne "type") -and (-not $_.Name.StartsWith("Cim"))) | ||
|
@@ -126,7 +196,7 @@ elseif ($Operation -eq 'Get') | |
else | ||
{ | ||
$errmsg = "Can not find type " + $inputobj_pscustomobj.type + "; please ensure that Get-CimInstance returns this resource type" | ||
Write-Error $errmsg | ||
Write-Trace $errmsg | ||
exit 1 | ||
} | ||
} | ||
|
@@ -140,5 +210,5 @@ elseif ($Operation -eq 'Validate') | |
} | ||
else | ||
{ | ||
Write-Error "ERROR: Unsupported operation requested from wmigroup.resource.ps1" | ||
} | ||
Write-Trace "ERROR: Unsupported operation requested from wmigroup.resource.ps1" | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is my understanding correct that this is the only instance in this file that will use
WHERE
clause?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes