2017-07-14 30 views
0

您好我有以下腳本輸出上一個命令導致未來一到失敗

winrs -r:test.one.two -u:test -p:'te$st' echo %computername% 

winrs -r:test2.one.two -u:test -p:'te$st' echo %computername% 

winrs -r:test3.one.two -u:test -p:'te$st' echo %computername% 

我有以下問題,如果第一winrs命令失敗,原因cannot resolve host name或結果是不同的,那麼預期的計算機名稱,例如空行。下一個命令也失敗了,有沒有辦法阻止這種行爲?忽略輸出或將其重定向到其他(但也是可見的)流?

回答

1

使用& cmd /c "winrs -r:test.one.two -u:test -p:'te$st' echo %computername% 2>&1"重定向錯誤,稍後您可以在每個級別使用try catch。

try 
{ 
    try 
    { 
    & cmd /c "winrs -r:test.one.two -u:test -p:'te$st' echo %computername% 2>&1" 
    } 
    catch 
    { 
    "1st winrs failed" 
    } 
    try 
    { 
    & cmd /c "winrs -r:test2.one.two -u:test -p:'te$st' echo %computername% 2>&1" 
    } 
    catch 
    { 
    "2nd winrs failed" 
    } 
    try 
    { 
    & cmd /c "winrs -r:test3.one.two -u:test -p:'te$st' echo %computername% 2>&1" 
    } 
    catch 
    { 
    "3rd winrs failed" 
    } 
} 
catch 
{ 
"Entire Script failed" 
} 

希望它有幫助。

相關問題