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.
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:\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
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"