简体   繁体   中英

How to pass variable from Batch File to a Powershell Script

I have a batch file that copies user files from one computer to another networked computer.

@echo off
cls
@echo Type the old Computer Name
set /p asset=

REM robocopy.exe \\%asset%\c$\ C:\ /S /Z /XJD /XJ /XA:SH /XA:T /XD "Dir1" "Dir2"  /XF *.dll *.log *.txt *.exe /log+:"\\server\path\%asset%-to-%computername%-Transfer.log" /NP /FP /V /TEE

PowerShell.exe -Command "& {Start-Process PowerShell.exe -ArgumentList '-ExecutionPolicy Bypass -File ""%~dpn0.ps1""' -Verb RunAs}"

pause

This is my PowerShell script:

$source = "\\${env:asset}\C$\Temp\Source"
$dest = "C:\Temp\Dest"
Write-Output $source
Read-Host "Press ENTER to quit"

I then need to call a PowerShell script that invokes an admin login, then pass the %asset% and %useraiu% variables.

I can't seem to figure out how to pass the %asset% and %useraiu% from my batch file to the PowerShell script.

I have found that if you are calling the Powershell script from a batch file and need to have the Powershell script run with admin, you would need to use this syntax.

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File """"%~dpn0.ps1"""" """"%asset%"""" ' -Verb RunAs}"

The PowerShell script name and parameters need to be wrapped in 4 double quotes in order to properly handle paths/values with spaces

This is the only solution that worked for me so far.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM