Powershell按顺序打印变量

Powershell按顺序打印变量,powershell,parameters,Powershell,Parameters,我是powershell的新手,在尝试将参数传递到函数时,我发现了一个非常奇怪的行为: 代码 问题 以下是我调用脚本时使用的参数: .\boxMove.ps1 -vhdSourceDir C:\BuildAgent\work -vhdDestinationDir e:\ -hypervisorList 'foobar' 这是它生成的输出 THEBUILD is 527c6ddeb8776914 looking for build at C:\BuildAgent\work 527c6ddeb

我是powershell的新手,在尝试将参数传递到函数时,我发现了一个非常奇怪的行为:

代码

问题

以下是我调用脚本时使用的参数:

.\boxMove.ps1 -vhdSourceDir C:\BuildAgent\work -vhdDestinationDir e:\ -hypervisorList 'foobar'
这是它生成的输出

THEBUILD is 527c6ddeb8776914
looking for build  at C:\BuildAgent\work 527c6ddeb8776914 
the dir is C:\BuildAgent\work 527c6ddeb8776914 and the build is 
Get-ChildItem : Cannot find path 'C:\BuildAgent\work 527c6ddeb8776914\packer\windows\box\hyperv\' because it does not exist.
这些参数显示不正常。例如,短语“查找构建”应如此显示

但它显示为

looking for build  at C:\BuildAgent\work 527c6ddeb8776914
短语“目录是…”也应改为

the dir is C:\BuildAgent\work and the build is 527c6ddeb8776914 
但它的意思是

the dir is C:\BuildAgent\work 527c6ddeb8776914 and the build is

powershell为什么不按顺序打印字符串?

首先,清理函数定义。而不是

function (params){code}
你应该使用

function
{
Param ($Param1,$param2)
code
}
另外,当接受数组作为参数时,不要使用[array],而是指定数组类型(在您的例子中可能是字符串),这样可以是(第4行)


很难说脚本失败的确切原因,但我首先要解决明显的问题,然后再次测试。

当您将参数传递给函数时,不要将它们封装在括号中,如果传递了多个参数,则它们不应以逗号分隔,而应使用空格分隔。

例如:

findBoxFile($vhdSourceDir,$THEBUILD) 
应改为:

findBoxFile $vhdSourceDir $THEBUILD

然后,您会发现这将消除您在输出顺序不正确时遇到的问题。

首先:不要使用括号调用PowerShell函数。
[Parameter(Mandatory=$true)][string[]]$hypervisorList,
findBoxFile($vhdSourceDir,$THEBUILD) 
findBoxFile $vhdSourceDir $THEBUILD