从批处理文件调用PowerShell脚本以连接远程计算机不起作用

从批处理文件调用PowerShell脚本以连接远程计算机不起作用,powershell,batch-file,parameter-passing,quoting,Powershell,Batch File,Parameter Passing,Quoting,当我正常运行PowerShell脚本时,它工作正常,当从批处理文件调用同一脚本时,会出现问题 Unt1.ps1脚本: $linux\u app\u user=“ORXXXX\”+$args[0] $pass\u win=$args[1] $path=$args[2] $pass=ConvertTo SecureString-AsPlainText$pass\u win-Force $cred=新对象System.Management.Automation.PSCredential-Argumen

当我正常运行PowerShell脚本时,它工作正常,当从批处理文件调用同一脚本时,会出现问题

Unt1.ps1
脚本:

$linux\u app\u user=“ORXXXX\”+$args[0]
$pass\u win=$args[1]
$path=$args[2]
$pass=ConvertTo SecureString-AsPlainText$pass\u win-Force
$cred=新对象System.Management.Automation.PSCredential-ArgumentList
$linux\u应用程序\u用户,$pass
$Invoke命令-ComputerName XXXXXXXX.XXXX.XXX.XXXX-Credential$cred-ErrorAction
停止-脚本块{
参数($path)
调用表达式$path
}-Arg$path
cal.bat
脚本:

@echo关闭
设置服务器=slXXXXXXXX.XXX.XXXX.com
设置PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0
CD/D“%PowerShellDir%
powershell.exe-ExecutionPolicy RemoteSigned-文件
C:\Users\chaj\Documents\String\Unt1.ps1'XXXX''XXXX@321''C:\cal.bat'
错误:

[xxxxxx.xx.xxxxx.xxx] Connecting to remote server xxxxxx.xx.xxxxx.xxx failed with the following error message : The user name or password is incorrect. For more information, see the about_Remote_Troubleshooting Help topic. At C:\Users\chafg\Documents\String\Unt1.ps1:7 char:1 + $Result=Invoke-Command -ComputerName xxxxxx.xx.xxxxx.xxx -Credenti ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OpenError: (xxxxxx.xx.xxxxx.xxx) [], PSRemotingTransportException + FullyQualifiedErrorId : LogonFailure,PSSessionStateBroken [xxxxxx.xx.xxxxx.xxx]连接到远程服务器xxxxxx.xx.xxxxx.xxx失败 出现以下错误消息:用户名或密码不正确。 有关更多信息,请参阅关于远程故障排除帮助主题。 在C:\Users\chagg\Documents\String\Unt1.ps1:7 char:1 +$Result=Invoke命令-ComputerName xxxxxx.xx.xxxxx.xxx-Credenti。。。 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +类别信息:OpenError:(xxxxxx.xx.xxxxx.xxx)[],PSRemotingTransportException +FullyQualifiedErrorId:LogonFailure,PSSessionStateBreaked 将
-File
参数一起使用时,所有参数都会逐字使用-除非包含在
“…”
(双引号)中:与使用
-Command
时不同,
“…”
(单引号)不会被识别为字符串分隔符

因此,您的命令(此处简化):

powershell.exe-文件C:\path\to\Unt1.ps1'XXXX''XXXX@321''C:\cal.bat'

使
Unt1.ps1
脚本查看包含
'
的参数,这不是您的意图;例如,它不是按预期接收
$args[0]
而是逐字接收
'XXXX'

修复方法是使用
“…”
(双引号)

powershell.exe -File C:\path\to\Unt1.ps1 "XXXX" "XXXX@321" "C:\cal.bat"
或者,考虑到您的特定示例参数不需要引用(尽管真实参数可能需要引用):


能否请您向我们提供
cal.bat
的实际格式正确的内容?当前唯一正确的行是
@echo off
您发布的两个代码段都已断开(换行不正确,缺少双引号,…)。请修复您问题中的代码。当您发布的代码不允许我们重现您面临的问题时,我们无法帮助您。我修改了cal.bat代码。您能验证代码并告诉我该问题的解决方案吗?您的
cal.bat
内容仍然不正确。这些是非关键性的:
1。
它不使用re
SET
命令的推荐语法,即
SET“VarName=StringValue“
2.
您的
CD
文件路径上缺少结束双引号。这些是非常关键的
3。
您丢失了一个行连接字符或添加了一个不需要的换行符,
cmd.exe
不处理单引号,
,其处理方式与
powershell.exe
处理字符串的方式相同。@Compo:虽然单引号肯定是错误的引号,我突然想到,在这种情况下,
cmd.exe
是偶然出现的(除非您在
“…”
中有未替换的
cmd.exe
元字符):问题是,对于
-File
,PowerShell也不会将单引号识别为字符串分隔符,并将其视为数据的一部分。
powershell.exe -File C:\path\to\Unt1.ps1 XXXX XXXX@321 C:\cal.bat