Powershell:从New-Item输出完整错误消息的正确方法是什么?



我正在运行一个vagrantwinrm命令,注意到失败的命令不会打印出整个错误输出。。。我认为|可能用于扩展此类命令的输出。。。但经过一些互联网搜索,并尝试了一些选项,如:

  • | fl
  • | Format-Table -Wrap -Au

我在的最终输出中仍然得到...我的错误消息,即在命令为echo'd的部分。

NewItemIOError
At line:1 char:1
+ New-Item -path C:varlibkubeletetckubernetespki -type SymbolicL ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

上述故障的发生有一个明确而明显的原因,所以我不需要帮助调试这个问题,但我通常想知道New-Item使用什么是正确的选项,以确保powershell用10000行日志向我显示其在自动化环境中失败执行的全部输出。

从新项目和类似的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, CmdletProviderContext context, 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, CmdletProviderContext context, Boolean escapeCurrentLocation, PSDriveInfo& workingDriveForPath, CmdletProvider& providerInstance)
at System.Management.Automation.LocationGlobber.GetProviderPath(String path, CmdletProviderContext context, 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()

PS。如果你想要更多信息:

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, CmdletProviderContext context, Boolean escapeCurrentLocation, PSDriveInfo& workingDriveForPath, CmdletProvider& providerInstance)
at System.Management.Automation.LocationGlobber.GetProviderPath(String path, CmdletProviderContext context, 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()
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      :

相关内容

最新更新