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:pathTomyZip.zip"
$dst = "c:destination"
saps "c:Program Fileswinzipwzunzip.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.
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'. The argument is null, empty, or an element of the argument collection contains a null value. Supply a collection that does not contain any null values and then try the command again. At c:pathtomodulemyModule.psm1:39 char:38 + saps -FilePath $cmd -ArgumentList <<<< $args -WindowStyle Hidden -Wait + CategoryInfo : InvalidData: (:) [Start-Process], 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. Here is the very basic example:
function Unzip-File ($ZipFile, $Destination)
{
$wzunzip = 'c:Program Fileswinzipwzunzip.exe'
Start-Process -WindowStyle Hidden -Wait -FilePath $wzunzip -ArgumentList (
'-d',
$ZipFile,
$Destination
)
}
Unzip-File 'c:pathTomyZip.zip' 'c:destination'
Update:
is there a way to pass the exe file to the function as well? I'll eventually have multiple exe files coming into the function that logs the command and then executes it.
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:pathtofile.exe' '-arg1', '-arg2', '-arg3'
链接地址: http://www.djcxy.com/p/37882.html
下一篇: Powershell中模块功能的参数