Windows 用于重新启动网络适配器的简单Powershell脚本对于在受限用户帐户上工作变得不简单

Windows 用于重新启动网络适配器的简单Powershell脚本对于在受限用户帐户上工作变得不简单,windows,powershell,scripting,windows-8.1,Windows,Powershell,Scripting,Windows 8.1,因此,我只想创建一个快捷脚本,单击该脚本将重新启动网络适配器。问题是,它需要在一个基本上没有特权的帐户上运行,所以我需要将它提升为另一个用户(管理员帐户)运行 我想不出做这件事的正确方法,这让我发疯。这就是我到目前为止所做的: $username = "Domain\User" $password = "Password" $credentials = New-Object System.Management.Automation.PSCredential -ArgumentList @($us

因此,我只想创建一个快捷脚本,单击该脚本将重新启动网络适配器。问题是,它需要在一个基本上没有特权的帐户上运行,所以我需要将它提升为另一个用户(管理员帐户)运行

我想不出做这件事的正确方法,这让我发疯。这就是我到目前为止所做的:

$username = "Domain\User"
$password = "Password"
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))
start-process powershell -Credential ($credentials) -ArgumentList '-ExecutionPolicy unrestricted -noprofile -verb runas -inputformat text -command "{restart-netadapter -InterfaceDescription "Dell Wireless 1538 802.11 a/g/n Adapter" -Confirm:$false}"'
它将打开一个新的powershell窗口,但该命令无法运行。它可以在提升的powershell提示符中自行正常工作。有一次我发现,即使我使用管理员帐户调用powershell,它也不是提升的powershell,因此我添加了
-动词runas
,但它仍然不起作用


这真的不应该那么难,但无论如何我都不是powershell大师。非常感谢您的帮助。谢谢

这是我用来让我的代码工作的代码,我不能因为编写它而获得荣誉,因为我是从


但这会检查它是否提升,如果没有提升,则会提升它。

在我看来,最好的方法是创建一个计划任务,以特权帐户的形式运行脚本。完全去掉嵌入的凭证

然后,受限帐户只需要能够启动任务


由于重新启动适配器的代码是一行代码,您甚至不需要将其放入脚本文件中,因此您无需担心执行策略或任何问题。

首先我注意到,您的密码是纯文本的,这并不理想,我将告诉您,如果不容易,请尝试保护该密码。我会看看是否能找到我用来启动Admin Powershell的代码,我会给你回复,尽管我不会在不要求凭据的情况下提升它。我可以让它开始使用start Process powershell-ArgumentList'-ExecutionPolicy Unrestricted-文件C:\wifi\u reset.ps1'-verb RunAs,但它会请求其他帐户上的凭据以提升它。如果我添加-Credential来启动进程,它不希望在结尾使用-verb RunAs。对,您需要根据需要更改代码,这对我来说很有效,但您可以将凭据硬编码到这个中,它应该做您需要的事情。
# Get the ID and security principal of the current user account
$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object     System.Security.Principal.WindowsPrincipal($myWindowsID)

# Get the security principal for the Administrator role
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator

# Check to see if we are currently running "as Administrator"
if ($myWindowsPrincipal.IsInRole($adminRole))
  {
   # We are running "as Administrator" - so change the title and background     color to indicate this
   $Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"
   $Host.UI.RawUI.BackgroundColor = "DarkBlue"
   clear-host
   }
else
   {
   # We are not running "as Administrator" - so relaunch as administrator

   # Create a new process object that starts PowerShell
   $newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";

# Specify the current script path and name as a parameter
$newProcess.Arguments = $myInvocation.MyCommand.Definition;

# Indicate that the process should be elevated
$newProcess.Verb = "runas";

# Start the new process
[System.Diagnostics.Process]::Start($newProcess);

# Exit from the current, unelevated, process
exit
}

# Run your code that needs to be elevated here
Write-Host -NoNewLine "Press any key to continue..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")