PowerShell:以管理员身份运行命令

您所在的位置:网站首页 然后以管理员身份运行cmd PowerShell:以管理员身份运行命令

PowerShell:以管理员身份运行命令

2024-02-12 20:08| 来源: 网络整理| 查看: 265

你知道如果你是一个系统的管理用户,你只需右键单击批处理脚本,然后以管理员身份运行它,而不需要输入管理员密码吗?

我想知道如何使用PowerShell脚本执行此操作。我不想输入密码;我只想模仿右键单击以管理员身份运行的方法。

到目前为止,我读到的所有内容都要求您提供管理员密码。

如果当前控制台未提升,并且您尝试执行的操作需要提升的权限,则可以使用"以管理员身份运行"选项启动PowerShell。

1PS> Start-Process powershell -Verb runAs 相关讨论 这似乎是成功的唯一途径。然而,添加的shell窗口在很多情况下都是有问题的。我最终配置了调用脚本以管理员身份运行的内容(例如通过注册表)。 这将在其他位置打开一个新控制台。有没有在当前工作目录下以管理员身份运行的方法? start-process -verb runAs"" -argumentlist"") 此处讨论的Win+X

下面是对Shay Levi建议的补充(只需在脚本开头添加这些行):

1234567If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator")) {   $arguments ="& '" + $myinvocation.mycommand.definition +"'" Start-Process powershell -Verb runAs -ArgumentList $arguments Break }

这将导致当前脚本以管理员模式传递给新的PowerShell进程(如果当前用户有权访问管理员模式,并且脚本不是以管理员身份启动)。

相关讨论 这对我很有用:if (-not (([Security.Principal.WindowsPrincipal][Security.Principal.W‌​indowsIdentity]::Get‌​Current()).IsInRole(‌​[Security.Principal.‌​WindowsBuiltInRole]:‌​:Administrator)))。 很容易修改为不以管理员身份运行时只抛出错误。只需接受if语句,并将throw放在then块中。 @G.Lombard的评论有一个微妙的语法错误。这就是我的工作:if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.Wi‌​ndowsIdentity]::GetC‌​urrent()).IsInRole([‌​Security.Principal.W‌​indowsBuiltInRole]::‌​Administrator))。 唯一对我有用的语法是在最初的帖子里,不是G.Lombard和Anjdreas写的。 我同意使用枚举值([Security.Principal.WindowsBuiltInRole]::Administrator)比使用字符串参数"Administrator"更好-我遇到了具有重命名的内置角色的安全性增强的环境 如果要以管理员身份运行PowerShell,是否有一种方法可以抑制Windows?对于需要静默运行此命令的应用程序,让用户询问可能会很麻烦。

自提升PowerShell脚本

Windows 8.1/PowerShell 4.0+

一行:

123if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator")) { Start-Process powershell.exe"-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit } # Your script here 相关讨论 这也适用于Windows10 Pro。谢谢。 好吧,从技术上讲,如果格式是那样的话,所有东西都是"一行",但这并不能使它成为真正的"一行" 伊克斯。因此,这里有一个更好的方法:if([bool]([security.principal.windowsIdentity]::getcurrent()‌&8203;).groups-notcontains"s-1-5-32-544"start powershell-argumentlist";'$myinvocation.mycommand.path'"-动词runas 缺点:如果在提示中输入一个非管理员,则以一个无休止的分叉出口循环结束。 但这不能通过参数 为了传递args,我将它修改为:if (!([Security.Principal.WindowsPrincipal][Security.Principal.‌​WindowsIdentity]::Ge‌​tCurrent()).IsInRole‌​([Security.Principal‌​.WindowsBuiltInRole]"Administrator")) { Start-Process powershell.exe"-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" `"$args`"" -Verb RunAs; exit }。 这不是一条线。

本杰明阿姆斯特朗发表了一篇关于自我提升PowerShell脚本的优秀文章。他的代码有一些小问题;下面是根据注释中建议的修复程序修改的版本。

基本上,它获取与当前进程关联的标识,检查它是否是管理员,如果不是,则创建具有管理员权限的新PowerShell进程并终止旧进程。

1234567891011121314151617181920212223242526272829303132333435363738# 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 an administrator if ($myWindowsPrincipal.IsInRole($adminRole)) {     # We are running as an administrator, so change the title and background colour to indicate this     $Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition +"(Elevated)";     $Host.UI.RawUI.BackgroundColor ="DarkBlue";     Clear-Host; } else {     # We are not running as an 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 with added scope and support for scripts with spaces in it's path     $newProcess.Arguments ="& '" + $script:MyInvocation.MyCommand.Path +"'"     # 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"); 相关讨论 这正是我要找的。谢谢您! 我没有得到正确的脚本名和参数解析,所以我将执行过程包装在cmd.exe/c $newProcess = new-object System.Diagnostics.ProcessStartInfo"cmd.exe" $newProcess.Arguments = ‘/c ‘ + [System.Environment]::GetCommandLineArgs() $newProcess.WorkingDirectory = [environment]::CurrentDirectory中。 这样做而不是启动流程有什么好处吗?我很好奇这个方法与上面和其他线程上发布的方法之间的区别。他们都依赖.NET,但这种方法更依赖于… 我发现与阿姆斯特朗文章(本文的首句)直接链接相关的各种评论也非常有用。

您可以创建一个批处理文件(*.bat文件),该文件在双击时以管理权限运行PowerShell脚本。这样,您不需要更改PowerShell脚本中的任何内容。为此,请使用与PowerShell脚本相同的名称和位置创建一个批处理文件,然后将以下内容放入其中:

1234567@echo off set scriptFileName=%~n0 set scriptFolderPath=%~dp0 set powershellScriptFileName=%scriptFileName%.ps1 powershell -Command"Start-Process powershell "-ExecutionPolicy Bypass -NoProfile -NoExit -Command `"cd \`"%scriptFolderPath%\`"; & \`".\%powershellScriptFileName%\`"`"" -Verb RunAs"

就是这样!

解释如下:

假设PowerShell脚本位于C:\Temp\ScriptTest.ps1路径中,则批处理文件必须具有C:\Temp\ScriptTest.bat路径。当有人执行此批处理文件时,将执行以下步骤:

命令将执行该命令

1powershell -Command"Start-Process powershell "-ExecutionPolicy Bypass -NoProfile -NoExit -Command `"cd \`"C:\Temp\`"; & \`".\ScriptTest.ps1\`"`"" -Verb RunAs"

将打开新的PowerShell会话并执行以下命令:

1Start-Process powershell"-ExecutionPolicy Bypass -NoProfile -NoExit -Command `"cd \`"C:\Temp\`"; & \`".\ScriptTest.ps1\`"`"" -Verb RunAs

将在system32文件夹中打开另一个具有管理权限的新PowerShell会话,并将向其传递以下参数:

1-ExecutionPolicy Bypass -NoProfile -NoExit -Command"cd "C:\Temp"; & ".\ScriptTest.ps1""

将使用管理权限执行以下命令:

1cd"C:\Temp"; &".\ScriptTest.ps1"

一旦脚本路径和名称参数被双引号引用,它们就可以包含空格或单引号字符(')。

当前文件夹将从system32更改为C:\Temp并执行脚本ScriptTest.ps1。传递参数-NoExit后,窗口将不会关闭,即使您的PowerShell脚本引发了一些异常。

相关讨论 这非常有效,包括桌面快捷方式。谢谢 令人惊叹的!我喜欢这种样式。 如果我这样做,我会弹出一个弹出窗口,询问我是否允许PowerShell对我的系统进行更改。这使得它不能用于自动化。

您可以轻松地添加一些注册表项,以获得用于.ps1文件的"以管理员身份运行"上下文菜单:

123New-Item -Path"Registry::HKEY_CLASSES_ROOT\Microsoft.PowershellScript.1\Shell unas\command" ` -Force -Name '' -Value '"c:\windows\system32\windowspowershell\v1.0\powershell.exe" -noexit"%1"'

(从@shay更新为更简单的脚本)

基本上,在HKCR:\Microsoft.PowershellScript.1\Shell unas\command设置默认值以使用PowerShell调用脚本。

相关讨论 它不起作用,您正在创建一个"(默认)"键,而不是更新"(默认)"键值。我能够将代码压缩成一个对我有用的单行程序。你能测试一下吗?new item-path"注册表::hkey_classes_rootmicrosoft.powershellscript.1sh‌&35; 8203;ell unascommand"-force-名称''-value'"c:windowssystem32windows powershellv1.0powershell.exe"-noexit"%1" @Shay Levy-嗨,Shay,谢谢你的更新。我已经用它更新了答案。它确实有效。但我也工作过,虽然很冗长。我没有用PowerShell做过太多的注册编辑,但是用"(默认)"进行注册编辑是我见过的一个例子。它没有创建一个新的键(类似于默认设置),但确实按预期更新了默认键。你有没有试过或者只是从(default)部分猜到的? 我试过了。它在命令键下创建了一个"(默认)"键。 在对注册表值"C:WindowsSystem32WindowsPowerShellv1.0PowerShell.exe"-ExecutionPolicy RemoteSigned-NoExit";"%1"进行了一些细微更改后,这对我很有效。 答案中的注册表值有各种各样的问题。它实际上不运行该命令,也没有正确地引用脚本名(这意味着它在带有空格的路径上中断)。我成功地使用了以下内容:"c:\windows\system32\windowspowershell\v1.0\powershell.exe" -noexit -command"& '%1'"。

使用

#Requires -RunAsAdministrator

还没有说明。似乎只有在PowerShell4.0之后才会出现。

http://technet.microsoft.com/en-us/library/hh847765.aspx

When this switch parameter is added to your requires statement, it specifies that the Windows PowerShell session in which you are running the script must be started with elevated user rights (Run as Administrator).

对我来说,这似乎是一个很好的方法,但我还不确定现场经验。PowerShell3.0运行时可能会忽略这一点,甚至更糟的是,会给出一个错误。

当脚本以非管理员身份运行时,会出现以下错误:

The script 'StackOverflow.ps1' cannot be run because it contains a "#requires" statement for running as Administrator. The current Windows PowerShell session is not running as Administrator. Start Windows PowerShell by using the Run as Administrator option, and then try running the script again.

12+ CategoryInfo          : PermissionDenied: (StackOverflow.ps1:String) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : ScriptRequiresElevation 相关讨论 不幸的是,如果shell没有管理权限,它只会使脚本失败并出现错误。它本身不会提升。 它使PowerShell2.0上的脚本失败。太愚蠢了。我从来没有用过它。这不好。他们在想什么? PS3似乎给出了建议的错误。我得到了Parameter RunAsAdministrator requires an argument。@我不相信他们总是在想。

乔纳森和肖伊·利维发布的密码对我不起作用。

请在下面找到工作代码:

1234567891011If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator")) {   #"No Administrative rights, it will display a popup window asking user for Admin rights" $arguments ="& '" + $myinvocation.mycommand.definition +"'" Start-Process"$psHome\powershell.exe" -Verb runAs -ArgumentList $arguments break } #"After user clicked Yes on the popup, your file will be reopened with Admin rights" #"Put your code here" 相关讨论 非常有用和实用的解决方案:只是按照我的脚本来处理,它就可以工作了。 @Abatonime你如何指出容易忽略的差异,而不是为了读者的利益?老实说,这一变化不值得对另一个答案发表评论。

您需要使用管理权限重新运行该脚本,并检查该脚本是否在该模式下启动。下面我编写了一个脚本,它有两个功能:doelevatedOperations和dostandardOperations。您应该将需要管理权限的代码放在第一个代码中,将标准操作放在第二个代码中。isrunasadmin变量用于标识管理模式。

我的代码是从Microsoft脚本中简化的摘录,该脚本在您为Windows应用商店应用程序创建应用程序包时自动生成。

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960param(     [switch]$IsRunAsAdmin = $false ) # Get our script path $ScriptPath = (Get-Variable MyInvocation).Value.MyCommand.Path # # Launches an elevated process running the current script to perform tasks # that require administrative privileges.  This function waits until the # elevated process terminates. # function LaunchElevated {     # Set up command line arguments to the elevated process     $RelaunchArgs = '-ExecutionPolicy Unrestricted -file"' + $ScriptPath + '" -IsRunAsAdmin'     # Launch the process and wait for it to finish     try     {         $AdminProcess = Start-Process"$PsHome\PowerShell.exe" -Verb RunAs -ArgumentList $RelaunchArgs -PassThru     }     catch     {         $Error[0] # Dump details about the last error         exit 1     }     # Wait until the elevated process terminates     while (!($AdminProcess.HasExited))     {         Start-Sleep -Seconds 2     } } function DoElevatedOperations {     Write-Host"Do elevated operations" } function DoStandardOperations {     Write-Host"Do standard operations"     LaunchElevated } # # Main script entry point # if ($IsRunAsAdmin) {     DoElevatedOperations } else {     DoStandardOperations }

您还可以强制应用程序以管理员身份打开。当然,如果您有管理员帐户。

找到文件,右键单击>属性>快捷方式>高级并选中以管理员身份运行

然后单击确定。

相关讨论 你如何编写这个脚本? 简单快速的解决方案。

加上我的2美分。我的基于网络会话的简单版本在Windows7/Windows10中一直有效。为什么过于复杂化?

1if (!(net session)) {$path = "& '" + $myinvocation.mycommand.definition +"'" ; Start-Process powershell -Verb runAs -ArgumentList $path ; exit}

只需添加到脚本顶部,它将以管理员身份运行。

相关讨论 显示"访问被拒绝"后,是否有方法向用户发送消息?

C:\Users\"username"\AppData oaming\Microsoft\Windows\Start Menu\Programs\Windows PowerShell是PowerShell的快捷方式所在地。它也会转到另一个位置来调用实际的"exe"(%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe)。

由于PowerShell是在权限相关时由用户配置文件驱动的;如果您的用户名/配置文件具有执行某项操作的权限,那么在PowerShell中,您通常也可以执行该操作。也就是说,您可以更改位于用户配置文件下的快捷方式,例如C:\Users\"username"\AppData oaming\Microsoft\Windows\Start Menu\Programs\Windows PowerShell。

右键单击并单击"属性"。单击"快捷方式"选项卡下的"高级"按钮,该选项卡位于其他两个按钮"打开文件位置"和"更改图标"右侧相邻的"注释"文本字段正下方。

选中"以管理员身份运行"复选框。单击ok,然后单击apply和ok。再次右键单击位于C:\Users\"username"\AppData oaming\Microsoft\Windows\Start Menu\Programs\Windows PowerShell中标记为"Windows PowerShell"的图标,然后选择"Pin to Start menu/taskbar"。

现在,无论何时单击该图标,它都会调用UAC进行升级。选择"是"后,您将注意到PowerShell控制台已打开,屏幕顶部将标记为"管理员"。

再往前走一步…您可以右键单击Windows PowerShell配置文件位置中的同一图标快捷方式,并指定一个键盘快捷方式,该快捷方式将执行与单击最近添加的图标完全相同的操作。因此,如果它说"快捷键",则输入键盘键/按钮组合,如:ctrl+alt+pp(用于PowerShell)。单击apply和ok。

现在,您所要做的就是按下您分配的按钮组合,您将看到UAC被调用,选择"是"后,您将看到一个PowerShell控制台出现,标题栏上显示"管理员"。

这种行为是设计出来的。由于Microsoft真的不希望.ps1文件成为最新的电子邮件病毒,因此存在多个安全层。有些人认为这与任务自动化的概念背道而驰,这是公平的。vista+安全模型是为了"去自动化"一些事情,从而使用户可以接受它们。

但是,我怀疑如果您以提升的方式启动PowerShell本身,它应该能够运行批处理文件,而无需再次请求密码,直到您关闭PowerShell。

这里的许多答案都很接近,但比需要的工作要多一些。

创建脚本的快捷方式并将其配置为"以管理员身份运行":

创建快捷方式。 右击快捷键,打开Properties...。 从

另一个更简单的解决方案是,您也可以右键单击"c:windowssystem32cmd.exe"并选择"以管理员身份运行",然后您可以以管理员身份运行任何应用程序,而无需提供任何密码。

@pgk和@andrew odri的答案的问题是,当您有脚本参数时,特别是当它们是必需的时。您可以使用以下方法解决此问题:

用户右键单击.ps1文件并选择"使用PowerShell运行":通过输入框向他询问参数(这比使用helpmessage参数属性要好得多); 用户通过控制台执行脚本:允许他传递所需的参数,并让控制台强制他通知必需的参数。

下面是如果脚本具有computername和port强制参数,代码将如何处理:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798[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

我正在使用下面的解决方案。它通过转录功能处理stdout/stderr,并将退出代码正确传递给父进程。您需要调整转录路径/文件名。

123456789101112131415161718192021222324If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator")) {   echo"* Respawning PowerShell child process with elevated privileges"   $pinfo = New-Object System.Diagnostics.ProcessStartInfo   $pinfo.FileName ="powershell"   $pinfo.Arguments ="& '" + $myinvocation.mycommand.definition +"'"   $pinfo.Verb ="RunAs"   $pinfo.RedirectStandardError = $false   $pinfo.RedirectStandardOutput = $false   $pinfo.UseShellExecute = $true   $p = New-Object System.Diagnostics.Process   $p.StartInfo = $pinfo   $p.Start() | Out-Null   $p.WaitForExit()   echo"* Child process finished"   type"C:/jenkins/transcript.txt"   Remove-Item"C:/jenkins/transcript.txt"   Exit $p.ExitCode } Else {   echo"Child process starting with admin privileges"   Start-Transcript -Path"C:/jenkins/transcript.txt" } # Rest of your script goes here, it will be executed with elevated privileges

我找到了一种方法…

创建批处理文件以打开脚本:

12@echo off START"""C:\Scripts\ScriptName.ps1"

然后在桌面上创建一个快捷方式,比如说(右键单击新建->快捷方式)。

然后将其粘贴到以下位置:

12C:\Windows\System32 unas.exe /savecred /user:*DOMAIN*\*ADMIN USERNAME* C:\Scripts\BatchFileName.bat

第一次打开时,必须输入一次密码。然后将其保存在Windows凭据管理器中。

之后,您就可以以管理员身份运行,而无需输入管理员用户名或密码。

相关讨论 /savecred不安全!

除Shay Levy的回答外,按照以下设置(仅一次)

使用管理员权限启动PowerShell。 遵循堆栈溢出问题PowerShell说"在此系统上禁用脚本的执行"。 例如,将.ps1文件放在任何PATH文件夹中。WindowsSystem32文件夹

安装后:

按WIN+R 调用powershell Start-Process powershell -Verb runAs 。

现在您可以在一个命令行中运行所有内容。上述功能适用于Windows 8基本64位。

我发现最可靠的方法是将它包装在一个自提升的.bat文件中:

123456789101112@echo off NET SESSION 1>NUL 2>NUL IF %ERRORLEVEL% EQU 0 GOTO ADMINTASKS CD %~dp0 MSHTA"javascript: var shell = new ActiveXObject('shell.application'); shell.ShellExecute('%~nx0', '', '', 'runas', 0); close();" EXIT :ADMINTASKS powershell -file"c:\users\joecoder\scripts\admin_tasks.ps1" EXIT

.bat检查您是否已经是管理员,并在需要时以管理员身份重新启动脚本。它还可以防止外来的"cmd"窗口打开,并将ShellExecute()的第4个参数设置为0。

相关讨论 回答很好,但我尝试了,但没有成功(并退出了我调用它的命令行)。我这样修正了:我把第一个EXIT改为GOTO :EOF,删除了第二个。另外,cd %~dp0应该是cd /d %~dp0并在@echo off之后发出第一个命令。这样,您也不需要EDOCX1的绝对路径(7),只需将其放在与.bat相同的文件夹中即可。如果需要查看结果,请将第4个参数更改为1。 您运行的是哪个O/S和版本? Windows 7 SP1旗舰版。我在C:中有系统,但在D:中也有数据和可移植的应用程序,主要是(和其他几个驱动器)。顺便说一句。。。如果程序/脚本使用参数呢?什么是mshta命令? 我想你可能在测试中出错了,因为脚本工作得很好。顺便说一下,它的设计是为了退出调用cmd的过程,这样就不会"修复"它,但我很高兴您能够根据需要修改它。 好的,如果你想退出cmd(我没有)。但是对于其他的变化,我认为是修正的,因为我的版本对我们都适用,而你的版本对我不适用,也就是说,我的版本更通用(解决不同驱动器的情况)。总之,一个非常聪明的方法。

我以前没见过自己的方法,所以试试看。它更容易跟随,而且占地面积更小:

123if([bool]([Security.Principal.WindowsIdentity]::GetCurrent()).Groups -notcontains"S-1-5-32-544") {     Start Powershell -ArgumentList"& '$MyInvocation.MyCommand.Path'" -Verb runas     }

很简单,如果使用管理员权限调用当前PowerShell会话,则获取当前标识时,组中会显示管理员组已知的SID。即使帐户是该组的成员,SID也不会出现,除非用提升的凭据调用该进程。

几乎所有这些答案都是微软本·阿姆斯特朗广受欢迎的方法的一个变种,即如何在不真正了解实际情况的情况下完成它,以及如何模仿相同的程序。

要将命令输出附加到包含当前日期的文本文件名中,可以执行以下操作:

12$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 }

这是一个澄清…

PowerShell runas/savecred凭据"不安全",已尝试,并将管理员标识和密码添加到凭据缓存中,可在其他地方使用oops!。如果您这样做了,我建议您检查并删除条目。

请检查您的程序或代码,因为Microsoft策略是,如果没有UAC(入口点)以管理员身份执行程序,则不能在同一代码blob中混合用户和管理代码。这将是Linux上的sudo(相同的东西)。

UAC有3种类型,看不见,一个提示或程序清单中生成的入口点。它不会提升程序,因此如果没有UAC,它需要管理员,它将失败。UAC虽然作为一个管理员的需求是好的,但是它可以防止代码在没有身份验证的情况下执行,并且可以防止混合代码场景在用户级别执行。

结果证明这太容易了。你所要做的就是以管理员的身份运行一个命令。然后键入explorer.exe并按Enter。这将打开Windows资源管理器。现在右键单击要运行的PowerShell脚本,选择"使用PowerShell运行",它将以管理员模式在PowerShell中启动。

它可能会要求您启用策略以运行,键入y并按Enter。现在脚本将以管理员身份在PowerShell中运行。如果一切都是红色的,那意味着你的政策还没有生效。然后再试一次,它会工作得很好。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3