以管理员身份运行 PowerShell 脚本而不输入密码 [英] Running a PowerShell script as administrator without typing in passwords

查看:98
本文介绍了以管理员身份运行 PowerShell 脚本而不输入密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个脚本,只需运行一个批处理文件,它就可以在通过 Wi-Fi 或有线 Internet 连接计算机之间进行切换.我写这个是因为我不喜欢每次都输入用户名和密码在两者之间切换.

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.

注意:我必须启用 UAC,所以很遗憾,不能选择关闭它.

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?

推荐答案

您可以启动一个新的、提升的 PowerShell 进程来运行您的脚本,例如:

You can start a new, elevated PowerShell process to run your script e.g.:

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

如果您不希望 PowerShell 窗口停留,那么去掉 '-noexit' 但对于调试脚本的启动,它很有用.

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'   

这篇关于以管理员身份运行 PowerShell 脚本而不输入密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆