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

Powershell: What's the right way to output full error messages from New-Item?

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

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

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, 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      :