How to run a powershell remotely as adminstrator and exist powershell script in that window?

My requirement, i have a PowerShell script need to deploy it through ansible. the script needed to open the powershell as administrator. but during the installation the installer tried to start subcommand to install service from C:\Program files\quest\service.exe --install, it is getting failed here because this is going to non admin powershell. the same script if i run manully in the server it is getting through the installation is successfull. the user which ansible login to the machine is already part of administrator and it is domain user

my script is

Function Check-RunAsAdministrator()
{
#Get current user context
$CurrentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())

#Check user is running the script is member of Administrator Group
if($CurrentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator))
{
Write-host “Script is running with Administrator privileges!”
}
else
{
#Create a new Elevated process to Start PowerShell
$ElevatedProcess = New-Object System.Diagnostics.ProcessStartInfo “PowerShell”;

   # Specify the current script path and name as a parameter
   $ElevatedProcess.Arguments = "& '" + $script:MyInvocation.MyCommand.Path + "'"

   #Set the Process to elevated
   $ElevatedProcess.Verb = "runas"

   #Start the new elevated process
   [System.Diagnostics.Process]::Start($ElevatedProcess)

   #Exit from the current, unelevated, process
   Exit

}

}
#Check Script is running with Elevated Privileges
Check-RunAsAdministrator
#Place your script here.
Import-Module "C:\Program Files\Quest\ChangeAuditor\Client\ChangeAuditor.PowerShell.dll
$password = convertTo-SecureString “xxxxxx” -AsPlainText -Force
$dbcredential = New-Object System.Management.Automation.PSCredential(“xxxx@domain.com”, $password)
Install-CACoordinator -MsiPath “D:\Quest.msi” -DatabaseServer “sqlmi” -AzureADAuthDatabaseCredential $dbcredential -Databasename “ChangeAuditor” -LogPath “D:\install.log”

Raja,
Welcome to the forum. :wave:t3:

Without having any experience with ansible …

That sounds weird and it not a standard behaviour of PowerShell and might be caused by ansible. Usually every process launched by an elevated PowerShell session will inherit its environment from this eleveted PowerShell.

Anyway you don’t need to re-invent the wheel again. You can use your prefered internet search enginge to look for “PowerShell self elevate” and you will find more than enough examples of code snippets you can use to make a script restart itself with elevated rights if it’s started without. :wink:

Regardless of that … When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.