用于重新启动网络适配器的简单Powershell脚本变得简单,无法在受限制的用户帐户上工作

时间:2015-07-10 19:19:17

标签: windows powershell scripting windows-8.1

所以我要做的就是创建一个快捷方式脚本,单击该脚本将重新启动网络适配器。问题是它需要在一个基本没有权限的帐户上运行,所以我需要让它运行升级并作为不同的用户(管理员帐户)。

我无法找到正确的方法来做到这一点并让我疯狂。这就是我到目前为止所做的:

$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,所以我添加了-verb runas,但它仍然无法正常工作。

这真的不应该那么难,但我绝不是一个强权大师。任何帮助深表感谢。谢谢!

2 个答案:

答案 0 :(得分:1)

在我看来,执行此操作的最佳方法是创建一个将脚本作为特权帐户运行的计划任务。完全摆脱嵌入式凭证。

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

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

答案 1 :(得分:0)

这是我以前用来工作的代码,因为我是从Here

找到的,所以我写不出来
# 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")

但是这会检查它是否升高,然后如果它没有升高它。