invoke-command -filepath ??

Welcome Forums General PowerShell Q&A invoke-command -filepath ??

Viewing 5 reply threads
  • Author
    Posts
    • #198761
      Participant
      Topics: 6
      Replies: 7
      Points: 115
      Rank: Participant

      I have a hosts file (hosts.txt)

      Computer1

      Computer2

      My simple script test.ps1

      get-content c:\temp\hosts.txt |foreach {test-connection -computername $_ -count 1}

      Trying to run

      invoke-command -computername Computer3 -filepath c:\temp\test.ps1

      It returns error that c:\temp\hosts.txt not found.

      Any idea?

       

    • #198764
      js
      Participant
      Topics: 28
      Replies: 772
      Points: 2,220
      Helping Hand
      Rank: Community Hero

      It’s looking for hosts.txt on the remote computer.

    • #198767
      Participant
      Topics: 4
      Replies: 1908
      Points: 4,259
      Helping Hand
      Rank: Community Hero

      Regardless of js’s valid comment …. are you sure you want to remote to another computer and run a ping command from there to a third computer?

    • #198776
      Participant
      Topics: 6
      Replies: 7
      Points: 115
      Rank: Participant

      We pre-imaged new computer with host name. However, due to DNS and DHCP issue, we always has 1,2 can’t ping remotely. If I can remote in subnet to ping, I can see it is alive. My next question is it any way to keep the hosts.txt on local drive instead of copying to remote computer.

    • #199241
      Participant
      Topics: 4
      Replies: 70
      Points: 192
      Helping Hand
      Rank: Participant

      Not sure I fully understand what you are doing, but to use local data i.e. your hosts file on a remote machine, you might want to assign the data to a variable and use the “using:” scope modifier in your invoke-command script block.  See Get-Help About_Scopes for more info.

      $checkhosts = get-content c:\temp\hosts.txt
      Invoke-Command -ComputerName Computer3 -ScriptBlock {
           $using:checkhosts | foreach {test-connection -computername $_ -count 1}
      }
      • This reply was modified 2 months, 1 week ago by Mike R..
    • #199277
      js
      Participant
      Topics: 28
      Replies: 772
      Points: 2,220
      Helping Hand
      Rank: Community Hero

      This is little known, you can also define a param to any script block. Although passing arrays is more painful. Note the (, )

      $checkhosts = get-content c:\temp\hosts.txt
      Invoke-Command Computer3,Computer4,Computer5 { param ($passhosts)
           $passhosts | foreach {test-connection -computername $_ -count 1}
      } -args (,$checkhosts)
      
      • This reply was modified 2 months, 1 week ago by js.
      • This reply was modified 2 months, 1 week ago by js.
      • This reply was modified 2 months, 1 week ago by js.
Viewing 5 reply threads
  • You must be logged in to reply to this topic.