
Linux에 대한 대안을 찾을 수 없기 때문에sudo
고도 명령을 수행하는 데 다음과 같은 질문이 있습니다.
권한 상승이 필요한 PowerShell 함수를 어떻게 정의하나요? UAC 프롬프트를 의미합니다.
예를 들어, 그러한 기능은 다음과 같습니다:
function system-check {
SFC /ScanNow
}
체계:
윈도우 8.1 프로 64비트
파워셸:
Major Minor Build Revision ----- ----- ----- -------- 5 0 10586 117
편집1:
100% 이해하기 쉽도록 다시 설명하겠습니다.
- 사용자로 PowerShell을 실행합니다.
- 앞서 언급한 기능을 실행합니다.
system-check
- 명령을 실행할 수 있도록 기능을 향상시키고 싶습니다. UAC 프롬프트가 나타나기를 원합니다.
답변1
관리자 창에서 특정 명령을 실행하려면 다음을 수행하십시오.
Start-Process -FilePath powershell.exe -ArgumentList {$ScriptBlock} -verb RunAs
예를 들어:
Start-Process -FilePath powershell.exe -ArgumentList {
SFC /scannow
} -verb RunAs
관리자 창에서 특정 스크립트를 실행하려면 다음을 수행하세요.
Start-Process powershell -ArgumentList '-noprofile -file MyScript.ps1' -verb RunAs
UAC를 묻는 전체 PowerShell 세션을 실행하려면 다음을 수행합니다.
Start-Process powershell.exe -Verb runAs
현재 창이 상승된 권한으로 실행 중인 경우 $True 또는 $False를 반환하는 함수:
function isadmin
{
#Returns true/false
([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
}
스크립트가 관리자 권한으로만 실행되도록 하려면 다음을 시작 부분에 추가하세요.
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
Echo "This script needs to be run As Admin"
Break
}
PowerShell v4.0에서는 #Requires 문을 사용하여 위의 내용을 단순화할 수 있습니다.
#Requires -RunAsAdministrator
원천:높은 권한으로 실행