众所周知,这Write-Host是邪恶的.In PowerShell 5,Write-Information被添加并被认为是替换Write-Host.
但是,真的,哪个更好?
Write-Host因为它不使用管道是邪恶的,所以输入消息不能被重用.
但是,Write-Host只是在控制台中展示一些东西呢?在什么情况下我们应该重用输入?
无论如何,如果我们真的想重用输入,为什么不写这样的东西:
$foo = "Some message to be reused like saving to a file"
Write-Host $foo
$foo | Out-File -Path "D:\foo.log"
Run Code Online (Sandbox Code Playgroud)
另一个缺点Write-Host是,Write-Host可以通过使用-ForegroundColor和指定消息在控制台中显示的颜色-BackgroundColor.
另一方面,通过使用Write-Information,输入消息可以通过No.6管道在任何我们想要的地方使用.并且不需要像我上面写的那样编写额外的代码.但是黑暗的一面是,如果我们想要将消息写入控制台并保存到文件中,我们必须这样做:
# Always set the $InformationPreference variable to "Continue"
$InformationPreference = "Continue";
# if we don't want something like this:
# ======= Example 1 =======
# File Foo.ps1
$InformationPreference = "Continue";
Write-Information "Some …Run Code Online (Sandbox Code Playgroud) 如何从PowerShell写入stderr,或者捕获这样的错误
这些年来,我throw通过错误或写作经历幸存下来Write-Error,但我已经厌倦了,而且在我的脚本中我只想看到一条简洁的错误信息.我一直在努力的每个组合trap,throw,Write-Error,和-ErrorAction,但无济于事:
try {
throw "error" # Sample code for a stack overflow. In the theater
# of your mind, imagine there is code here that does something real and useful
} catch {
Write-Error "An error occurred attempting to 'do something.' Have you tried rebooting?"
}
Run Code Online (Sandbox Code Playgroud)
这是我想看到的用户体验:
C:\> & .\Do-Something.ps1
An error occurred attempting to 'do something.' Have you tried rebooting?
C:\> ?
Run Code Online (Sandbox Code Playgroud)
相反,我得到:
C:\> …Run Code Online (Sandbox Code Playgroud) 为什么Powershell的Write-Errorcmdlet 不能为我工作?我的输出看起来不像文档中的示例:
PS C:\> Write-Error "This is an error"
Write-Error "This is an error" : This is an error
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException
Run Code Online (Sandbox Code Playgroud)
我一直期待输出类似于Write-Warning:
PS H:\> Write-Warning "This is a warning"
WARNING: This is a warning
Run Code Online (Sandbox Code Playgroud)
从Write-Error和about_preference_variables文档我认为我不应该看到任何例外?
PS H:\> Get-Help About_Preference_Variables
$ErrorActionPreference
----------------------
...
PS> $erroractionpreference
Continue # Display the value of the preference.
PS> write-error "Hello, World"
# Generate a non-terminating error.
write-error "Hello, …Run Code Online (Sandbox Code Playgroud) 我试图弄清楚是什么决定了是否从 PowerShell 函数返回一个值,并且我遇到了一些奇怪的情况。about_return 文档说:
在 PowerShell 中,每个语句的结果都会作为输出返回,即使没有包含 Return 关键字的语句也是如此。
但这似乎掩盖了细节。如果我运行这个:
function My-Function {
1
[System.Console]::WriteLine("Hello")
$null
$true
$false
0
2
}
Run Code Online (Sandbox Code Playgroud)
运行此命令将返回一个数组(并打印“Hello”):
1
True
False
0
2
Run Code Online (Sandbox Code Playgroud)
这意味着它$null不会自动返回。然后我尝试递增,因为我正在函数中使用它:
function My-Function {
$n = 1
$n
$n++
($n++)
-join @(1, 2, 3)
(-join @(1, 2, 3))
}
Run Code Online (Sandbox Code Playgroud)
返回:
1
2
123
123
Run Code Online (Sandbox Code Playgroud)
所以,$n被$n++退回了,但($n++)不是吗?但与另一个运算符 ( -join) 相比,每种情况都是相同的。为什么将括号括起来$n++会阻止它被返回,并且为什么其他运算符没有相同的行为?这更加令人困惑,因为=运算符似乎以相反的方式工作:
function My-Function {
($n = 1)
$n
$n++
($n++)
} …Run Code Online (Sandbox Code Playgroud) 这个自我回答的问题旨在系统地概述 PowerShell CLI(命令行界面),适用于Windows PowerShell ( powershell.exe) 和PowerShell (Core) v6+(pwsh.exe在 Windows 上,pwsh在 Unix 上)。
尽管存在官方帮助主题(请参阅答案中的链接),但它们并没有描绘出完整的画面并且缺乏系统的处理(截至撰写本文时)。
其中,回答了以下问题:
特定于版本的 CLI 有何不同?
如何将要执行的 PowerShell 代码传递给 CLI?怎么办-Command(-c)和-File(-f)有什么区别?
传递给这些参数的参数需要如何引用和转义?
哪些字符编码问题会起作用?
PowerShell CLI 如何处理 stdin 输入,它们生成什么 stdout/stderr 以及以什么格式?
powershell quoting command-line-interface character-encoding io-redirection
我有一个通过自动化运行的 PowerShell 脚本,所以我需要将脚本的输出捕获到一个文件中,但我还想捕获运行的命令,为输出提供一些上下文(我会set -x在Linux shell 脚本)。我不知道如何将这些命令捕获到 Windows 中的输出文件中;任何帮助深表感谢!
# script.ps1
Set-PSDebug -Trace 1
Write-Host "Hello, World!"
Run Code Online (Sandbox Code Playgroud)
我如何调用脚本(从 cmd):
$ powershell -command "./script.ps1 *>&1 | Tee-Object -FilePath ./output.txt"
Run Code Online (Sandbox Code Playgroud)
终端输出:
DEBUG: 2+ >>>> Write-Host "Hello, World!"
Hello, World!
Run Code Online (Sandbox Code Playgroud)
output.txt 文件内容:
Hello, World!
Run Code Online (Sandbox Code Playgroud)
您可以看到输出文件缺少终端中显示的调试行。理想情况下,我希望调试行只在输出文件中(不是终端输出),但在两个地方都有调试行也可以。
请注意,PowerShell 5.1 是我安装的版本。