错误流是Powershell的一种输出流,用于存储脚本执行过程中发生的错误信息。 Write-Error命令的语法如下: Write-Error -Message <String> -Category <String> -TargetObject <Object> <CommonParameters> 参数说明: -Message:指定要写入错误流的错误消息。 -Category:指定错误的分类。常见的分类包括NotSpecified、OpenErr...
Write-Error [-Message] <string> [-Category <ErrorCategory>] [-ErrorId <string>] [-TargetObject <Object>] [-RecommendedAction <string>] [-CategoryActivity <string>] [-CategoryReason <string>] [-CategoryTargetName <string>] [-CategoryTargetType <string>] [<CommonParameters>]Power...
但是根据我的测试,发现Write-Warning和Write-Error的输出最大宽度就是控制台的宽度(换行位置取决于控制台大小),超出即换行输出。 参考 https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-warning?view=powershell-5.1
Write-Error -Message "Houston, we have a problem." -ErrorAction Stop 感谢Lee Dailey 提醒我可以这样使用 -ErrorAction Stop。Cmdlet -ErrorAction Stop如果在任何高级函数或 cmdlet 上指定 -ErrorAction Stop,它会把所有 Write-Error 语句转为终止错误,这些错误会使执行停止或可由 catch 处理。Power...
Write-Error -Exception 我們可以將這些具類型的例外狀況新增至Write-Error,我們仍然可以catch依例外狀況類型錯誤。 如Write-Error下列範例所示使用: PowerShell # with normal messageWrite-Error-Message"Could not find path:$path"-Exception([System.IO.FileNotFoundException]::new())-ErrorActionStop# With messa...
try { # 在这里执行可能会出错的代码 # 例如,调用一个可能会失败的函数 Some-FailingFunction } catch { # 获取错误消息 $errorMessage = $Error[0].Exception.Message Write-Host "执行失败: $errorMessage" } 在这个示例中,如果Some-FailingFunction函数执行失败,catch块将捕获到错误,并将错误消息存储在$error...
Write-Error -Message "Houston, we have a problem." -ErrorAction Stop Thank you to Lee Dailey for reminding about using -ErrorAction Stop this way. Cmdlet -ErrorAction Stop If you specify -ErrorAction Stop on any advanced function or cmdlet, it turns all Write-Error statements into termin...
$host.PrivateData.ErrorForegroundColor = "Green" You have now configured your console settings, but you’ll need to save them to your profile settings so that the screen opens up exactly as you want it to, every time. For that, first run the command $profile. This will show you the na...
'$ErrMsg+=' Try running this script with administrator privileges. '$ErrMsg+=$_.Exception.MessageWrite-Error$ErrMsg} FileSize :69632IsLogFull : False LastAccessTime :3/13/201909:41:46LastWriteTime :3/13/201909:41:46OldestRecordNumber :1RecordCount :23LogName : Security LogType : Administrative ...
# [Console]::OutputEncoding = [System.Text.Encoding]::UTF8Write-Host-NoNewline"`r"Write-Host-NoNewline" %@@@`r"Write-Host-NoNewline" @@@`r"Write-Host-NoNewline" %@@@`r"Write-Host-NoNewline" @@@`r"Write-Host-NoNewline" @@@:`r"Write-Host-NoNewline" %@@@...