2017-07-12 2 views
2

昇格されたpowershellセッションを開始すると、マップされたネットワークドライブへの接続やボーナスの変更など、私の素晴らしい上昇力を思い出させるプロンプトの色。昇格したセッションを実行しているときに別のpowershellプロファイルを読み込む方法を教えてください

私は

Import-Module ($PSScripts + "elevated.ps1"); 

を目の前で#Requires -RunAsAdministratorでモジュールを作成し、私のprofile.ps1ファイルに次の行を追加した場合、それは上昇のセッションのための仕事の罰金を行いますが、毎回、私は開きます通常のセッションでは、大きな赤色のエラーメッセージが表示されます。これは、最適ではないものです。

Import-Module : The script 'elevated.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. 
At C:\Users\sdixon.MV\Documents\WindowsPowerShell\profile.ps1:22 char:3 
+ Import-Module ($PSScripts + "elevated.ps1") -ErrorAction silentlyco ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : PermissionDenied: (elevated.ps1:String) [Import-Module], ScriptRequiresException 
    + FullyQualifiedErrorId : ScriptRequiresElevation,Microsoft.PowerShell.Commands.ImportModuleCommand 

これは私が使用してみた場合でも起こります:

Import-Module ($PSScripts + "elevated.ps1") -ErrorAction silentlycontinue; 

は、現在のセッションが上昇しているかどうかを検出する方法はありますか?

答えて

1

次のようにあなたが管理者権限を確認することができますように見えます:

If (([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")){ 
    Import-Module ($PSScripts + "elevated.ps1") 
} Else { 
    Write-Warning 'You do not currently have Administrator rights.' 
} 

出典:https://gallery.technet.microsoft.com/scriptcenter/1b5df952-9e10-470f-ad7c-dc2bdc2ac946

https://blogs.technet.microsoft.com/heyscriptingguy/2011/05/11/check-for-admin-credentials-in-a-powershell-script/

彼はまた、ここにスクリプトギャラリーで利用可能なTest-IsAdmin機能を、書きました

関連する問題