Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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
PowerShell函数中的第一个参数丢失其值?_Powershell - Fatal编程技术网

PowerShell函数中的第一个参数丢失其值?

PowerShell函数中的第一个参数丢失其值?,powershell,Powershell,这里有一个我用\GetEMSInstallers调用的函数。由于某些未知原因,第一个参数总是丢失其值: function Get-EMSInstallers { param ( $ems_for_amx_source = '\\server\ems_path', $installers_dir = 'D:\installers' ) process { if (!(Test-Path "$installers_dir\EMS4AMX")) { "Co

这里有一个我用
\GetEMSInstallers
调用的函数。由于某些未知原因,第一个参数总是丢失其值:

function Get-EMSInstallers {

param (
    $ems_for_amx_source = '\\server\ems_path',
    $installers_dir = 'D:\installers'
)

process {

    if (!(Test-Path "$installers_dir\EMS4AMX")) {
        "Copying files and folders from $ems_for_amx_source to $installers_dir\EMS4AMX"
        copy $ems_for_amx_source "$installers_dir\EMS4AMX" -rec -force
    }
}

}
Get-EMSInstallers $args
当我调用它时,我得到以下输出:

Copying files and folders from  to D:\installers\EMS4AMX
Copy-Item : Cannot bind argument to parameter 'Path' because it is an empty array.
At C:\Users\ad_ctjares\Desktop\Scripts\Ems\GetEMSInstallers.ps1:12 char:17
+             copy <<<<  $ems_for_amx_source "$installers_dir\EMS4AMX" -rec -force
    + CategoryInfo          : InvalidData: (:) [Copy-Item], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyArrayNotAllowed,Microsoft.PowerShell.Commands.CopyI
   temCommand
将文件和文件夹从复制到D:\installers\EMS4AMX
复制项:无法将参数绑定到参数“Path”,因为它是空数组。
在C:\Users\ad\u ctjares\Desktop\Scripts\Ems\GetEMSInstallers.ps1:12 char:17

+copy当您没有传入任何参数来获取EMSInstallers时,您仍然有一个$args数组-它只是空的。因此,$ems_for_amx_源参数被设置为这个空数组

换言之,解决这个问题的一个方法是:

if ($args)
{
  Get-EMSInstallers $args
}
else
{
  Get-EMSInstallers
}
可能有一种更强大的方法可以做到这一点-如果我想到的话,我可能会在以后修改它。:-)但这会让你无论如何开始。

你可以使用将数组中的所有值传递给函数,而不是将数组作为单个参数传递:
get-EMSInstallers@args
(或者使用OP的其他问题和:)的答案。