当我正常运行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 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"
powershell.exe -File C:\path\to\Unt1.ps1 XXXX XXXX@321 C:\cal.bat