问题描述
我正在运行一个 vagrant
winrm 命令,并注意到失败的命令不会打印出整个错误输出...我认为 |
可能用于扩展从这样的命令......但经过一些互联网搜索,并尝试了一些选项,例如:
| fl
| Format-Table -Wrap -Au
我仍然在最终输出中得到一个 ...
我的错误消息,即在命令被回显的部分。
NewItemIOError
At line:1 char:1
+ New-Item -path C:\var\lib\kubelet\etc\kubernetes\pki\ -type SymbolicL ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
上述失败的发生有一个明确而明显的原因,所以我不需要帮助调试问题,而是想知道与 New-Item
一起使用的正确选项是什么,以确保 powershell 显示我在具有 10000 行日志的自动化环境中执行失败的整个输出。
从 new-item 和类似的 powershell 命令输出详细错误消息的最简单方法是什么?
解决方法
简单地说-控制台中异常消息中的点仅用于显示目的-不会为您提供文本墙。 如果您想显示 FULL 异常,您可以使用以下内容:
try {
New-Item NONEXISTING:\asd.txt -ErrorAction Stop #This will throw error
Write-Host "If it shows error was not caught"
}
catch {
$_.Exception | Format-List -Force
}
这会显示如下信息,并为您提供有关异常对象的完整信息:)
ErrorRecord : Cannot find drive. A drive with the name 'NONEXISTING' does not exist.
ItemName : NONEXISTING
SessionStateCategory : Drive
WasThrownFromThrowStatement : False
Message : Cannot find drive. A drive with the name 'NONEXISTING' does not exist.
Data : {}
InnerException :
TargetSite : System.Management.Automation.PSDriveInfo GetDrive(System.String,Boolean)
StackTrace : at System.Management.Automation.SessionStateInternal.GetDrive(String name,Boolean automount)
at System.Management.Automation.SessionStateInternal.GetDrive(String name,Boolean automount)
at System.Management.Automation.LocationGlobber.GetDriveRootRelativePathFromPSPath(String path,CmdletProviderContext context,Boolean escapeCurrentLocation,PSDriveInfo& workingDriveForPath,CmdletProvider& providerInstance)
at System.Management.Automation.LocationGlobber.GetProviderPath(String path,Boolean isTrusted,ProviderInfo& provider,PSDriveInfo& drive)
at System.Management.Automation.SessionStateInternal.NewItem(String[] paths,String name,String type,Object content,CmdletProviderContext context)
at Microsoft.PowerShell.Commands.NewItemCommand.ProcessRecord()
HelpLink :
Source : System.Management.Automation
HResult : -2146233087
对于完整的消息或堆栈跟踪,只需使用 $_.Exception.Message
或 $_.Exception.StackTrace
:
try {
New-Item NONEXISTING:\asd.txt -ErrorAction Stop; #This will throw error
Write-Host "If it shows error was not caught"
}
catch {
Write-Host "MESSAGE`n$($_.Exception.Message)"
Write-Host "STACK TRACE`n$($_.Exception.StackTrace)"
}
它提供信息:
MESSAGE
Cannot find drive. A drive with the name 'NONEXISTING' does not exist.
STACK TRACE
at System.Management.Automation.SessionStateInternal.GetDrive(String name,Boolean automount)
at System.Management.Automation.SessionStateInternal.GetDrive(String name,Boolean automount)
at System.Management.Automation.LocationGlobber.GetDriveRootRelativePathFromPSPath(String path,CmdletProvider& providerInstance)
at System.Management.Automation.LocationGlobber.GetProviderPath(String path,PSDriveInfo& drive)
at System.Management.Automation.SessionStateInternal.NewItem(String[] paths,CmdletProviderContext context)
at Microsoft.PowerShell.Commands.NewItemCommand.ProcessRecord()
附注。如果您想了解更多信息:
try {
New-Item NONEXISTING:\asd.txt -ErrorAction Stop
Write-Host "If it shows error was not caught"
}
catch {
$_ | Format-List -Force
}
这将显示所有信息,包括 ScriptStackTrace(异常发生在哪一行):
Exception : System.Management.Automation.DriveNotFoundException: Cannot find drive. A drive with the name 'NONEXISTING' does not exist.
at System.Management.Automation.SessionStateInternal.GetDrive(String name,Boolean automount)
at System.Management.Automation.SessionStateInternal.GetDrive(String name,Boolean automount)
at System.Management.Automation.LocationGlobber.GetDriveRootRelativePathFromPSPath(String path,CmdletProvider& providerInstance)
at System.Management.Automation.LocationGlobber.GetProviderPath(String path,PSDriveInfo& drive)
at System.Management.Automation.SessionStateInternal.NewItem(String[] paths,CmdletProviderContext context)
at Microsoft.PowerShell.Commands.NewItemCommand.ProcessRecord()
TargetObject : NONEXISTING
CategoryInfo : ObjectNotFound: (NONEXISTING:String) [New-Item],DriveNotFoundException
FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.NewItemCommand
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at <ScriptBlock>,<No file>: line 2
PipelineIterationInfo : {}
PSMessageDetails :