Vista以降のUAC昇格ですが、PowerShellモジュールの多くは昇格(Elevated)されていないと実行出来ない場合もあります。 この辺は、linuxに近くなってきてセキュリティ面ではいいものの、スクリプト実行時には困ったります。
そこで、サクッとelevatedされたかどうかのチェックをやってみましょうというネタ。
ソースとサンプル
GitHubで。 https://github.com/guitarrapc/PowerShellUtil/blob/master/AdminElevated/Test-Elevated.ps1
現在のユーザー情報を取得する
簡単ですね。
[Security.Principal.WindowsIdentity]::GetCurrent()
昇格されたかどうかを確認する
先ほどの情報を$userに入れて、実行状態がadministartorロールかどうか確認します。
$user = [Security.Principal.WindowsIdentity]::GetCurrent() (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
function化してみる
# Requires -Version 3.0 function Test-Elevated { <# .SYNOPSIS Retrieve elavated status of PowerShell Console. .DESCRIPTION Test-Elevated will check shell was elevated is required for some operations access to system folder, files and objects. .NOTES Author: guitarrapc Date: June 17, 2013 .OUTPUTS bool .EXAMPLE C:\PS> Test-Elevated true .EXAMPLE C:\PS> Test-Elevated false #> $user = [Security.Principal.WindowsIdentity]::GetCurrent() (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) }
実行時の状態チェックに利用できます。
Write-Verbose "checking is this user elevated or not." if(-not(Test-Elevated)) { $warningMessage = "To run this Cmdlet on UAC 'Windows Vista, 7, 8, Windows Server 2008, 2008 R2, 2012 and later versions of Windows' must start an elevated PowerShell console." Write-Warning $warningMessage Read-Host "Press any key." # exit を置けば終了するし てきとーに }