Last active
August 11, 2021 08:40
-
-
Save stknohg/2222bb0f68282d2f8ca9 to your computer and use it in GitHub Desktop.
昇格したPowerShellコンソールおよびISEを起動させるファンクション
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.Synopsis | |
昇格したPowerShellコンソールおよびISEを起動します。 | |
.DESCRIPTION | |
昇格したPowerShellコンソールおよびISEを起動します。 | |
コンソールの場合、元のコンソールからカレントパスを引き継ぎます。 | |
ISEの場合、現在開いているファイルを引き継ぎます。 | |
.PARAMETER ExitCurrentProcess | |
昇格したPowerShellコンソールおよびISE起動後に元のプロセスを終了するか否かを指定します。 | |
.PARAMETER Wait | |
PowerShellコンソールおよびISE起動後に元のプロセスで終了待ちをするか否かを指定します。 | |
.PARAMETER Credential | |
昇格時に認証情報を利用する場合にその認証情報を指定します。 | |
このパラメーターはUACが無効な場合や、ServerCoreで使用してください。 | |
.EXAMPLE | |
Start-ElevatedPowerShell | |
#> | |
function Start-ElevatedPowerShell | |
{ | |
[CmdletBinding()] | |
Param | |
( | |
# 昇格後に元のプロセスを終了するか否か | |
[switch]$ExitCurrentProcess = $true, | |
# 昇格後に元のプロセスで終了待ちをするか否か | |
[switch]$Wait = $false, | |
# 昇格時に認証情報を使う場合の認証情報(基本指定しない。主にServerCore向け。) | |
[PSCredential]$Credential = $null | |
) | |
# 実行ホストのチェック | |
$IsConsole = $Host.Name -eq "ConsoleHost" | |
$IsISE = $Host.Name -eq "Windows PowerShell ISE Host" | |
if( -not ( $IsConsole -or $IsISE ) ) | |
{ | |
Write-Warning "このファンクションはPowerShellコンソールおよびPowerShell ISEでのみ動作します。" | |
return | |
} | |
# 昇格済みかチェック | |
$CurrentUser = [Security.Principal.WindowsIdentity]::GetCurrent() | |
if( (New-Object Security.Principal.WindowsPrincipal $CurrentUser).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) ) | |
{ | |
Write-Warning "このプロセスは既に昇格済みです。" | |
return | |
} | |
# 現在の各種情報を取得 | |
$CurrentProcess = Get-Process -Id $PID | |
# 内部ファンクション(Verb、または認証情報を指定してプロセスを起動する) | |
function ElevateUseVerbOrCredential([string]$FilePath, [string[]]$ArgumentList, [PSCredential]$Credential = $null) | |
{ | |
if( $null -eq $ArgumentList -or "" -eq $ArgumentList ) | |
{ | |
if( $null -eq $Credential ) | |
{ | |
Start-Process -FilePath "$FilePath" -Verb "runas" -Wait:$Wait -ErrorAction Stop | |
} | |
else | |
{ | |
Start-Process -FilePath "$FilePath" -Credential $Credential -Wait:$Wait -ErrorAction Stop | |
} | |
} | |
else | |
{ | |
if( $null -eq $Credential ) | |
{ | |
Start-Process -FilePath "$FilePath" -ArgumentList @($ArgumentList) -Verb "runas" -Wait:$Wait -ErrorAction Stop | |
} | |
else | |
{ | |
Start-Process -FilePath "$FilePath" -ArgumentList @($ArgumentList) -Credential $Credential -Wait:$Wait -ErrorAction Stop | |
} | |
} | |
} | |
# 新たにpowershellコンソールおよびISEを管理者として起動して昇格させる | |
try | |
{ | |
if( $IsConsole ) | |
{ | |
# コンソールの場合はカレントパスを取得 | |
$CurrentPath = (Get-Location).Path | |
# 昇格 | |
ElevateUseVerbOrCredential -FilePath "$($CurrentProcess.Path)" -ArgumentList @("-NoExit", "-Command ""&{Set-Location '$CurrentPath'}""") -Credential $Credential | |
} | |
if( $IsISE ) | |
{ | |
# ISEの場合は現在開いているファイルを取得 | |
$OpenFiles = $psISE.CurrentPowerShellTab.Files | |
$Argument = "" | |
if( $OpenFiles.Count -gt 0 ) | |
{ | |
foreach ( $File in $OpenFiles ) | |
{ | |
if( -not $File.IsSaved ) | |
{ | |
Write-Warning ("保存されていないファイル""{0}""があるため昇格を中断します。" -f $File.DisplayName) | |
return | |
} | |
# | |
if( $Argument -ne "" ) | |
{ | |
$Argument += "," | |
} | |
$Argument += ("""{0}""" -f $File.FullPath) | |
} | |
} | |
# 昇格 | |
ElevateUseVerbOrCredential -FilePath "$($CurrentProcess.Path)" -ArgumentList $Argument -Credential $Credential | |
} | |
} | |
catch [InvalidOperationException] | |
{ | |
Write-Warning "昇格をキャンセルしました。`r`n$($_.Exception.Message)" | |
return | |
} | |
if( $ExitCurrentProcess ) | |
{ | |
exit | |
} | |
} | |
Set-Alias elevate Start-ElevatedPowerShell |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment