Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function 为什么powershell无法识别多个参数?_Function_Powershell - Fatal编程技术网

Function 为什么powershell无法识别多个参数?

Function 为什么powershell无法识别多个参数?,function,powershell,Function,Powershell,我正在尝试编写一个包含3个参数的powershell函数 Function CopyVHD ($filename, $sourcevhd, $destination) { echo "filename is $filename" echo "source is $sourcevhd" echo "destination is $destination" # Validate that the VHD doesn't exist on remote if (

我正在尝试编写一个包含3个参数的powershell函数

Function CopyVHD ($filename, $sourcevhd, $destination)
{
    echo "filename is $filename"
    echo "source is $sourcevhd"
    echo "destination is $destination"
    # Validate that the VHD doesn't exist on remote
    if ( Test-Path "$sourcevhd\$filename" )
    {
        echo "File $filename already exists at $destination"
    }
    else 
    {
      echo "copying $sourcevhd\\$filename to $destination"
      Copy-Item $sourcevhd\$filename "$destination" -Recurse
    }

}
CopyVHD("foo.vhd","c:\","d:\")
PS C:\Windows\system32> C:\Users\sowen\Documents\closed-pile.ps1
filename is foo.vhd C:\ D:\
source is 
destination is 
然后我传入3个参数

Function CopyVHD ($filename, $sourcevhd, $destination)
{
    echo "filename is $filename"
    echo "source is $sourcevhd"
    echo "destination is $destination"
    # Validate that the VHD doesn't exist on remote
    if ( Test-Path "$sourcevhd\$filename" )
    {
        echo "File $filename already exists at $destination"
    }
    else 
    {
      echo "copying $sourcevhd\\$filename to $destination"
      Copy-Item $sourcevhd\$filename "$destination" -Recurse
    }

}
CopyVHD("foo.vhd","c:\","d:\")
PS C:\Windows\system32> C:\Users\sowen\Documents\closed-pile.ps1
filename is foo.vhd C:\ D:\
source is 
destination is 
为什么powershell将3个参数合并为1个?如果在下面的输出中注意到,变量
filename
已使用了所有3个参数,而参数
目标
为空

PS C:\Windows\system32> C:\Users\example\Documents\closed-pile.ps1
newest vhd is foo.vhd
filename is foo.vhd C:\ D:\
source is 
destination is 
copying \\foo.vhd C:\ D:\ to 
Copy-Item : Cannot find drive. A drive with the name '\foo.vhd C' does not exist.
At C:\Users\example\Documents\closed-pile.ps1:28 char:7
+       Copy-Item $sourcevhd\$filename "$destination" -Recurse
+       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (\foo.vhd C:String) [Copy-Item], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
我尝试了不同的变量名,以防我错误地使用保留字。 我尝试了不同的空间语法

CopyVHD ("foo.vhd","c:\","d:\")
CopyVHD("foo.vhd","c:\","d:\")
CopyVHD("foo.vhd", "c:\", "d:\")
CopyVHD( "foo.vhd", "c:\", "d:\" )
我在同一脚本中有另一个函数,它正确地接受2个参数。为什么这个函数不起作用

更新

我尝试了下面的语法,如链接的SO问题所示。它给出了同样的失败

CopyVHD "foo.vhd", "C:\", "D:\"
更新2

也尝试了单引号

CopyVHD 'foo.vhd', 'C:\', 'D:\'
函数仍然无法识别3个参数

Function CopyVHD ($filename, $sourcevhd, $destination)
{
    echo "filename is $filename"
    echo "source is $sourcevhd"
    echo "destination is $destination"
    # Validate that the VHD doesn't exist on remote
    if ( Test-Path "$sourcevhd\$filename" )
    {
        echo "File $filename already exists at $destination"
    }
    else 
    {
      echo "copying $sourcevhd\\$filename to $destination"
      Copy-Item $sourcevhd\$filename "$destination" -Recurse
    }

}
CopyVHD("foo.vhd","c:\","d:\")
PS C:\Windows\system32> C:\Users\sowen\Documents\closed-pile.ps1
filename is foo.vhd C:\ D:\
source is 
destination is 

在PowerShell中不能用逗号分隔参数,逗号用于显示一个参数的多个条目

调用此函数的方法是用空格分隔参数。或者更好的是,提供参数名

CopyVHD A B C

CopyVHD -filename A -sourcevhd B -destination C

>filename is a
source is b
destination is c 

在PowerShell中不能用逗号分隔参数,逗号用于显示一个参数的多个条目

调用此函数的方法是用空格分隔参数。或者更好的是,提供参数名

CopyVHD A B C

CopyVHD -filename A -sourcevhd B -destination C

>filename is a
source is b
destination is c 

这不是调用powershell函数的方式。您需要
CopyVHD“arg1”、“arg2”、“arg3”
。我意识到我的复制标志无效,因为我链接到的问题是关于VS构建事件的。不过,这里有一个答案可以解决您的问题。您在powershell中调用函数的方式与在VB中调用函数的方式不同。FoxDeploy调用powershell函数的回答是正确的。这不是调用powershell函数的方式。您需要
CopyVHD“arg1”、“arg2”、“arg3”
。我意识到我的复制标志无效,因为我链接到的问题是关于VS构建事件的。不过,这里有一个答案可以解决您的问题。您在powershell中调用函数的方式与在VB中调用函数的方式不同。FoxDeploy调用powershell函数的回答是正确的。