Powershell:为什么写入输出和写入警告对字符串和变量的处理方式不同?(Powershell: Why does write

编程入门 行业动态 更新时间:2024-10-26 00:24:23
Powershell:为什么写入输出和写入警告对字符串和变量的处理方式不同?(Powershell: Why does write-output and write-warning treat strings and variables differently?)

我对PowerShell比较陌生,而且我发现一个奇怪的地方,我不确定如何解释。

这个输出很好。

write-output $var.Name.padright(40)" - "(get-date -format s)" : Creating on $var1"

这在另一方面,抛出一个错误:

write-warning $var.Name.padright(40)" - "(get-date -format s)" : Creating on $var1" Write-Warning : A positional parameter cannot be found that accepts argument ' - '. At Y:\test.ps1:228 char:22 + write-warning <<<< $var.name.padright(40)" - "(get-date -format s)" : Creating on $var1" + CategoryInfo : InvalidArgument: (:) [Write-Warning], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.WriteWarningCommand

任何想法为什么? 如果我想做出上述工作,我会这样做:

$warning = $var.Name.padright(40) + " - " + (get-date -format s) + " : Creating on $var1" write-warning $warning

正如你所看到的,我发现了一种解决方法,但我想知道为什么字符串连接对写输出的工作方式不同于写警告。 是否有一个原因,我应该首先显式创建字符串作为变量,而不仅仅是使用简写?

I'm relatively new to powershell, and I have found an oddity that I'm not sure how to explain.

This outputs just fine.

write-output $var.Name.padright(40)" - "(get-date -format s)" : Creating on $var1"

This on the other hand, throws an error:

write-warning $var.Name.padright(40)" - "(get-date -format s)" : Creating on $var1" Write-Warning : A positional parameter cannot be found that accepts argument ' - '. At Y:\test.ps1:228 char:22 + write-warning <<<< $var.name.padright(40)" - "(get-date -format s)" : Creating on $var1" + CategoryInfo : InvalidArgument: (:) [Write-Warning], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.WriteWarningCommand

Any idea why? If I want to make the above work, I instead do this:

$warning = $var.Name.padright(40) + " - " + (get-date -format s) + " : Creating on $var1" write-warning $warning

I've found a workaround, as you can see, but I'd like to understand why string concatenation works differently for write-output than write-warning. Is there a reason I should explicitly create the string as a variable first rather than just using the shorthand?

最满意答案

这不是关于如何创建字符串,而是关于解释器正在寻找什么。 写输出有一个名为inputobject的参数,它接受一个或多个PSobjects。 写警告正在寻找单个字符串对象。

您使用的语法并不理想,当您使用cmdlet时,请尽量准确地发送给每个参数。

如果不清楚,以下是实际使用的代码:

Write-Warning -Message "$($var.Name.padright(40)) - $(get-date -format s) : Creating on $var1";

该语法创建一个单一的字符串对象,将其传递到Write-Warning命令的-Message参数中,这正是它期望的第一个位置参数。 您还可以指定参数名称(参见上面的示例),以确保代码可被其他人阅读,并且解释者完全理解您的意图。

It's not about how the string is created it's about what the interpreter is looking for. Write-output has a parameter called inputobject that accepts one or more PSobjects. Write-warning is looking for a single string object.

The syntax you're using isn't ideal, when you're using cmdlets try and be really precise with what you send to each parameter.

Here's the actual Code to use in case that wasn't clear:

Write-Warning -Message "$($var.Name.padright(40)) - $(get-date -format s) : Creating on $var1";

That syntax creates a single string object to pass into the -Message parameter on the Write-Warning command, which is what it's expecting as the first positional parameter. You can also specify the parameter name (see above example), to ensure that the code is readable by other people, and that the interpreter fully understands your intentions.

更多推荐

本文发布于:2023-08-01 14:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1359321.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   变量   方式   Powershell   write

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!