locked
script not returning computer name variable in write-output RRS feed

  • Question

  • Can anyone provide advise pleaes !

    I was wondering why below script not returning computer name variable when entered in write-output function

    foreach($computer in (Get-Content "C:\scripts\servers1.txt")){
        if(test-connection $computer -Count 2 -Quiet){
            invoke-command -ComputerName $computer -ScriptBlock{
                Get-NetAdapter | where status -eq 'up' | foreach{
                    Set-DNSClientServerAddress –interfaceIndex $_.ifIndex –ServerAddresses ("10.0.0.1","10.0.0.2") -Verbose 
                    write-output "ip changed to '$computer' - $(get-date)"
                }
            }   
        }
        else{
            write-output "Unable to connect to '$computer' - $(get-date)"
        }
    }


    Tareq dehneh

    Friday, June 14, 2019 3:18 PM

Answers

  • That is not available in an Invoke.  THe variable does not exist remotely.  It is a local variable.

    Use this inside the script block.

    write-output "ip changed to '$($env:computername)' - $(get-date)"


    \_(ツ)_/

    • Marked as answer by jrv Saturday, June 15, 2019 9:47 AM
    Friday, June 14, 2019 4:08 PM

All replies

  • That is not available in an Invoke.  THe variable does not exist remotely.  It is a local variable.

    Use this inside the script block.

    write-output "ip changed to '$($env:computername)' - $(get-date)"


    \_(ツ)_/

    • Marked as answer by jrv Saturday, June 15, 2019 9:47 AM
    Friday, June 14, 2019 4:08 PM
  • Thank you so much . It worked !

    Tareq dehneh

    • Marked as answer by TAREQ01 Saturday, June 15, 2019 3:21 AM
    • Unmarked as answer by jrv Saturday, June 15, 2019 9:47 AM
    Saturday, June 15, 2019 3:21 AM