您知道如果您是系统的管理员用户,您可以右键单击一个批处理脚本,然后以管理员身份运行它,而无需输入管理员密码?
我想知道如何用PowerShell脚本做到这一点。我不想输入我的密码;我只是想模仿右键单击Run As Administrator方法。
到目前为止,我读到的所有内容都要求您提供管理员密码。
您知道如果您是系统的管理员用户,您可以右键单击一个批处理脚本,然后以管理员身份运行它,而无需输入管理员密码?
我想知道如何用PowerShell脚本做到这一点。我不想输入我的密码;我只是想模仿右键单击Run As Administrator方法。
到目前为止,我读到的所有内容都要求您提供管理员密码。
当前回答
在Shay Levy的答案之上,遵循下面的设置(只有一次)
以管理员权限启动PowerShell。 在堆栈溢出问题PowerShell说“脚本的执行在这个系统上是禁用的。” 例如,将.ps1文件放在任何PATH文件夹中。Windows \ System32系统文件夹
设置之后:
按Win + R 调用powershell Start-Process powershell -Verb runAs <ps1_file>
现在,您可以在一个命令行中运行所有内容。上述操作适用于Windows 8 Basic 64位。
其他回答
事实证明这太简单了。您所要做的就是以管理员身份运行cmd。然后输入explorer.exe并按enter键。这会打开Windows资源管理器。 现在右键单击你想要运行的PowerShell脚本,选择“运行PowerShell”,这将在管理员模式下启动PowerShell。
它可能会要求您启用策略以运行,键入Y并按enter。现在脚本将以管理员身份在PowerShell中运行。万一全红了,说明你的政策还没生效。然后再试一次,应该可以正常工作。
@pgk和@Andrew Odri的回答的问题是当您有脚本参数时,特别是当它们是强制性的时。可以通过以下方法解决此问题:
用户右键单击.ps1文件并选择“使用PowerShell运行”:通过输入框向他询问参数(这是比使用HelpMessage参数属性更好的选择); 用户通过控制台执行脚本:允许他传递所需的参数,并让控制台强制他通知必须的参数。
下面是如何将是代码,如果脚本有计算机名和端口强制参数:
[CmdletBinding(DefaultParametersetName='RunWithPowerShellContextMenu')]
param (
[parameter(ParameterSetName='CallFromCommandLine')]
[switch] $CallFromCommandLine,
[parameter(Mandatory=$false, ParameterSetName='RunWithPowerShellContextMenu')]
[parameter(Mandatory=$true, ParameterSetName='CallFromCommandLine')]
[string] $ComputerName,
[parameter(Mandatory=$false, ParameterSetName='RunWithPowerShellContextMenu')]
[parameter(Mandatory=$true, ParameterSetName='CallFromCommandLine')]
[UInt16] $Port
)
function Assert-AdministrativePrivileges([bool] $CalledFromRunWithPowerShellMenu)
{
$isAdministrator = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if ($isAdministrator)
{
if (!$CalledFromRunWithPowerShellMenu -and !$CallFromCommandLine)
{
# Must call itself asking for obligatory parameters
& "$PSCommandPath" @script:PSBoundParameters -CallFromCommandLine
Exit
}
}
else
{
if (!$CalledFromRunWithPowerShellMenu -and !$CallFromCommandLine)
{
$serializedParams = [Management.Automation.PSSerializer]::Serialize($script:PSBoundParameters)
$scriptStr = @"
`$serializedParams = '$($serializedParams -replace "'", "''")'
`$params = [Management.Automation.PSSerializer]::Deserialize(`$serializedParams)
& "$PSCommandPath" @params -CallFromCommandLine
"@
$scriptBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptStr)
$encodedCommand = [Convert]::ToBase64String($scriptBytes)
# If this script is called from another one, the execution flow must wait for this script to finish.
Start-Process -FilePath 'powershell' -ArgumentList "-ExecutionPolicy Bypass -NoProfile -EncodedCommand $encodedCommand" -Verb 'RunAs' -Wait
}
else
{
# When you use the "Run with PowerShell" feature, the Windows PowerShell console window appears only briefly.
# The NoExit option makes the window stay visible, so the user can see the script result.
Start-Process -FilePath 'powershell' -ArgumentList "-ExecutionPolicy Bypass -NoProfile -NoExit -File ""$PSCommandPath""" -Verb 'RunAs'
}
Exit
}
}
function Get-UserParameters()
{
[string] $script:ComputerName = [Microsoft.VisualBasic.Interaction]::InputBox('Enter a computer name:', 'Testing Network Connection')
if ($script:ComputerName -eq '')
{
throw 'The computer name is required.'
}
[string] $inputPort = [Microsoft.VisualBasic.Interaction]::InputBox('Enter a TCP port:', 'Testing Network Connection')
if ($inputPort -ne '')
{
if (-not [UInt16]::TryParse($inputPort, [ref]$script:Port))
{
throw "The value '$inputPort' is invalid for a port number."
}
}
else
{
throw 'The TCP port is required.'
}
}
# $MyInvocation.Line is empty in the second script execution, when a new powershell session
# is started for this script via Start-Process with the -File option.
$calledFromRunWithPowerShellMenu = $MyInvocation.Line -eq '' -or $MyInvocation.Line.StartsWith('if((Get-ExecutionPolicy')
Assert-AdministrativePrivileges $calledFromRunWithPowerShellMenu
# Necessary for InputBox
[System.Reflection.Assembly]::Load('Microsoft.VisualBasic, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a') | Out-Null
if ($calledFromRunWithPowerShellMenu)
{
Get-UserParameters
}
# ... script code
Test-NetConnection -ComputerName $ComputerName -Port $Port
当然,如果您有管理员帐户,还可以强制以管理员身份打开应用程序。
找到文件,右键单击>属性>快捷方式>高级,并选择以管理员身份运行
单击“确定”。
要将命令的输出附加到包含当前日期的文本文件名,可以这样做:
$winupdfile = 'Windows-Update-' + $(get-date -f MM-dd-yyyy) + '.txt'
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -Command `"Get-WUInstall -AcceptAll | Out-File $env:USERPROFILE\$winupdfile -Append`"" -Verb RunAs; exit } else { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -Command `"Get-WUInstall -AcceptAll | Out-File $env:USERPROFILE\$winupdfile -Append`""; exit }
C:\Users\“username”\AppData\漫游\Microsoft\Windows\开始菜单\程序\Windows PowerShell是PowerShell快捷方式所在的位置。它仍然会转到不同的位置来调用实际的“exe”(%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe)。
由于PowerShell在涉及权限时是用户配置文件驱动的;如果你的用户名/配置文件有权限做一些事情,那么在PowerShell中,你通常也可以这样做。也就是说,您可以更改位于用户配置文件下的快捷方式,例如C:\Users\"username"\AppData\Roaming\Microsoft\Windows\开始菜单\Programs\Windows PowerShell。
右键单击属性。点击“快捷方式”标签下的“高级”按钮,该标签位于“评论”文本框的右下方,与“打开文件位置”和“更改图标”两个按钮的右侧相邻。
选中“以管理员身份运行”的复选框。单击确定,然后应用和确定。再次右键单击位于C:\Users\“username”\AppData\漫游\Microsoft\Windows\开始菜单\程序\Windows PowerShell的图标,并选择“固定到开始菜单/任务栏”。
现在,无论何时单击该图标,它都会调用UAC进行升级。选择“YES”后,您将注意到PowerShell控制台打开,屏幕顶部将标记为“Administrator”。
更进一步说……你可以在Windows PowerShell的配置文件中右键单击相同的图标快捷方式,并分配一个键盘快捷方式,该快捷方式将执行与单击最近添加的图标完全相同的操作。所以在说“快捷键”的地方,输入键盘键/按钮组合,如:Ctrl + Alt + PP(适用于PowerShell)。单击“应用”和“确定”。
现在你所要做的就是按下你分配的按钮组合,你会看到UAC被调用,在你选择'YES'后,你会看到一个PowerShell控制台出现,“Administrator”显示在标题栏上。