run powershell script as administrator?

Soldato
Joined
29 Jul 2003
Posts
7,663
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

:)
 
Soldato
Joined
24 Sep 2015
Posts
3,657
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.
 
Soldato
OP
Joined
29 Jul 2003
Posts
7,663
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)
 
Man of Honour
Joined
30 Oct 2003
Posts
13,229
Location
Essex
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)

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.
 
Soldato
OP
Joined
29 Jul 2003
Posts
7,663
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
 
Soldato
OP
Joined
29 Jul 2003
Posts
7,663
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"):
 
Back
Top Bottom