0

VBScript에서 PowerShell 명령을 실행하고 있습니다.VBScript의 PowerShell에서 오류 메시지를 다시 얻는 방법

다음은 내가 Enable-Mailbox 명령이 실패 할 때 그래서 다시 오류 메시지가 오지 않는

'This Script is used for creating Mailboxes for Active Directory Users. 
'This script triggers a Power Shell Script which creates the mailbox for the 
'ActiveDirectory User. 
' 
Set args = WScript.Arguments 
'Argument 0 contains the identity User Name 
WScript.Echo args.Item(0) 
'Argument 1 contains the Mail Store Alias Name 
WScript.Echo args.Item(1) 
'Argument 2 contains the Mail Database 
WScript.Echo args.Item(2) 
'Argument 3 contains the Report Log Path 
WScript.Echo args.Item(3) 

On Error Resume Next 

Dim shell 
Set shell = CreateObject("WScript.Shell") 

'Firing the PowerShell command from VBScript 
shell.Run "PowerShell.exe -PSConsoleFile ""E:\Program Files\Microsoft\Exchange Server\Bin\exshell.psc1"" -NoExit ""&{""Enable-Mailbox -Identity '"&Replace(args.Item(0),"'", "''")&"' -Alias '"&args.Item(1)&"' -Database '"&args.Item(2)&"';""exit 0""} ",,20 

If Err.Number <> 0 Then 
    WScript.Echo("Error Occurred in CreateMailBoxExchange script" & Err.Description) 
    WScript.Quit(2) 
End If 
WScript.Quit(3) 

를 실행하고 VBScript를합니다. 해당 메시지를 어떻게 캡처하여 사용자에게 다시 보내야합니까?

+0

try/catch Enable-Mailbox. 제대로 작동하지 않을 때 0이 아닌 종료 코드를 반환합니다. 오류 코드의 의미를 문서화하거나 기록하십시오. % LASTEXITCODE %를 검사하십시오. 아니면 PowerShell로 모든 것을 옮기고 래퍼를 제거하십시오 :) –

답변

1

Enable-Mailbox 아무 것도 표시하지 않지만 최소한 automatic variable$?을 설정해야합니다.이 값은 마지막 cmdlet이 성공적으로 실행되었는지 여부를 나타냅니다.

당신은 정수로 그 변수의 부울 값을 캐스팅하고 종료 코드로 그것을 반환 할 수 있습니다 : 당신은 어쨌든 아무것도하지 않는 PowerShell 명령 문자열 (...; "exit 0")에 무엇을 가지고

Enable-Mailbox ...; exit [int]$? 

, 실제로는 종료 코드를 설정하지 않고 단순히 문자열 "exit 0"을 반향하므로 (기본값 0이 리턴됩니다). 그러나 당신이 그 문 주위에 따옴표를 제거하더라도 여전히 항상 (1True0False) 적절한 종료 코드는 숫자 값을 가지고 부울로 다시 변환 할 수 있습니다를 설정하여

0을 반환 :

enabled = CBool(shell.Run("...", 0, True)) 

하거나 숫자 값 취

exitcode = shell.Run("...", 0, True) 

그러나, 후자는 일이 반전한다을 반환 값 (exit [int](!$?))은 관습에 따라 숫자 종료 상태 0은 성공을 나타내는 반면, 숫자가 아닌 0 종료 코드는 신호 오류 상태에 사용되기 때문에.