如何设置可执行文件以在PowerShell中以管理员权限运行

时间:2016-12-12 17:23:33

标签: powershell powershell-v2.0

我需要帮助

我很难在powershell中设置一个脚本,该脚本为用户的DOMAIN管理员权限提供可执行文件。因为我需要在许多桌面上安装程序,而且我需要检查程序是否已经安装。

这种接缝很简单,但我知道如何在shell脚本中编程,而不是很多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);
    }  

1 个答案:

答案 0 :(得分:1)

你有什么理由不能简单地做到:

Start-Process powershell -Verb runAs

从PS控制台?这将启动一个具有管理员权限的新ps窗口....

PowerShell: Running a command as Administrator