(无效)命令
New-NetIPAddress -InterfaceAlias Ethernet0 -IPAddress 172.1.2.3 -PrefixLengh 27
会产生以下错误:
New-NetIPAddress : A parameter cannot be found that matches parameter name 'PrefixLengh'.
At line:1 char:65
+ ... ddress -InterfaceAlias Ethernet0 -IPAddress 172.1.2.3 -PrefixLengh 27
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-NetIPAddress], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,New-NetIPAddress
我想将此错误消息重定向到文件。
通常,这应该有点像这样:
New-NetIPAddress -InterfaceAlias Ethernet0 -IPAddress 172.1.2.3 -PrefixLengh 27 2>&1 > output
我尝试了很多组合,例如
*> out
,2>&1 3>&1 4>&1 5>&1 6>&1 > out
,但没有一个组合重定向输出。它总是被打印到屏幕上并且文件保持为空。 PS版本:5.1.22621.2506
(Windows 11 上最新的 powershell)。
如何正确地将此错误输出重定向到文件?
ParameterBindingException 是一个语句终止错误,在这种情况下您无法重定向它。您需要使用
try
\ catch
语句。有关 PowerShell 中错误处理的深入详细信息,请参阅 您想了解的有关异常的所有内容。
try {
New-NetIPAddress -InterfaceAlias Ethernet0 -IPAddress 172.1.2.3 -PrefixLengh 27
}
catch {
$_ | Out-File path\to\output
}