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

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

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

[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脚本:

@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'

错误:

[xxxxxx.xx.xxxxx.xxx]连接到远程服务器xxxxxx.xx.xxxxx.xxx失败并显示以下错误消息:用户名或密码不正确。有关更多信息,请参见about_Remote_Troubleshooting帮助主题。在C:\ Users \ chafg \ Documents \ String \ Unt1.ps1:7 char:1+ $ Result =调用命令-ComputerName xxxxxx.xx.xxxxx.xxx -Credenti ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~+ CategoryInfo:OpenError:(xxxxxx.xx.xxxxx.xxx)[],PSRemotingTransportException+ FullyQualifiedErrorId:LogonFailure,PSSessionStateBroken
powershell batch-file parameter-passing quoting
1个回答
0
投票

[当使用PowerShell CLI时-File参数一起使用时,所有参数都使用verbatim-除非括在"..."(双引号):与使用-Command时不同,< ['...'(单引号)被not识别为字符串delimiters

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

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

使Unt1.ps1脚本查看自变量

,并附带'

,这不是您的意图;例如,它按原样接收$args[0]而不是XXXX接收'XXXX'

修复是使用"..."(双引号)

powershell.exe -File C:\path\to\Unt1.ps1 "XXXX" "XXXX@321" "C:\cal.bat"

或者,假设您的特定样本参数不引用([require(尽管实际的可能):]]
powershell.exe -File C:\path\to\Unt1.ps1 XXXX XXXX@321 C:\cal.bat
© www.soinside.com 2019 - 2024. All rights reserved.