1

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

  • | 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 命令输出详细错误消息的最简单方法是什么?

4

1 回答 1

5

简单地说 - 控制台中异常消息中的点仅用于显示目的 - 不会给你文字墙。如果要显示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.Messageor $_.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      :
于 2021-06-07T18:43:18.033 回答