[英]calling PowerShell script from batch file to connect remote machine is not working
When I am running a PowerShell script normally it's working fine, issue is raising when calling the same script from batch file.当我正常运行 PowerShell 脚本时,它工作正常,从批处理文件调用相同的脚本时会出现问题。
Unt1.ps1
script: Unt1.ps1
脚本:
$linux_app_user="ORXXXX\"+$args[0]
$pass_win=$args[1]
$path=$args[2]
$pass = ConvertTo-SecureString -AsPlainText $pass_win -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList
$linux_app_user, $pass
$Invoke-Command -ComputerName XXXXXXXX.XXXX.XXX.XXXX -Credential $cred -ErrorAction
Stop -ScriptBlock {
param($path)
Invoke-Expression $path
} -Arg $path
cal.bat
script: cal.bat
脚本:
@echo off
SET Server=slXXXXXXXX.XXX.XXXX.com
SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0
CD /D "%PowerShellDir%
powershell.exe -ExecutionPolicy RemoteSigned -File
C:\Users\chaj\Documents\String\Unt1.ps1 'XXXX' 'XXXX@321' 'C:\cal.bat'
Error:错误:
[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
When using the PowerShell CLI with the -File
parameter, all arguments are used verbatim - except if enclosed in "..."
(double quotes): unlike when you use -Command
, '...'
(single quotes) are not recognized as string delimiters .当使用带有 -File 参数的PowerShell CLI -File
,所有 arguments 都将逐字使用 - 除非包含在"..."
(双引号)中:与使用-Command
时不同, '...'
(单引号)无法识别作为字符串分隔符。
Therefore, your command (simplified here):因此,您的命令(此处简化):
powershell.exe -File C:\path\to\Unt1.ps1 'XXXX' 'XXXX@321' 'C:\cal.bat'
causes the Unt1.ps1
script to see the arguments with the enclosing '
, which is not your intent;导致Unt1.ps1
脚本看到带有封闭'
的 arguments ,这不是您的意图; eg, instead of $args[0]
receiving XXXX
, as intended, it receives 'XXXX'
, verbatim.例如,不是$args[0]
按预期接收XXXX
,而是逐字接收'XXXX'
。
The fix is to use "..."
(double quotes) :解决方法是使用"..."
(双引号) :
powershell.exe -File C:\path\to\Unt1.ps1 "XXXX" "XXXX@321" "C:\cal.bat"
Alternatively, given that your specific sample arguments don't require quoting (though the real ones may):或者,鉴于您的特定样本 arguments不需要引用(尽管真实的可能):
powershell.exe -File C:\path\to\Unt1.ps1 XXXX XXXX@321 C:\cal.bat
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.