Invoke-Command script block not working

I’m trying to learn how to use an Invoke-Command with a ScriptBlock to add a registry key. 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’. However, when I run the script against my local hostname it appears to skip the reg commands in the script block. When I run those same commands directly in a PS prompt they run as expected and the reg key gets created.

I have Don Jones’ book “Learn Windows PowerShell in a Month of Lunches” and it mentions a common error after running the Enable-PSRemoting command which is related to “one of the network connection types on this machine is set to Public”. I did receive this error so I ran the following command to check it:

PS C:\> Get-NetConnectionProfile

Name : redacted
InterfaceAlias : Wi-Fi
InterfaceIndex : 4
NetworkCategory : Private
IPv4Connectivity : Internet
IPv6Connectivity : NoTraffic

So It appears my only network connect is of the type Private. Is that a bogus error? What am I missing here? Thanks for any guidance you can provide!

*****begin*****

$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
}
Stop-Transcript
*****end*****

Try doing Enable-PSRemoting -SkipNetworkProfileCheck -Force

Thanks for the -SkipNetworkProfilecheck switch. I ran this and it did not return the error this time.

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.

Just to make it clear, what are you expecting as output and what you are getting now ?

Thanks again. 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 then 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

Hi. It seems maybe this forum is not very active these days but I found the solution in another forum. I didn’t realize that Invoke-Command would not work on a non-server class machine. Once I tried it on a Windows server it worked perfectly fine. Thanks again.

For the cmdlet Invoke-Command it doesn’t matter if a computer is a server or a workstation. If that’s your conclusion it is wrong.

[quote quote=214044]Hi. It seems maybe this forum is not very active these days but I found the solution in another forum. I didn’t realize that Invoke-Command would not work on a non-server class machine. Once I tried it on a Windows server it worked perfectly fine. Thanks again.

[/quote]
To identify the issue you should go through the error. You are suppressing the same. How could we understand whats going on there? Even when we asked, you also failed to submit that.