由于我找不到 Linux 的sudo提升命令的任何替代方法,因此我有以下问题:
如何定义需要提升的 PowerShell 函数?我的意思是 UAC 提示。
说,这样的函数如下:
function system-check {
SFC /ScanNow
}
Run Code Online (Sandbox Code Playgroud)
系统:
Windows 8.1 专业版 64 位
电源外壳:
Run Code Online (Sandbox Code Playgroud)Major Minor Build Revision ----- ----- ----- -------- 5 0 10586 117
编辑1:
为了 100% 可以理解,让我改写一下:
system-checkAsh*_*ton 37
Start-Process -FilePath powershell.exe -ArgumentList {$ScriptBlock} -verb RunAs
Run Code Online (Sandbox Code Playgroud)
例如:
Start-Process -FilePath powershell.exe -ArgumentList {
SFC /scannow
} -verb RunAs
Run Code Online (Sandbox Code Playgroud)
Start-Process powershell -ArgumentList '-noprofile -file MyScript.ps1' -verb RunAs
Run Code Online (Sandbox Code Playgroud)
Start-Process powershell.exe -Verb runAs
Run Code Online (Sandbox Code Playgroud)
function isadmin
{
#Returns true/false
([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
}
Run Code Online (Sandbox Code Playgroud)
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
Echo "This script needs to be run As Admin"
Break
}
Run Code Online (Sandbox Code Playgroud)
#Requires -RunAsAdministrator
Run Code Online (Sandbox Code Playgroud)
来源:以提升的权限运行
| 归档时间: |
|
| 查看次数: |
14311 次 |
| 最近记录: |