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

jay*_*100 2 powershell verbose new-item

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

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

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

NewItemIOError
At line:1 char:1
+ New-Item -path C:\var\lib\kubelet\etc\kubernetes\pki\ -type SymbolicL ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

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

从 new-item 和类似的 powershell 命令输出详细错误消息的最简单方法是什么?

小智 8

简而言之 - 控制台中异常消息中的点仅用于显示目的 - 不会为您提供文本墙。如果你想显示完整的异常,你可以使用这样的东西:

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
}
Run Code Online (Sandbox Code Playgroud)

这将显示如下信息,并为您提供有关异常对象的完整信息:)

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
Run Code Online (Sandbox Code Playgroud)

对于完整的消息或堆栈跟踪,只需使用$_.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)"
}
Run Code Online (Sandbox Code Playgroud)

它提供了信息:

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()
Run Code Online (Sandbox Code Playgroud)

附言。如果您想了解更多信息:

try {
    New-Item NONEXISTING:\asd.txt -ErrorAction Stop
    Write-Host "If it shows error was not caught"
}
catch {
    $_ | Format-List -Force
}
Run Code Online (Sandbox Code Playgroud)

这将显示包括 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      :
Run Code Online (Sandbox Code Playgroud)