我使用powershell作为Windows中的shell。当我尝试启动PATH环境变量中缺少dll依赖项的某些应用程序时,则什么也没有发生,powershell只是以新的命令提示符静默返回。
有没有办法使Powershell更大声地失败,告诉我确切缺少的内容,就像默认的cmd Shell一样?
我遇到了同样的问题。 PowerShell将$LASTEXITCODE
代码设置为-1073741515
(0xC0000142
,3221225794
),但没有输出说明实际出了什么问题。通过cmd.exe运行它时,我会弹出类似以下内容的窗口:
代码执行无法进行,因为未找到some.dll。重新安装该程序可能会解决此问题。
cygwin bash输出与stderr找不到的dll相关的错误,如果通过PowerShell中的bash运行相同的dll,则可以看到错误:
> & 'C:\tools\cygwin\bin\bash.exe' '-c' '"C:/Users/xxx/dir/main.exe"'
C:/Users/xxx/dir/main.exe: error while loading shared libraries: another.dll: cannot open shared object file: No such file or directory
这也适用于git bash:
> & 'C:\Program Files\Git\bin\bash.exe' '-c' '"C:/Users/xxx/dir/main.exe"'
C:/Users/xxx/dir/main.exe: error while loading shared libraries: another.dll: cannot open shared object file: No such file or directory
相当不错,但总比没有好。
恐怕无法获取该信息...但是请尝试阅读
PowerShell中错误处理简介 http://blogs.msdn.com/b/kebab/archive/2013/06/09/an-introduction-to-error-handling-in-powershell.aspx
或PowerShell教程–最终尝试捕获并在PowerShell中进行错误处理http://www.vexasoft.com/blogs/powershell/7255220-powershell-tutorial-try-catch-finally-and-error-handling-in-powershell
Try
{
$AuthorizedUsers = Get-Content \\ FileServer\HRShare\UserList.txt -ErrorAction Stop
}
Catch [System.OutOfMemoryException]
{
Restart-Computer localhost
}
Catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
Send-MailMessage -From [email protected] -To [email protected] -Subject "HR File Read Failed!" -SmtpServer EXCH01.AD.MyCompany.Com -Body "We failed to read file $FailedItem. The error message was $ErrorMessage"
Break
}
Finally
{
$Time=Get-Date
"This script made a read attempt at $Time" | out-file c:\logs\ExpensesScript.log -append
}