PowerShell-Docs-DSC icon indicating copy to clipboard operation
PowerShell-Docs-DSC copied to clipboard

Getting started instructions fail

Open ComputerHabit2 opened this issue 11 months ago • 6 comments

Type of issue

Code doesn't work

Feedback

Getting started instructions fail to work properly.

In this part of the document: https://learn.microsoft.com/en-us/powershell/dsc/get-started/?view=dsc-3.0#get-the-current-state-of-a-resource

PS C:\Windows\System32> dsc resource get --resource $resource.type --input $instance
error: unexpected argument 'NT\\\\CurrentVersion
}' found

Usage: dsc.exe resource get [OPTIONS] --resource <RESOURCE>

Page URL

https://learn.microsoft.com/en-us/powershell/dsc/overview?view=dsc-3.0

Content source URL

https://github.com/MicrosoftDocs/PowerShell-Docs-DSC/blob/main/dsc/docs-conceptual/dsc-3.0/overview.md

Author

@michaeltlombardi

Platform Id

3d84f5db-e174-af66-b5ec-b353f87e07dc

Document Id

7befffa4-ef78-f531-1a36-ecce7355978c

ComputerHabit2 avatar May 13 '25 21:05 ComputerHabit2

I suspect the issue is something with using single quotes in the example and there is a space between

PS C:\Windows\System32> $instance
{
    "valueName":  "SystemRoot",
    "keyPath":  "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion"
}

ComputerHabit2 avatar May 13 '25 21:05 ComputerHabit2

I picked random entry. Seems like anything JSON might be an issue in the microsoft world.

PS C:\Windows\system32> $instance = @{
  keyPath   = 'HKLM\SOFTWARE\Microsoft\.NETFramework\Advertised\Policy\AppPatch\v2.0.50727.00000\BTSNTSvc.exe\{CA109828-7CE7-40F4-AD73-C7575455A7D5}'
  valueName = 'Company'
} | ConvertTo-Json

PS C:\Windows\system32> dsc resource get --resource $resource.type --input $instance

dsc : [2m2025-05-13T21:52:54.072885Z[0m [31mERROR[0m Invalid JSON or YAML: YAML: did not find expected ',' or '}' at line 3 column 119, 
while parsing a flow mapping
At line:1 char:1
+ dsc resource get --resource $resource.type --input $instance
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: ([2m2025-05-13T... a flow mapping:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

ComputerHabit2 avatar May 13 '25 21:05 ComputerHabit2

It's mainly on the escape characters:

Image

What happens when you execute the following block?

dsc resource get --resource Microsoft.Windows/Registry --input '{"keyPath":"HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion","valueName":"SystemRoot"}'

Gijsreyn avatar May 14 '25 06:05 Gijsreyn

It looks like this might be a bug in Windows PowerShell.

Here's the sample executing in PowerShell:

Screenshot of terminal showing the example code from the article executing as expected without error

And again, but in Windows PowerShell:

Screenshot showing the reported error in a terminal running Windows PowerShell

Curiously, I tried to compress the JSON to see if that would help:

$instance = @{
  keyPath   = 'HKLM\Software\Microsoft\Windows NT\CurrentVersion'
  valueName = 'SystemRoot'
} | ConvertTo-Json -Compress

dsc resource get --resource $resource.type --input $instance
2025-05-14T14:10:45.230145Z ERROR Error: Schema: "keyPath" is a required property

I wondered if this could possibly be some goofy behavior for key ordering, but no:

$instance = [ordered]@{
  keyPath   = 'HKLM\Software\Microsoft\Windows NT\CurrentVersion'
  valueName = 'SystemRoot'
} | ConvertTo-Json -Compress

dsc resource get --resource $resource.type --input $instance
2025-05-14T14:11:35.411757Z ERROR Error: Schema: "keyPath" is a required property

It looks like something strange is happening with the JSON in Windows PowerShell. So I handcrafted the JSON:

$instanceJson = '{"keyPath":"HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion","valueName":"SystemRoot"}'
dsc resource get --resource $resource.type --input $instanceJson

And the same behavior - works in PowerShell, errors in Windows PowerShell:

Screenshot of terminal showing the previous commands executing without error in PowerShell and erroring in Windows PowerShell

Continuing to investigate, but my guess right now has to do with argument parsing for native commands in Windows PowerShell.

michaeltlombardi avatar May 14 '25 14:05 michaeltlombardi

Hello and thank you for responding.

I discovered that the commands actually do work. I was using older PowerShell 5.1 installed by default.

I installed PowerShell 7 and the scripts appear to work.

winget install --id Microsoft.PowerShell --source winget

Perhaps the documentation could say to use PS 7 only.

Thanks again.

ComputerHabit2 avatar May 14 '25 14:05 ComputerHabit2

I think the difference in behavior between PS7 and WinPS5.1 is due to this PR which addresses some long standing arg passing issues to native command sin WinPS5.1 particularly with quotes and whitespace. PS7 behaves more like other shells. I think for WinPS5.1 use, my recommendation would be to use STDIN to pass in the JSON to avoid problems with quotes/whitespace and put that in the docs so users aren't surprised by the behavior.

SteveL-MSFT avatar May 15 '25 21:05 SteveL-MSFT