Invoke-Command script block not executing

I’m trying to learn how to use an Invoke-Command with a ScriptBlock to add a registry key.  However, when I run the script against my local hostname it appears to skip the reg commands in the script block.  My end goal is to be able to run this against a list of remote computers, but for now I’m just testing it on my local Windows 10 machine.  I have already run ‘WinRM Quickconfig’ and ‘Enable-PSRemoting -force’.
PS C:\coding> Enable-PSRemoting -SkipNetworkProfileCheck -Force
WinRM is already set up to receive requests on this computer.
WinRM has been updated for remote management.
WinRM firewall exception enabled.
However, when I run my script it still does not execute the script block as expected.  Here is the script:
$Computers = Get-Content “C:\coding\PowerShell\ServerNames.txt” | Sort-Object
$ErrorActionPreference= ‘silentlycontinue’
Start-Transcript -path “C:\coding\PowerShell\RegFixes\serverlog.txt” -append
foreach ($Computer in $Computers){
    write-output “Working on $Computer”
    Invoke-Command -computername $Computer -ScriptBlock {
    New-Item -Name “DemoKey1”  -Path “HKLM:\System”  -type Directory
    New-ItemProperty -Path “HKLM:\System\DemoKey1” -Name “Test” -Value “0” -PropertyType “DWord”
 Start-Sleep -Seconds 3
I am expecting the following commands in the script block to run:
New-Item -Name “DemoKey1”  -Path “HKLM:\System”  -type Directory
New-ItemProperty -Path “HKLM:\System\DemoKey1” -Name “Test” -Value “0” -PropertyType “DWord”
When I run them directly in the PowerShell command prompt they run fine and they create the reg key as expected.  But they are not running from my script.  Here is the output I’m seeing now:
PS C:\coding\powershell\regfixes> ./Write-RegFix.ps1                                                                   
Transcript started, output file is C:\coding\PowerShell\RegFixes\serverlog.txt
Working on XYZ-Workstation
Transcript stopped, output file is C:\coding\PowerShell\RegFixes\serverlog.txt
Thank you for looking and for any advice you can offer!
Parents Reply Children
No Data