Forum Discussion
xaura
Jul 24, 2023Copper Contributor
Can't pass arguments from one script to another script
Hey! I am trying to find a way to pass arguments I provide while originally running my script to a version of the script which reruns if I don't have admin permissions. When I have tried what...
LeonPavesic
Jul 25, 2023Silver Contributor
Hi xaura,
i tried to modify the script, you can try and test if it works:
param (
[bool]$RepairMenu,
[string]$RunStage
)
# Check for running as admin
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host "Attempting to Elevate!"
# Re-launch the script elevated and pass the arguments using -ArgumentList
Start-Process powershell.exe -Verb RunAs -ArgumentList "-File `"$PSCommandPath`", '-RepairMenu', '$RepairMenu', '-RunStage', '$RunStage'"
Exit # Exit the current script after launching the elevated one
}
# The rest of your script code goes here, which will be executed with admin permissions.
# For demonstration purposes, let's just print the received arguments:
Write-Host "RepairMenu: $RepairMenu"
Write-Host "RunStage: $RunStage"
You can save the above script in a .ps1 file, for example, myscript.ps1. Then, you can run the script from within its directory using the desired arguments like this
.\myscript.ps1 -RepairMenu $true -RunStage "Production"
You can also replace the # The rest of your script code goes here comment with the actual code of your script that should be executed with admin permissions.
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.
If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic
xaura
Jul 25, 2023Copper Contributor
Unfortunaely after adding Read-Host to be sure it wasn't just losing cause the script knowingly ended, it still closes immediately