【发布时间】:2014-02-26 17:55:23
【问题描述】:
受post的启发,我在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
但是,在执行时它会返回此错误:
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
我已经搜索过这个错误,但无法弄清楚。在我看来,它无法正确转换字符串类型!请帮忙!
【问题讨论】:
-
在要作为参数传递的字符串后面有一个逗号
$Cmd。 -
如果你使用的是PS V3,你可以使用转义符
--%