0

这是我想要在脚本块中工作的内容

$scriptblock={
for /f "tokens=14" %i in ('"ipconfig | findstr IPv4"') do set ip=%i

nslookup %ip%
}

每次虽然我得到

使用“1”参数调用“Create”的异常:“At line:4 char:4 + for /f "tokens=14" %i in ('"ipconfig | findstr IPv4"') do set ip=%i + ~ 在关键字 'for' 之后缺少开头的 '('。" 在 line:1 char:1 + $scriptblock=[scriptblock]::create($scriptblock) + ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ParseException

我尝试先将其设置为 here 字符串,然后使用[scriptblock]::create(),但仍然得到相同的结果。

如何将其放入脚本块?

4

1 回答 1

3

不要尝试混合使用批处理和 PowerShell 命令。要获取 IPv4 地址,请尝试以下操作:

$ip = (ipconfig | Foreach {if ($_ -match 'IPv4 address.*?:\s+(.*)') {$matches[1]}})[0]

如果您使用的是 Windows 8 或更高版本,则可以使用:

Get-NetIPAddress -AddressFamily IPv4 -AddressState Preferred
于 2015-09-28T14:29:15.253 回答