简体   繁体   中英

Running a PowerShell script as administrator without typing in passwords

I wrote a script that will switch between having a computer connect via Wi-Fi or wired Internet simply by running a batch file. I wrote this because I don't like having to type in my username and password to switch between the two every time.

NOTE: I HAVE to have UAC enabled so unfortunately just turning it off isn't an option.

It looks at the status of the wireless adapter. If it is currently enabled, it will turn off the wireless adapter and enable the wired adapter. If the wireless is not enabled, it will enabled and disable the wired. This is the code.

$adapter = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName . | Where-Object {$_.ServiceName -ne "hamachi"}
function wirelessOn
{
    $wireless = Get-WmiObject -Class Win32_NetworkAdapter | Where-Object {$_.Name -like "*Wireless*"}
    $wireless.Disable()
    $wired = Get-WmiObject -Class Win32_NetworkAdapter | Where-Object {$_.Name -like "*gigabit*"}
    $wired.Enable()
}

function wirelessOff
{
    $wired = Get-WmiObject -Class Win32_NetworkAdapter | Where-Object {$_.Name -like "*gigabit*"}
    $wired.Disable()
    $wireless = Get-WmiObject -Class Win32_NetworkAdapter | Where-Object {$_.Name -like "*Wireless*"}
    $wireless.Enable()
}

switch -wildcard ($adapter.description){
    "*wireless*" {
        wirelessOn
    }
    "*gigabit*" {
        wirelessOff
    }
}

Unfortunately, this script only functions properly if run as administrator, thus the whole reason I wrote it is moot. Is there a way I can have this elevate to administrator privileges without me having to do anything?

You can start a new, elevated PowerShell process to run your script eg:

Start-Process PowerShell -verb runas -ArgumentList '-noexit','-File','path-to-script'

If you don't want the PowerShell window to hang around then get rid of the '-noexit' but for debugging the launch of your script, it is useful.

If you had access to an admin account username/password, you could do this:

# Capture encrypted password once and store to file
$passwd = Read-Host "Enter password" -AsSecureString
$encpwd = ConvertFrom-SecureString $passwd
$encpwd > $path\password.bin

# Afterwards always use this to start the script
$encpwd = Get-Content $path\password.bin
$passwd = ConvertTo-SecureString $encpwd
$cred = new-object System.Management.Automation.PSCredential 'domain\username',$passwd
Start-Process PowerShell -Cred $cred -ArgumentList '-noexit','-File','path-to-script'   

You can do this by using a batch (.bat) file and Task Scheduler.

First, you must create a batch file (you can use notepad for this) in the same folder as your PowerShell .ps1 file with the following contents:

@ECHO OFF
SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%PutTheNameOfYourPowerShellScriptHere.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%PowerShellScriptPath%""' -Verb RunAs}";

Open Task Scheduler and click on Task Scheduler Library on the left pane. Then, right-click Task Scheduler Library and select Create Task (this should open a window). Give your task a name and check that Run with highest privileges is selected. Then, click on the Actions tab and click on New (this should open a window). Verify that the selected Action is Start a program and then click on Browse to select the batch file. Click Ok on both windows and you should be back on the main Task Scheduler window. You should see your newly created task on top of the list, so right-click it and select Run . You should see that your PowerShell script runs with full Administrator Privileges and without any UAC prompts.

Now, if you want to run your PowerShell script on a regular schedule, you could customize that by right-clicking your newly created task, selecting Properties , clicking on the Triggers tab and clicking on New .

However, if you'd prefer to run your PowerShell script by double-clicking a shortcut file you'd have to follow some additional steps. Right-click on your desktop and select New > Shortcut and enter the following location:

C:\Windows\System32\schtasks.exe /run /tn "PutTheNameOfYourNewlyCreatedTaskHere"

Click Next , give your shortcut a name and click on Finish . You should try opening your shortcut and see if your PowerShell script runs as you want.

That is all.

Bonus: if you want to run your PowerShell script by pressing a key or a key combination, you can also right-click your shortcut and input what you want in the Shortcut key: field. However, if it doesn't let you enter a particular key or key combination, you could also install PowerToys by Microsoft and use its Keyboard Manager utility to remap a key or a key combination. For example. you could enter the F8 in the Shortcut key: field and then remap that to a key that you rarely use, like the Calculator key on most keyboards.

Please let me know if you have any problems while following this guide.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM