Daniel
Daniel

Reputation: 95

How to set an executable to run with administrator rights in powershell

I need a help

I have trouble to set a script in powershell that give DOMAIN adminsitrator rights to an User to an executable . Because I need to install a program in many desktops, plus I need to check if the program is already installed.

This seams to be easy but I know how to program in shell script not much powershell.

$SPAdmin = "DOMAIN\ADMIN" 
$Password="FooBoo"|ConvertTo-SecureString -AsPlainText -Force
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $SPAdmin, $Password 

Get-WmiObject -Class Win32_Service -ComputerName "Server" -Filter "Name='ServiceName'" -Credential $Credential

$name = "CagService"

   if (Get-Service $name -ErrorAction SilentlyContinue)
    {   
     Write-Host "O Servico do Agente conhecido como $name ja esta Instalado na Maquina com Hostname: $env:computername"
     sleep 5
    }
    Else
    {
            $storageDir = $pwd
            $source = "https://source"
            $destination = "$storageDir\agent.exe"

            Invoke-WebRequest $source -OutFile $destination

            $exec = New-Object -com shell.application
            $exec.shellexecute($destination);
    }  

Upvotes: 1

Views: 9666

Answers (1)

Nick
Nick

Reputation: 1208

Is there any reason that you can't simply do:

Start-Process powershell -Verb runAs

From a PS console? That launches a new ps window with admin privileges....

PowerShell: Running a command as Administrator

Upvotes: 1

Related Questions