Ne peut pas argument de processus de transformation

Inspiré par cette post, j'ai créé le script ci-dessous DOSCommands.ps1

Function Invoke-DOSCommands {
    Param(
        [Parameter(Position=0,Mandatory=$true)]
        [String]$cmd,
        [String]$tmpname = $(([string](Get-Random -Minimum 10000 -Maximum 99999999)) + ".cmd"),
        [switch]$tmpdir = $true)
    if ($tmpdir) {
        $cmdpath = $(Join-Path -Path $env:TEMP -ChildPath $tmpname);
    }
    else {
        $cmdpath = ".\" + $tmpname
    }
    Write-Debug "tmpfile: " + $cmdpath
    Write-Debug "cmd: " + $cmd
    echo $cmd | Out-File -FilePath $cmdpath -Encoding ascii;
    & cmd.exe /c $cmdpath | Out-Null
}

Invoke-DOSCommands "Echo ""Hello World""", -tmpdir $false

Cependant, en cours d'exécution, il retourne cette erreur:

Invoke-DOSCommands : Cannot process argument transformation on parameter 'cmd'. Cannot convert value to type S ystem.String.
At DOSCommands.ps1:20 char:19
+ Invoke-DOSCommands <<<<  "Echo ""Hello World""", -tmpdir $false
    + CategoryInfo          : InvalidData: (:) [Invoke-DOSCommands], ParameterBindin...mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Invoke-DOSCommands

J'ai cherché pour cette erreur, mais ne peut pas le comprendre. Il me semble qu'il ne peut pas convertir le type de chaîne correctement! S'il vous plaît aider!

Vous avez une virgule après la chaîne que vous souhaitez passer comme paramètre $Cmd.
si vous utilisez PS V3, vous pouvez utiliser l'échappement sting --%

OriginalL'auteur Vijay | 2014-02-03