将.vbs中的字符串传递给Powershell会导致“找不到位置参数错误”?

时间:2014-07-03 11:05:41

标签: vba powershell

我有一个小的.vbs Windows窗体,它接受一个字符串(计算机名称)。所有脚本都用它来组装然后运行Powershell命令来删除该特定机器上的文件。这是脚本:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim compStr As String = TextBox1.Text
    Dim baseStr As String = "-noexit get-childItem " & Chr(34) & "\\" & compStr & "\c$\ProgramData\Sophos\Remote Management System\3\Agent\AdapterStorage\ALC" & Chr(34) & " | where {$_.length -eq 0} | remove-Item"
    MsgBox(baseStr)
    Process.Start("powershell", baseStr)
    Me.Close()

End Sub

麻烦的是,它引发了这个错误:

Get-ChildItem : A positional parameter cannot be found that accepts argument 'S
ystem\3\Agent\AdapterStorage\ALC'.
At line:1 char:14
+ get-childItem <<<<  \\P94\c$\ProgramData\Sophos\Remote Management System\3\Ag
ent\AdapterStorage\ALC | where {$_.length -eq 0} | remove-Item
+ CategoryInfo          : InvalidArgument: (:) [Get-ChildItem], ParameterB
indingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell
.Commands.GetChildItemCommand

但是,如果我通过蓝色命令shell自己运行Powershell命令,它可以完美地运行。看起来它与文件路径中的空格有关,但我无法理解它,因为.vbs输出的字符串看起来完全正确。

1 个答案:

答案 0 :(得分:0)

替换单身双引号 - 将Chr(34)更改为Chr(39)