Skip to content

Commit 0df4278

Browse files
committed
Address feedback Steve
1 parent bab4712 commit 0df4278

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

wmi-adapter/Tests/wmi.tests.ps1

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ Describe 'WMI adapter resource tests' {
6161
}
6262

6363
It 'Set does not work without input for resource' -Skip:(!$IsWindows) {
64-
$out = dsc resource set --resource root.cimv2/Win32_Environment --input '{}' 2>&1
65-
$out[0] | Should -BeLike "*No valid properties found in the CIM class 'Win32_Environment' for the provided properties.*"
64+
$out = dsc resource set --resource root.cimv2/Win32_Environment --input '{}' 2>$TestDrive/error.log
65+
$out | Should -BeNullOrEmpty
66+
(Get-Content $TestDrive/error.log -Raw) | Should -BeLike "*No valid properties found in the CIM class 'Win32_Environment' for the provided properties.*"
6667
}
6768

6869
It 'Set does not work without a key property' -Skip:(!$IsWindows) {
@@ -71,8 +72,9 @@ Describe 'WMI adapter resource tests' {
7172
UserName = ("{0}\{1}" -f $env:USERDOMAIN, $env:USERNAME) # Read-only property is key, but we require a key property to be set
7273
} | ConvertTo-Json
7374

74-
$out = dsc resource set -r root.cimv2/Win32_Environment -i $i 2>&1
75-
$out[0] | Should -BeLike "*All key properties in the CIM class 'Win32_Environment' are read-only, which is not supported.*"
75+
$out = dsc resource set -r root.cimv2/Win32_Environment -i $i 2>$TestDrive/error2.log
76+
$out | Should -BeNullOrEmpty
77+
(Get-Content $TestDrive/error2.log -Raw) | Should -BeLike "*All key properties in the CIM class 'Win32_Environment' are read-only, which is not supported.*"
7678
}
7779

7880
It 'Set works on a WMI resource' -Skip:(!$IsWindows) {

wmi-adapter/wmi.dsc.resource.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"$Input | ./wmi.resource.ps1 Set"
4848
],
4949
"input": "stdin",
50-
"implementsPretest": true
50+
"implementsPretest": false
5151
},
5252
"validate": {
5353
"executable": "powershell",

wmi-adapter/wmiAdapter.psm1

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,24 +58,23 @@ function GetValidCimProperties {
5858
$availableProperties = $CimClass.CimClassProperties | Where-Object -Property Name -in $Properties.psobject.Properties.name
5959
$validatedProperties = [System.Collections.Generic.List[Array]]::new()
6060

61-
$keyProperty = ($availableProperties.flags -like "Property, Key*")
61+
$keyProperties = $availableProperties | Where-Object {$_.Flags.Hasflag([Microsoft.Management.Infrastructure.CimFlags]::Key)}
62+
6263

6364
if ($null -eq $availableProperties) {
6465
"No valid properties found in the CIM class '$ClassName' for the provided properties." | Write-DscTrace -Operation Error
6566
exit 1
6667
}
6768

68-
if ($null -eq $keyProperty) {
69-
"Key property not provided for CIM class '$ClassName'." | Write-DscTrace -Operation Error
70-
exit 1
71-
}
72-
7369
if ($ValidateKeyProperty.IsPresent) {
7470
# Check if any key property is also read-only
75-
$keyProps = $availableProperties | Where-Object { $_.Flags.ToString() -like "*Key*" }
76-
$readOnlyKeyProps = $keyProps | Where-Object { $_.Flags.ToString() -like "*ReadOnly*" }
71+
if ($keyProperties.Count -eq 0) {
72+
"No key properties found in the CIM class '$ClassName'." | Write-DscTrace -Operation Error
73+
exit 1
74+
}
75+
$readOnlyKeyProps = $keyProperties | Where-Object { $_.Flags.HasFlag([Microsoft.Management.Infrastructure.CimFlags]::ReadOnly) }
7776

78-
if ($readOnlyKeyProps.Count -eq $keyProps.Count) {
77+
if ($readOnlyKeyProps.Count -eq $keyProperties.Count) {
7978
"All key properties in the CIM class '$ClassName' are read-only, which is not supported." | Write-DscTrace -Operation Error
8079
exit 1
8180
}
@@ -134,7 +133,7 @@ function BuildWmiQuery {
134133
)
135134

136135
$targetProperties = if ($KeyPropertiesOnly.IsPresent) {
137-
$Properties | Where-Object { $_.Flags.ToString() -like "*Key*" }
136+
$Properties | Where-Object {$_.Flags.HasFlag([Microsoft.Management.Infrastructure.CimFlags]::Key)}
138137
} else {
139138
$Properties
140139
}
@@ -199,9 +198,11 @@ function GetWmiInstance {
199198
"No WMI instances found using query '$query'. Retrying with key properties only." | Write-DscTrace -Operation Debug
200199
$keyQuery = BuildWmiQuery -ClassName $wmi_classname -Properties $properties -DesiredStateProperties $DesiredState.properties -KeyPropertiesOnly
201200

202-
$wmi_instances = Get-CimInstance -Namespace $wmi_namespace -Query $keyQuery -ErrorAction Ignore -ErrorVariable err
203-
if ($null -eq $wmi_instances) {
204-
"No WMI instances found using key properties query '$keyQuery'." | Write-DscTrace -Operation Debug
201+
if ($keyQuery) {
202+
$wmi_instances = Get-CimInstance -Namespace $wmi_namespace -Query $keyQuery -ErrorAction Ignore -ErrorVariable err
203+
if ($null -eq $wmi_instances) {
204+
"No WMI instances found using key properties query '$keyQuery'." | Write-DscTrace -Operation Debug
205+
}
205206
}
206207
}
207208
}
@@ -268,7 +269,7 @@ function GetCimSpace {
268269
}
269270
}
270271
'Set' {
271-
$wmi_instance = ValidateCimMethodAndArguments -DesiredState $r
272+
$wmi_instance = GetCimInstanceProperties -DesiredState $r
272273
$properties = @{}
273274

274275
$wmi_instance.Properties | ForEach-Object {
@@ -277,7 +278,7 @@ function GetCimSpace {
277278
}
278279
}
279280

280-
$readOnlyProperties = $wmi_instance.Properties | Where-Object -Property Flags -like "*ReadOnly*"
281+
$readOnlyProperties = $wmi_instance.Properties | Where-Object {$_.Flags.HasFlag([Microsoft.Management.Infrastructure.CimFlags]::ReadOnly)}
281282

282283
if ($null -eq $wmi_instance.CimInstance) {
283284
$instance = New-CimInstance -Namespace $wmi_instance.Namespace -ClassName $wmi_instance.ClassName -Property $properties -ErrorAction Ignore -ErrorVariable err
@@ -312,7 +313,7 @@ function GetCimSpace {
312313
return $result
313314
}
314315

315-
function ValidateCimMethodAndArguments {
316+
function GetCimInstanceProperties {
316317
[CmdletBinding()]
317318
param (
318319
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]

0 commit comments

Comments
 (0)