【问题标题】:Unexpected token error when I have the variable already defined当我已经定义了变量时出现意外的令牌错误
【发布时间】:2015-06-08 19:05:30
【问题描述】:

简单的脚本,但不知道为什么会出现错误:它应该只将成功的机器写入一个文本文件,而将失败的机器写入另一个。

这是我得到的错误:

表达式或语句中出现意外标记“$ip”。 + 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}

   }

【问题讨论】:

  • 这只是一个缺少的+$computer + ";" $ip 应该是 $computer + ";" + $ip 或许多其他字符串连接解决方​​案。
  • 再次非常感谢.. @Matt

标签: powershell powershell-2.0


【解决方案1】:

错误试图告诉你它确实知道如何处理$ip。此时它不关心它是否是一个变量。这是一个解析错误。

$computer + ";" $ip

应该改为以下(与您的编码实践保持一致。)

$computer + ";" + $ip

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-13
    • 2017-11-21
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-26
    相关资源
    最近更新 更多