I have written a script that is used to download the current version of another script and look for and install any missing dependant modules. I am posting the last line only. I will post the rest of the script “over 100 lines” if necessary. Everything works fine except the last line.
All of the users of this script are admins on their local computer with their standard user account but have a different elevated network ADM account that is needed to access and manage user/device accounts in Active Directory and AzureAD, so just the simple “runas” will not work
Using this line does exactly what it needs to except the credential dialog has the mydomain\ populated in the username box, requiring the user to double or triple click in the field to enter their login ID Start-Process -FilePath Powershell "C:\Foldername\mainscript.ps1" -Credential mydomain\
Using this line will do everything perfectly except launch the final script. Start-Process -FilePath Powershell "C:\Foldername\mainscript.ps1" -verb runasuser
If I got it right … depending on the naming conventions of the accounts and admin accounts in your AD you could use the account name of the user and transform it to the admin name of the user and us it for the -Credential parameter.
How often will this “other” script change? How often will the modules change? How about using a software deployment solution instead? If these “admin” users need to enter their admin credentials anyway - why not starting the PowerShell session already elevated with their admin account?
The reason for not just starting Powershell as adm is in order for that to work you would have to right-click on the icon and run as different user in Win10 and in Win 11 you would have to choose more options and after that click on run as different user. This way I create a shortcut that reads as follows