从64位PowerShell调用32位命令行

时间:2016-03-17 13:39:59

标签: powershell batch-file

我需要从32位命令行调用一堆批处理文件。

我有这段代码:

$cmd = "C:\Windows\SysWOW64\cmd.exe"
$args="/C"
$pipe = "0 |"
Start-Process $cmd $args

如果我只调用$ cmd,则会打开命令行窗口。标题显示" C:\ windows \ syswow64 \ cmd.exe",但窗口中的路径显示" C:\ windows \ system32"。

如何强制PowerShell运行所有批处理文件以在32位系统中运行?另外,我如何使用$ pipe,因为某些批处理文件需要"按任意键继续"?

我能够使用下面的代码运行32位命令行。

$CMD = "C:\Windows\SysWOW64\cmd.exe"
$test = 'C:\Update\test folder\test.cmd'
$proc = (Invoke-WmiMethod Win32_Process Create "$cmd /c $test")

但是,因为文件夹名称有空格,所以上面的代码不起作用。如果我删除文件夹名称中的空格,一切正常。

我尝试使用双引号,单引号,$a = $test.tostring()但没有成功。 我该如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

正在运行32位可执行文件。您可以通过显示图像类型列(查看→显示列→过程图像)来验证此例如Process Explorer。 CMD窗口中显示的路径只是工作目录,与进程是32位还是64位无关。

答案 1 :(得分:0)

我最终找到了下面的代码,迫使它以32位调用cmd。

$NewProcInfo = New-Object System.Diagnostics.ProcessStartInfo
$NewProcInfo.FileName = "$CMD"
$NewProcInfo.RedirectStandardError = $true
$NewProcInfo.RedirectStandardOutput = $true
$NewProcInfo.UseShellExecute = $false
$Path = "`"$batchfile`""
$args = @("/c", "$Path")
$NewProcInfo.Arguments = "$args"
$Proc = New-Object System.Diagnostics.Process
$Proc.StartInfo = $NewProcInfo
$Proc.Start() | Out-Null
$Proc.WaitForExit()
$ReturnCode = $Proc.ExitCode

答案 2 :(得分:0)

您的初始解决方案有效。你刚刚犯了经典思维错误。

您从cmd.exe启动32位C:\Windows\SysWOW64

DOS框当然会显示您当前的目录C:\Windows\System32。它是一个32位应用程序。 64位Windows上的32位应用程序中的C:\Windows\System32实际上是目录C:\Windows\SysWOW64

您刚刚被锁定在DOS框内的32位环境中。