相关疑难解决方法(0)

错误变量输出

我现在正在寻找解决方案或帮助我理解原因的东西两天。

对于某些操作,我想将任何错误发送到“-ErrorVariable create_error”。没问题。之后,我只想要错误消息的例外:

$create_error = $create_error.Exception
Run Code Online (Sandbox Code Playgroud)

变量的输出现在是:

$create_error
Access to the path '2021' is denied.
Run Code Online (Sandbox Code Playgroud)

但是,如果我使用此变量作为电子邮件的正文,我会收到以下电子邮件文本:

    System.UnauthorizedAccessException: Access to the path '2021' is denied. 
    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 
    at System.IO.Directory.InternalCreateDirectory(String fullPath, String path, Object dirSecurityObj, Boolean checkHost) 
    at System.IO.DirectoryInfo.CreateSubdirectoryHelper(String path, Object directorySecurity) 
    at System.IO.DirectoryInfo.CreateSubdirectory(String path) 
    at Microsoft.PowerShell.Commands.FileSystemProvider.CreateDirectory(String path, Boolean streamOutput)
Run Code Online (Sandbox Code Playgroud)

发送邮件:

Send-MailMessage -To $to -From $from -Subject $subject -Body $create_error
Run Code Online (Sandbox Code Playgroud)

有什么想法如何在电子邮件中仅获取人类可读的错误消息吗?

谢谢你!此致!

powershell

1
推荐指数
1
解决办法
3125
查看次数

标签 统计

powershell ×1