1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

run powershell script as administrator?

Discussion in 'HTML, Graphics & Programming' started by wesley, Feb 13, 2020.

  1. wesley

    Soldato

    Joined: Jul 29, 2003

    Posts: 7,281

    hi

    got a wee powershell script that enable IP to DHCP and its working when i copy the script to powershell with adminsitrator

    i would like to make a script instead but it wouldnt let me run it.

    $networkConfig = Get-WmiObject Win32_NetworkAdapterConfiguration -filter "ipenabled = 'true'"
    $networkConfig.EnableDHCP()
    $networkConfig.SetDNSServerSearchOrder()

    ipconfig /registerdns

    "*****************************************" | Out-File C:\Windows\COMPANY-staticIP.txt -Append
    get-date | Out-File C:\Windows\COMPANY-staticIP.txt -Append
    ipconfig /all | Out-File C:\Windows\COMPANY-staticIP.txt -Append

    how to run the .ps1 with administrator? there is no option to right click -select run as adminstrator

    thanks

    :)
     
  2. the-evaluator

    Wise Guy

    Joined: Sep 24, 2015

    Posts: 1,695

    How are you wanting to run it, by double clicking the script or opening PowerShell / ISE and running it from there?
     
  3. wesley

    Soldato

    Joined: Jul 29, 2003

    Posts: 7,281

    Double clicking the script

    Thanks
     
  4. the-evaluator

    Wise Guy

    Joined: Sep 24, 2015

    Posts: 1,695

    I haven't tried it but what if you create a shortcut with the target set to:

    Code:
    powershell.exe -command "c:\path-to-script\script.ps1"

    Then right click on the shortcut, go to Advanced and there should be an option to run that as administrator.
     
  5. wesley

    Soldato

    Joined: Jul 29, 2003

    Posts: 7,281

    There is no option to run as administrator on PS1 which is rather strange
     
  6. wesley

    Soldato

    Joined: Jul 29, 2003

    Posts: 7,281

    Sorry I misread you

    Will try that when I'm.back work later

    Thanks again
     
  7. the-evaluator

    Wise Guy

    Joined: Sep 24, 2015

    Posts: 1,695

    I just tested it and it seems to work.
     
  8. wesley

    Soldato

    Joined: Jul 29, 2003

    Posts: 7,281

  9. the-evaluator

    Wise Guy

    Joined: Sep 24, 2015

    Posts: 1,695

    Is that literally just a shortcut to the ps1 file? You need to create a shortcut using the target as I mentioned above, not a shortcut to the ps1 file.
     
  10. wesley

    Soldato

    Joined: Jul 29, 2003

    Posts: 7,281

    oops you are right.

    got the administator option working now but the script is not doing anything, it doesnt set the IP to dhcp. which it does works when i copy/paste the script to powershell (run > cmd right click run as adminstrator)
     
  11. Vince

    Man of Honour

    Joined: Oct 30, 2003

    Posts: 8,853

    Location: Essex

    Not a powershell expert by any means but ive written a few scripts and normally get around this by setting the execution policy in the script.
     
  12. wesley

    Soldato

    Joined: Jul 29, 2003

    Posts: 7,281

    thanks

    how?
     
  13. the-evaluator

    Wise Guy

    Joined: Sep 24, 2015

    Posts: 1,695

    Put 'Set-ExecutionPolicy Unrestricted' at the start of the script.
     
  14. wesley

    Soldato

    Joined: Jul 29, 2003

    Posts: 7,281

    Code:
    PS C:\Users\infowp\Desktop> C:\Users\infowp\Desktop\dhcp3.ps1
    File C:\Users\infowp\Desktop\dhcp3.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at
    https:/go.microsoft.com/fwlink/?LinkID=135170.
        + CategoryInfo          : SecurityError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : UnauthorizedAccess
    
    PS C:\Users\infowp\Desktop> 
    got this error message
     
  15. wesley

    Soldato

    Joined: Jul 29, 2003

    Posts: 7,281

  16. the-evaluator

    Wise Guy

    Joined: Sep 24, 2015

    Posts: 1,695

    Open PowerShell as administrator and run 'Get-ExecutionPolicy'. What does it say?
     
  17. wesley

    Soldato

    Joined: Jul 29, 2003

    Posts: 7,281

    PS C:\WINDOWS\system32> Get-ExecutionPolicy
    Restricted
    PS C:\WINDOWS\system32>
     
  18. the-evaluator

    Wise Guy

    Joined: Sep 24, 2015

    Posts: 1,695

    In that administrator PS session, do 'Set-ExecutionPolicy unrestricted'
     
  19. wesley

    Soldato

    Joined: Jul 29, 2003

    Posts: 7,281

    PS C:\WINDOWS\system32> Get-ExecutionPolicy
    Restricted
    PS C:\WINDOWS\system32> Set-ExecutionPolicy unrestricted

    Execution Policy Change
    The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
    you to the security risks described in the about_Execution_Policies help topic at
    https:/go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy?
    [Y] Yes [A] Yes to All [N] No [L] No to All Suspend [?] Help (default is "N"):
     
  20. the-evaluator

    Wise Guy

    Joined: Sep 24, 2015

    Posts: 1,695

    Did you choose Y or A?

    If you did, try the dhcp script shortcut again.