Pass Parameter from Batch File to Powershell - help

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
DosFiorano
Posts: 1
Joined: 26 Jul 2022 09:56

Pass Parameter from Batch File to Powershell - help

#1 Post by DosFiorano » 26 Jul 2022 12:43

Hi Experts.

Some software we are using allows us to call a batch file when a certain event happens. So, Im using this to call a batch file which in turn calls a powershell script to send an notification email... A bit long winded I know. ... :?

So, an example command the software advises is : Cmd.exe /c “c:\process.bat” %1 (where %1 is an parameter ranging from %1 to %9)

At the moment my batch file is :

Code: Select all

echo %date% %time% Data Upload >> C:\upload_log.txt

@echo off
 
powershell.exe -ExecutionPolicy Unrestricted -Command ". 'C:\UPLOAD_ALERT.ps1'"
so, as you can see the batch file adds a new line to a log file and then calls the PS script to send the email.


Next is the PowerShell Script :

Code: Select all

 [System.Net.ServicePointManager]::ServerCertificateValidationCallback = { return $true }

$AWS_ACCESS_KEY = "*********************T32"
$AWS_SECRET_KEY = "***********************************"
$SECURE_KEY = $(ConvertTo-SecureString -AsPlainText -String $AWS_SECRET_KEY -Force)

$EmailFrom = "SFTP@mydomain.co.uk"
$EmailTo = "recipient1@mydomain.co.uk"
$Subject = "Upload completed"
$Body = "Hello - a new file has been uploaded"
$SMTPServer = "email-smtp.eu-west-2.amazonaws.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($AWS_ACCESS_KEY, $SECURE_KEY);
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body) 
This is working perfectly fine - but it ideally we need to pass the '%1' from the initial command Cmd.exe /c “c:\process.bat” %1 , which in our case is the filename that has been loaded, to the powershell script and insert into the body of the email.


Can anyone advise on how to accomplish this please?

Thanks so much

Fiorano

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Pass Parameter from Batch File to Powershell - help

#2 Post by aGerman » 27 Jul 2022 13:34

I guess you should pass %1 to PowerShell and grab it using $args[0] in the PowerShell script.

Steffen

Post Reply