Invoke-Command script block not working

Welcome Forums General PowerShell Q&A Invoke-Command script block not working

Viewing 7 reply threads
  • Author
    Posts
    • #213135
      Participant
      Topics: 4
      Replies: 5
      Points: 44
      Rank: Member

      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*****
      
      • This topic was modified 1 month, 4 weeks ago by Ryan. Reason: fixed typo
      • This topic was modified 1 month, 4 weeks ago by kvprasoon. Reason: code formatting
      • This topic was modified 1 month, 4 weeks ago by kvprasoon.
    • #213144
      Senior Moderator
      Topics: 8
      Replies: 1215
      Points: 4,334
      Helping Hand
      Rank: Community Hero

      Try doing Enable-PSRemoting -SkipNetworkProfileCheck -Force

    • #213174
      Participant
      Topics: 4
      Replies: 5
      Points: 44
      Rank: Member

      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.

    • #213183
      Senior Moderator
      Topics: 8
      Replies: 1215
      Points: 4,334
      Helping Hand
      Rank: Community Hero

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

    • #213291
      Participant
      Topics: 4
      Replies: 5
      Points: 44
      Rank: Member

      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

    • #214044
      Participant
      Topics: 4
      Replies: 5
      Points: 44
      Rank: Member

      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.

    • #214047
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      I didn’t realize that Invoke-Command would not work on a non-server class machine.

      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.

    • #214119
      Participant
      Topics: 23
      Replies: 48
      Points: 285
      Helping Hand
      Rank: Contributor

      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.

      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.

Viewing 7 reply threads
  • You must be logged in to reply to this topic.