Passing Parameter From Batch File to Powershell?

2020-05-06 12:16发布

问题:

I am trying to create a form which passes variable values for 1.CustomerID and 2.EmailReceiver into a batch file. I then want the Customer ID to be sent to a Sub-Procedure and produce reports (this is working) and have the EmailReceiver value sent to a PowerShell file. However when i try to pass the %EmailReceiver% value to the Powershell file it doesn't seem to work. I have the following Code.

Batch File Code:

<!-- :
for /f "tokens=1 delims=," %%a in ('mshta.exe "%~f0"') do (
    set "CustomerID=%%~a"    
)
Set EmailReceiver=%EmailReceiver%
sqlcmd -S LAPTOP\MSSQLSERVER02 -d Customer -E -Q " EXEC GetCustomerID '%CustomerId%' " -s "," -o "C:\Users\Iteration5\SearchResults\%CustomerID%.csv"
sqlcmd -S LAPTOP\MSSQLSERVER02 -d AuditLog -E -Q " EXEC dbo.AuditLogCustomerIDTest4'%CustomerId%','%SearchDate%', '%UserName%','%SearchTime%' "
powershell.exe -executionpolicy bypass -file C:\Users\Working.ps1 %EmailReceiver%

and the input fields relating to the CustomerID and EmailReceiver are;

<script language='javascript' >
        function pipeText() {
            var CustomerId=document.getElementById('CustomerId').value;
            var EmailReceiver=document.getElementById('EmailReceiver').value;


            var Batch = new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1);
            close(Batch.WriteLine('"'+CustomerId+'","'+EmailReceiver+'"'));
      }
    </script>


 <div style="font-family:Georgia; color:red; font-size:20px; display:flex; flex-direction: row; justify-content: center; text-align:center;"> 
   <label style="font-size:30px" for="Student">Customer ID:</label>
   <input title="This is the CustomerId" type='text' name='CustomerId' maxlength="20" size='25' required></input><br>
 </div>

 <div style="font-family:Georgia; color:red; font-size:20px; display:flex; flex-direction: row; justify-content: center; text-align:center;"> 
   <label style="font-size:30px" for="Student">Send Email To:</label>
   <input title="This is the CustomerId" type='text' name='EmailReceiver' maxlength="40" size='25' required></input><br>
 </div>

<br>
 <div style="justify-content: center; text-align:center;">   
    <button onclick='pipeText()'" style="background-color: #FFFFFF; color:red; border: 1px solid black;">Submit</button>
 </div>

The Powershell Code i then have which i want the EmailReceiver variable value to be passed to is:

echo $EmailReceiver
$MyEmail = "blondedavid@gmail.com"
$SMTP= "smtp.gmail.com"
$To = "$EmailReceiver"
$Subject = "SQL Requests"
$Body = "Something relating to SQL"
$Creds = (Get-Credential -Credential "$MyEmail")
$attachment = "C:\Users\David\Documents\4thYear\SQLAutomationCollege\Iteration5\SearchResults\37.csv"

Start-Sleep 2

Send-MailMessage -To $to -From $MyEmail -Subject $Subject -Body $Body -SmtpServer $SMTP -Credential $Creds -UseSsl -Port 587 -attachment $attachment 
pause

I'm sorry I am still new to this but would appreciate if anyone could tell me why I am getting an error saying that the -To value cannot be left as empty.