Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PowerShell不会在另一台计算机上运行批处理文件_Powershell_Batch File - Fatal编程技术网

PowerShell不会在另一台计算机上运行批处理文件

PowerShell不会在另一台计算机上运行批处理文件,powershell,batch-file,Powershell,Batch File,我有一个位于VM上的PowerShell脚本。该脚本创建一个PSSession,用于运行物理机器上的批处理文件。但是,当我运行脚本时,什么也没有发生 PowerShell: $Username = "Domain\User" $Password = ConvertTo-SecureString "Password*" -AsPlainText -Force $cred = New-Object System.Management.Automation.PSCredential ($Usernam

我有一个位于VM上的PowerShell脚本。该脚本创建一个PSSession,用于运行物理机器上的批处理文件。但是,当我运行脚本时,什么也没有发生

PowerShell:

$Username = "Domain\User"
$Password = ConvertTo-SecureString "Password*" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($Username, $password)
$session = New-PSSession -ComputerName "K2" -Credential $cred

Enter-PSSession -Session $session

Invoke-Command -ComputerName "K2" -ScriptBlock {
    Invoke-Expression -Command: "C:\Windows\system32\cmd.exe c/ 'cd C:\Program Files\SmartBear\ReadyAPI-2.4.0\Go4Schools Tests\Test Runner'"
    Invoke-Expression -Command: "C:\Windows\system32\cmd.exe c/ 'START "" /wait PS_TR.bat'"

    #Invoke-Expression -Command: "C:\Windows\system32\cmd.exe Call c/ C:\Program Files\SmartBear\ReadyAPI-2.4.0\Go4Schools Tests\Test Runner\PS_TR.bat"
}

Exit-PSSession
Get-PSSession | Remove-PSSession
PS C:\> C:\Users\User\Desktop\PS_TR.ps1 Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\User\Documents> Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\User\Documents> PS输出:

$Username = "Domain\User"
$Password = ConvertTo-SecureString "Password*" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($Username, $password)
$session = New-PSSession -ComputerName "K2" -Credential $cred

Enter-PSSession -Session $session

Invoke-Command -ComputerName "K2" -ScriptBlock {
    Invoke-Expression -Command: "C:\Windows\system32\cmd.exe c/ 'cd C:\Program Files\SmartBear\ReadyAPI-2.4.0\Go4Schools Tests\Test Runner'"
    Invoke-Expression -Command: "C:\Windows\system32\cmd.exe c/ 'START "" /wait PS_TR.bat'"

    #Invoke-Expression -Command: "C:\Windows\system32\cmd.exe Call c/ C:\Program Files\SmartBear\ReadyAPI-2.4.0\Go4Schools Tests\Test Runner\PS_TR.bat"
}

Exit-PSSession
Get-PSSession | Remove-PSSession
PS C:\> C:\Users\User\Desktop\PS_TR.ps1 Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\User\Documents> Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\User\Documents> 所以我不明白为什么脚本没有运行批处理文件

我已尝试使用上面和下面代码中的行:

  • Invoke Expresson
  • 调用项
  • 启动流程
  • 开始作业
从上面可以看出,我没有得到任何像样的输出,因此我发现很难调试


另外,直接从物理机器运行批处理文件也可以正常工作

不要使用
调用表达式
。不管你做什么工作都可以。另外,如果执行
cmd/c cd…
操作,则仅更改该cmd进程的工作目录。它不会影响您正在启动的下一个CMD进程

更改此项:

Invoke-Command -ComputerName "K2" -ScriptBlock {
    Invoke-Expression -Command: "C:\Windows\system32\cmd.exe c/ 'cd C:\Program Files\SmartBear\ReadyAPI-2.4.0\Go4Schools Tests\Test Runner'"
    Invoke-Expression -Command: "C:\Windows\system32\cmd.exe c/ 'START "" /wait PS_TR.bat'"
}
为此:

Invoke-Command -ComputerName "K2" -ScriptBlock {
    Set-Location 'C:\Program Files\SmartBear\ReadyAPI-2.4.0\Go4Schools Tests\Test Runner'
    & '.\PS_TR.bat'
}

问题就会消失。

不要使用
调用表达式。不管你做什么工作都可以。另外,如果执行
cmd/c cd…
操作,则仅更改该cmd进程的工作目录。它不会影响您正在启动的下一个CMD进程

更改此项:

Invoke-Command -ComputerName "K2" -ScriptBlock {
    Invoke-Expression -Command: "C:\Windows\system32\cmd.exe c/ 'cd C:\Program Files\SmartBear\ReadyAPI-2.4.0\Go4Schools Tests\Test Runner'"
    Invoke-Expression -Command: "C:\Windows\system32\cmd.exe c/ 'START "" /wait PS_TR.bat'"
}
为此:

Invoke-Command -ComputerName "K2" -ScriptBlock {
    Set-Location 'C:\Program Files\SmartBear\ReadyAPI-2.4.0\Go4Schools Tests\Test Runner'
    & '.\PS_TR.bat'
}

问题将消失。

由此我得到以下错误
术语“PSTR.bat”不被识别为cmdlet、函数、脚本文件或可操作程序的名称。
@Ross My bad。PowerShell在路径中不包含当前工作目录,因此它必须是
&.\PS\u TR.bat'
。更正了我的答案。谢谢,这似乎奏效了。但这将引导我进入另一个我将要调查的问题。非常感谢您的帮助。由此我得到以下错误
术语“PSTR.bat”无法识别为cmdlet、函数、脚本文件或可操作程序的名称。
@Ross My bad。PowerShell在路径中不包含当前工作目录,因此它必须是
&.\PS\u TR.bat'
。更正了我的答案。谢谢,这似乎奏效了。但这将引导我进入另一个我将要调查的问题。非常感谢你的帮助。