在PowerShell中将多个参数放入一个变量中 [英] Multiple parameters put into one variable in PowerShell

查看:32
本文介绍了在PowerShell中将多个参数放入一个变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 2 个参数传递给调用 invoke-command 的 PowerShell 脚本,并且我正在尝试传递多个参数.但是当我这样做时,似乎两个参数都被放入了一个变量中,我想知道为什么.

I am trying to pass in 2 argument to a PowerShell script which calls invoke-command and I'm trying to pass in multiple parameters. But when I do, it seems like both parameters are put into a single variable and I'm wondering why.

这是两个程序的代码:

POC.ps1:

param($filename, $user)

echo $filename
echo "This"
echo $user

$responseObject = Invoke-Command CAPTESTPK01 -FilePath .\validatePath.ps1  -ArgumentList($filename, $user) -AsJob 




while($responseObject.State -ne "Completed")
{

}

$result = Receive-Job -Id $responseObject.Id -Keep
echo $result

validatPath.ps1:

validatPath.ps1:

Param([string] $filename,
      [string] $user)

function ValidatePath( $filename, $user, $fileType = "container" )
{
    Write-Host "This is the file name: $filename"
    echo "This is user: $user"
    $fileExist = $null
    if( -not (test-path $filename -PathType $fileType) )
    {               
        throw "$user, the path $filename does not exist!"

    }
    else
    {
         Write-Host "This is the second part"
         echo $filename found!
    }
     Write-Host "This is the third part"
    return $fileExist
}


try
{

    ValidatePath($filename, $user)
}
catch
{
    $e = $_.Exception
    echo $e
}

输出如下:

C:\Users
This
Blaine
This is the file name: C:\Users Blaine <--- This indicated both arguments are in one variable
This is user: 
This is the second part
This is the third part
C:\Users
Blaine

推荐答案

PowerShell 函数的参数在调用时不应放在括号中.它应该是 ValidatePath $filename $user.您编写的内容导致仅使用一个参数调用 ValidatePath,而 $filename 是一个包含两个值的数组,即文件名和用户.

Arguments to PowerShell functions should not be put in parenthesis when called. It should be ValidatePath $filename $user. What you have written results in calling ValidatePath with just one parameter and $filename being an array of two values, namely the filename and the user.

顺便说一句:在 PowerShell 中调用 .Net 方法时,您确实需要括号.:)

BTW: When calling .Net methods in PowerShell you do need the parenthesis. :)

这篇关于在PowerShell中将多个参数放入一个变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆