NobleMan
NobleMan

Reputation: 515

Unexpected token error when I have the variable already defined

Simple script but can't figure out why I am getting the error : It should just write the machines that were successful to one text file and the failed to another.

Here is the error I am getting :

Unexpected token '$ip' in expression or statement. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnexpectedToken

$computers = Get-Content C:\temp\machines_no_rdp.txt

foreach($computer in $computers)

 {


  if((New-Object System.Net.NetworkInformation.Ping).Send("$computer",[int]1000).Address.IPAddressToString)

   {

    $computer
   $ip = [System.Net.Dns]::GetHostByName($computer).AddressList.IPAddressToString



    $computer + ";" $ip | Out-File C:\temp\Success.txt -Append

   }

    else{$computer + ";" + "Dead" | Out-File C:\temp\failure.txt -Append}

   }

Upvotes: 1

Views: 4733

Answers (1)

Matt
Matt

Reputation: 46710

The error is trying to tell you it does know what to do with $ip. It does not care at this point whether it is a variable or not. It's a parsing error.

$computer + ";" $ip

should instead be the following ( in keeping with your coding practice.)

$computer + ";" + $ip

Upvotes: 3

Related Questions