invoke-command -filepath ??

I have a hosts file (hosts.txt)



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?


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

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?

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.

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 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)