0

I have this script to test a specific site. It works but not the way I want to. After the "try" parameter, the variable $hostpc is not getting called.

This is the output I'm getting:

Testing Server:
Web Ping on Succeeded.

It should be something like this:

Testing Server: servername
Web Ping on servername Succeeded.


Script

$servers = Get-Content 'some.txt'

Foreach ($hostpc in $servers) {

    Invoke-Command $hostpc -ScriptBlock { 

        try {

            write-host "Testing Server: $hostpc"
            $request = [System.Net.WebRequest]::Create("some web site")
            $response = $request.GetResponse()
            Write-Host "Web Ping on $hostpc Succeeded."   -foregroundcolor Yellow
        } catch {
            Write-Host ("Web Ping on $hostpc FAILED!!! The error was '{0}'." -f $_)-foregroundcolor Red
        } 

    }
}
mklement0
  • 382,024
  • 64
  • 607
  • 775
RamosT
  • 11
  • 2
    Possible duplicate of [How can i pass a string/variable for get-filehash inside invoke-command in powershell?](http://stackoverflow.com/questions/35492437/how-can-i-pass-a-string-variable-for-get-filehash-inside-invoke-command-in-power) – mklement0 Apr 14 '16 at 20:29
  • 1
    The short of it: local variables aren't available on remote machines by default; in PSv3+, use `$using:hostpc`; in lower versions use `-ArgumentList` and `$args[0]` or an explicit `param($hostpc)` declaration inside the script block. – mklement0 Apr 14 '16 at 20:32
  • 1
    UPDATEI ended up using $using:hostpc it works perfect. – RamosT Apr 15 '16 at 12:25
  • Why is this question downvoted? – Nikhil Vartak Apr 15 '16 at 13:34
  • @RamosT: Glad to hear it. Just to clarify: the problem was related to `Invoke-Command` (alias `icm`) executing on a _remote_ machine that doesn't have access to _local_ variables, and not related to the `try` block. – mklement0 Apr 15 '16 at 15:36
  • @nikhilvartak: I didn't down-vote, but I suspect it was down-voted for poor initial formatting. – mklement0 Apr 15 '16 at 15:38
  • I've retitled the linked question that this one's a duplicate of to make that more obvious: [How can I pass a local variable to a script block executed on a remote machine with Invoke-Command?](http://stackoverflow.com/questions/35492437/how-can-i-pass-a-local-variable-to-a-script-block-executed-on-a-remote-machine-w) – mklement0 Apr 15 '16 at 17:24

1 Answers1

-1

I think your Variable and thus Ping is really working. The problem is with log display because you are not concatenating variables with string properly.

write-host "Testing Server: $hostpc"

Change to write-host "Testing Server: $($hostpc)"

Write-Host "Web Ping on $hostpc Succeeded."

Change to Write-Host "Web Ping on $($hostpc) Succeeded."

Nikhil Vartak
  • 5,002
  • 3
  • 26
  • 32
  • No, a simple variable reference such as `$hostpc` inside a double-quoted string works just fine (try `"Honey, I'm $HOME"`); see my comments on the question for what the real problem is. – mklement0 Apr 14 '16 at 21:13