如何在命令文件中的-file中使用“。\”

问题描述 投票:0回答:2

我需要从机器A上的Powershell脚本(A.ps1)在机器B上执行命令文件(B.cmd)。我不想静态地指定路径

B.cmd应该执行C.ps1,它与B.cmd在同一个文件夹中

机器A:A.ps1 机器B:B.cmd,C.ps1(全部在同一个文件夹中)

所以我的命令文件看起来像这个B.cmd

@echo off
powershell.exe -file ".\C.ps1" -Iterations 10
echo
echo
pause

我正在调用B.cmd文件的A.ps1文件中抛出一个错误

A.ps1

Invoke-Command -ComputerName $systemName -credential $credentials -ScriptBlock {Invoke-Expression -Command: "cmd.exe /c C:\Temp\Batch\Test\B.cmd"}

A.ps1抛出错误:

**The argument '.\C.ps1' to the -File parameter does not exist. Provide the path to an existing '.ps1' file as an argument to the -File parameter.**
+ CategoryInfo          : NotSpecified: (The argument '....File parameter.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName        : XXX

如何使用。\ work或者有替代方法或使用。\进行远程执行是否错误?

请原谅我的无知,因为我对PS非常新,谢谢!

powershell remote-access
2个回答
1
投票

如果您能够更改ABC.cmd,则可以使用以下内容

powershell.exe -file "%~dp0\XYZ.ps1"

这将获得运行ABC.cmd脚本的文件夹。请注意,%~dp0无法在cmd中进行测试,您需要在脚本中对其进行测试。


0
投票

所以不要在它周围包装批处理文件。浪费时间并混淆问题。

invoke-command -comp $computername -filepath C:\Temp\Batch\Test\XYZ.ps1 

如果您需要运行10次,请在PS脚本中循环执行。

© www.soinside.com 2019 - 2024. All rights reserved.