Run Command as administrator in PowerShell script. UAC

前端 未结 4 1592
北海茫月
北海茫月 2021-02-20 05:06

OK here is my issue:

I am trying to run a script remotely on a server.

I am an administrator on both boxes, firewall exceptions are in place, remote admin is e

4条回答
  •  时光说笑
    2021-02-20 05:26

    OK. After some research and testing I figured out the issue. After disabling UAC and the firewall and the script still not working I dug a little deeper and discovered that the main issue was the way invoke-command runs the commands. it uses the credentials of the person running the script to authenticate to the server then tries to use another account to run the permissions or lowers the privileges of the user so that certain commands cannot be run.

    I added the -Credentials switch to the invoke command and everything is working great now. Corrected code sample below:

    $user = New-Object Management.Automation.PSCredential("$UserName", $securePassword)
    invoke-command -ComputerName $ComputerName -Credential $user -ScriptBlock ` 
    { 
        cd C:\Windows\System32\inetsrv\;  
        ./appcmd.exe ADD vdir /app.name:/ /path:/ /physicalPath: 
    } 
    

提交回复
热议问题