在接近疯狂的一天之后,为什么我的脚本不能处理客户提供的数据,我想通了,是我的命令中用双引号和单引号括起来的某个地方的“编码”这个词阻止了 Wscript。 Shell 从打开 powershell(其中命令按预期工作)。德语的特征是通过添加“n”来附加“Marke”和“Code”等单词,因此“Markencode”允许包含“encode”的大量单词。 :/
我创建了一些最小的 vba 示例来显示问题。有谁知道解决方法?
Option Explicit
Const HK = """"
Sub ScanDrive()
Dim s As String, command As String
Dim oExec As Object
command = "echo 'encode'"
Debug.Print (command)
Set oExec = CreateObject("Wscript.Shell").Exec("powershell.exe -command " & HK & command & HK)
Do While oExec.Status = 0
Application.Wait (Now + TimeValue("0:00:01"))
Loop
Debug.Print (oExec.ExitCode)
s = oExec.StdOut.ReadAll
Debug.Print (s)
End Sub
输出为(Excel 中的 VBA 版本 7.1.1108):echo 'encode'
-536870873
不幸的是,我找不到该退出代码的任何内容。顺便说一句......放入“解码”而不是“编码”工作正常。
最佳答案
由于这似乎不是一般问题,因此我通过在“en”和“code”之间拆分字符串来解决它,然后让 powershell 将其放回去。
'...
pos = InStr(fileName, "encode")
If pos > 0 Then
Dim l, r As String
l = Left(fileName, pos + 1) & "'"
r = "'" & Right(fileName, Len(fileName) - (pos + 1))
'Split filename after "en" and before "code"
fileName = "-join(" & l & " , " & r & " )"
Debug.Print "Had to split the filename: " & fileName
End If
'...
也许这对某人有帮助:)
关于vba - Wscript.Shell Exec 中的单词 "encode"确实会阻止 powershell 脚本运行。我能做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68774324/