0

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You're right. I was mistaken about -Verb, which is a parameter of Start-Process, not of powershell.exe. Sorry about the noise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.