简体   繁体   English

Powershell中模块功能的参数

[英]Parameters to a function of a module in Powershell

I was able to execute this command before trying to make a function out of it.. 在尝试使用该命令之前,我能够执行此命令。

$unzip ="c:\path\To\myZip.zip"
$dst = "c:\destination"
saps "c:\Program Files\winzip\wzunzip.exe" "-d $unzip $dst" -WindowStyle Hidden -Wait

Then I created this function in a module that I am trying to pass parameters to.. 然后,我在试图将参数传递给的模块中创建了此函数。

function RunCmd ($cmd){
    write-host "cmd: $cmd" 
    saps $cmd -WindowStyle Hidden -Wait 
}

I have verified that the module has been imported correctly, but when I try and pass the parameters to the function I get errors stating that the argument cannot be read. 我已验证模块已正确导入,但是当我尝试将参数传递给函数时,出现错误,指出无法读取参数。

I've tried multiple ways to pass the parameters, but nothing works. 我尝试了多种方法来传递参数,但是没有任何效果。

Examples 例子

$cmd = @{'FilePath'= '$unzip';
     'ArgumentList'= '-d $unzip dst';}
RunCmd  @cmd


RunCmd """$unzip"" ""-d $unzip $dst"""

I have noticed that the command and arguments will be passed to the function in double quotes doing the second alternative, but that's when I get the arguments null exception. 我已经注意到,命令和参数将以双引号传递给函数,这是第二种选择,但是那是当我收到参数null异常时。

I have also tried to change the function to pass the command and arguments separately without success.. 我也尝试过更改函数以分别传递命令和参数而没有成功。

function RunCmd ($cmd, $args){
    write-host "cmd: $cmd" 
    saps $cmd $args -WindowStyle Hidden -Wait 
}

Any ideas? 有任何想法吗?

UPDATE: 更新:

this is my new function.. 这是我的新功能。

function RunCmd ($log, $cmd, $args){
    Log-Cmd $log
    saps -FilePath $cmd -ArgumentList $args -WindowStyle Hidden -Wait 
}

have also tried.. 也尝试过..

> function RunCmd ($log, $cmd, [string[]]$args){
>     Log-Cmd $log
>     saps -FilePath $cmd -ArgumentList $args -WindowStyle Hidden -Wait  }

but when the function tries to execute I get an error saying that the arguments are null. 但是当函数尝试执行时,我收到一条错误消息,指出参数为空。

Start-Process : Cannot validate argument on parameter 'ArgumentList'. 开始过程:无法验证参数'ArgumentList'上的参数。 The argument is null, empty, or an element of the argument collection contains a null value. 参数为null,为空,或者参数集合的元素包含空值。 Supply a collection that does not contain any null values and then try the command again. 提供不包含任何空值的集合,然后重试该命令。 At c:\\path\\to\\module\\myModule.psm1:39 char:38 + saps -FilePath $cmd -ArgumentList <<<< $args -WindowStyle Hidden -Wait + CategoryInfo : InvalidData: (:) [Start-Process], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.StartProcessCommand 在c:\\ path \\ to \\ module \\ myModule.psm1:39 char:38 + saps -FilePath $ cmd -ArgumentList <<<< $ args -WindowStyle隐藏-Wait + CategoryInfo:InvalidData:(:) [开始处理] ,ParameterBindingValidationException + FullyQualifiedErrorId:ParameterArgumentValidationError,Microsoft.PowerShell.Commands.StartProcessCommand

I have tried multiple ways to call this function.. 我尝试了多种方法来调用此函数。

RunCmd -log $log -cmd $unzip -args '-d', '$unzip', '$dst'
RunCmd $log $unzip '-d', '$unzip', '$dst'
RunCmd $log $unzip "-d", "$unzip", "$dst"

You have to pass arguments to the Start-Process cmdlet as array of strings. 您必须将参数作为字符串数组传递给Start-Process cmdlet。 Here is the very basic example: 这是非常基本的示例:

function Unzip-File ($ZipFile, $Destination)
{
    $wzunzip = 'c:\Program Files\winzip\wzunzip.exe'
    Start-Process -WindowStyle Hidden -Wait -FilePath $wzunzip -ArgumentList (
        '-d',
        $ZipFile,
        $Destination
    ) 
}

Unzip-File 'c:\path\To\myZip.zip' 'c:\destination'

Update: 更新:

is there a way to pass the exe file to the function as well? 有没有办法将exe文件传递给函数? I'll eventually have multiple exe files coming into the function that logs the command and then executes it. 我最终将有多个exe文件进入记录命令然后执行的功能。

Sure: 当然:

function Start-ProcAndLog ($ExeFile, $CmdLine)
{
    Start-Process -WindowStyle Hidden -Wait -FilePath $ExeFile -ArgumentList $CmdLine
}

# Note commas in second parameter: '-arg1', '-arg2', '-arg3' is an array
Start-ProcAndLog 'c:\path\to\file.exe' '-arg1', '-arg2', '-arg3'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM