我对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.WriteWarningCommandAny 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 $warningI'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.
更多推荐
发布评论