2013-05-08 49 views
0

嗨,我试图找出如何做到这一点或做的另一种方式,它PowerShell的试用和拦截执行多个命令中尝试

try { 
Get-ADComputer -Identity namedoesnotexist 
(Get-ChildItem).FullName 
} 
catch {$_ | Out-File log.log} 

当运行这段代码,我使用的是不存在的,所以我得到一个名字一个错误和catch会把它写到我的日志文件中(只是一个例子) 我想完成的是错误被捕获,但try语句继续运行我的Get-Childitem命令并尝试这一点。 任何其他简单的方法呢?

回答

1

将只有一行在在try..catch会给你的效果

try 
{ 
    Get-ADComputer -Identity namedoesnotexist 
} 
catch 
{ 
    $_ | Out-File log.log 
} 
(Get-ChildItem).FullName 

但也许trap是你在找什么

trap 
{ 
    $_ | Out-File log.log 
    continue # remove this if you still want to see each error 
} 
Get-ADComputer -Identity namedoesnotexist 
(Get-ChildItem).FullName 
+1

使用'-Append'开关' Out-File“,因此您不会在每个错误上覆盖日志文件。 – 2013-05-08 20:04:16

+0

陷阱正是我正在寻找的。谢谢! – TelefoneN 2013-05-10 08:44:01