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 seems like 100 ways to do this I either gets errors or I get nothing with the powershell windows just closing immediately.
I've tried a few ways to see if there is any error output as it resolved an earlier issue I ran into (sorta), but this time I can't seem to get any output.
My current code looks like this:
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
Start-Process powershell.exe -Verb RunAs -ArgumentList "-File `"$PSCommandPath`""
}
I would just run my script from within it's directory:
.\logic.ps1 -RepairMenu $true
I most commonly find some variable of this: -RepairMenu `"$RepairMenu`"" should be added within my arguments list, but regardless of how I add it to the list whether it's comma separated, space separated, anything, the script just exits with no reports of anything.
As provided the script will run and any code to do with the parameters passed isn't til later in the script and before that there would be some results I'd see in C: drive. I have no idea where I am going wrong with this.
Not that I hope this is important for my end result of this script, it might be for another involved with it and so I want to squash this now to make my testing prove that my script will work worst case scenario.
Any help would be appreciated!
2 Replies
Sort By
- LeonPavesicSilver 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
- xauraCopper ContributorUnfortunaely after adding Read-Host to be sure it wasn't just losing cause the script knowingly ended, it still closes immediately