0
votes

When I am running a PowerShell script normally it's working fine, issue is raising when calling the same script from batch file.

Unt1.ps1 script:

$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:

@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
1
Can you please edit your question to provide us with the actual correctly formatted content of cal.bat? The only correct line is currently @echo offCompo
Both code snippets you posted are broken (incorrect line wrapping, missing double quote, ...). Please fix the code in your question. We cannot help you when the code you post doesn't allow us to reproduce the problem you're facing.Ansgar Wiechers
I modified the cal.bat code can you please verify the code and tell me solution for that problemjanga chaitanya
Your cal.bat content is still incorrect. These are non-critical: 1. It doesn't use the recommended syntax for the SET commands, i.e. SET "VarName=StringValue", 2. There is a missing closing doublequote on your CD filepath. These are critical 3. you have missed a line concatenation character or added an unwanted line break, and cmd.exe does not treat single quotes, ', in the same way as powershell.exe does for strings.Compo
@Compo: While single quotes are definitely the wrong quotes to use, it occurred to me that cmd.exe is incidental in this scenario (except if you had unescaped cmd.exe metacharacters inside '...'): the issues is that with -File, PowerShell doesn't recognize single quotes as string delimiters either and considers them part of the data.mklement0

1 Answers

0
votes

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.

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; e.g., instead of $args[0] receiving XXXX, as intended, it receives 'XXXX', verbatim.

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):

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