Page 1 of 1

Pass Parameter from Batch File to Powershell - help

Posted: 26 Jul 2022 12:43
by DosFiorano
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

Re: Pass Parameter from Batch File to Powershell - help

Posted: 27 Jul 2022 13:34
by aGerman
I guess you should pass %1 to PowerShell and grab it using $args[0] in the PowerShell script.

Steffen