2017-06-21 3 views
2

私はpowershellに管理者権限があるかどうかを確認しようとしています。それは非常にシンプルで、うまく動作しますが、私の問題は、新しいインスタンスを開いた後、スクリプトで新しいインスタンスでpowershellを続行しないということです。新しいインスタンスはSystem32で実行されます。admin権限でpowershellを再起動し、現在のスクリプトを続行してください

新しいインスタンスだけを示しています。PS C:\windows\system32>

は、第2のインスタンスが起動した後に実行されている最初のインスタンスを閉じることも可能ですか?

function checkRights { 
$identity = [System.Security.Principal.WindowsIdentity]::GetCurrent() 
$princ = New-Object System.Security.Principal.WindowsPrincipal($identity) 
    if(!$princ.IsInRole(` 
    [System.Security.Principal.WindowsBuiltInRole]::Administrator)) 
    { 
    $powershell = [System.Diagnostics.Process]::GetCurrentProcess() 
    $psi = New-Object System.Diagnostics.ProcessStartInfo $powerShell.Path 
    $installPath = $MyInvocation.MyCommand.Path 
    $script = $installPath 
    $prm = $script 
    foreach($a in $args) { 
     $prm += ' ' + $a 
    } 
     $psi.Arguments = $prm 
     $psi.Verb = "runas" 
    [System.Diagnostics.Process]::Start($psi) | Out-Null 
    return; 
    } 
} 

答えて

1

このスニペットは、古いshell.Scriptスコープは(funcionから呼び出された場合)myinvocationのために必要だったと出口関数呼び出し:-)を追加した新しいPowerShellプロセスで同じスクリプトを実行し、終了します

 function checkRights() { 
$identity = [System.Security.Principal.WindowsIdentity]::GetCurrent() 
$princ = New-Object System.Security.Principal.WindowsPrincipal($identity) 
    if(!$princ.IsInRole(` 
    [System.Security.Principal.WindowsBuiltInRole]::Administrator)) 
    { 
    $powershell = [System.Diagnostics.Process]::GetCurrentProcess() 
    $psi = New-Object System.Diagnostics.ProcessStartInfo $powerShell.Path 
     $psi.Arguments = '-file ' + $script:MyInvocation.MyCommand.Path 
     $psi.Verb = "runas" 
    [System.Diagnostics.Process]::Start($psi) | Out-Null 
    return $false 
    } 
    else{ 
    return $true 
    } 
} 
$rights = checkrights 
$rights 
if($rights){ 
"opened in admin rights" 
#for pausing 
Read-Host 

}else 
{ 
"no admin rights ,trying to open in admin rights" 
[Environment]::Exit(0) 
} 
1
あなたが欲しいものを非常に明確ではない

、しかし限り、私はそれを理解されるように(私見):

1)あなたは遅かれ早かれ

実行中のプロセスを殺すことができます210

2)あなたはのparamを使用することができます。

WORKINGDIRECTORY

このパラメータは、新しい場所にPSを実行します:

function checkRights { 
$identity = [System.Security.Principal.WindowsIdentity]::GetCurrent() 
$princ = New-Object System.Security.Principal.WindowsPrincipal($identity) 
    if(!$princ.IsInRole(` 
    [System.Security.Principal.WindowsBuiltInRole]::Administrator)) 
    { 
    $powershell = [System.Diagnostics.Process]::GetCurrentProcess() 
    $psi = New-Object System.Diagnostics.ProcessStartInfo $powerShell.Path 
    $installPath = $MyInvocation.MyCommand.Path 
    $script = $installPath 
    $prm = $script 
    foreach($a in $args) { 
     $prm += ' ' + $a 
    } 
     $psi.Arguments = $prm 
     $psi.Verb = "runas" 

#if ($dir.Attributes -eq "Directory") { 

$process = Get-Process | ? {$_.name -like '*powersh*'} 

if (($process).Count -eq 1) 
{ 
$psi.WorkingDirectory = "C:\delinf" 
[System.Diagnostics.Process]::Start($psi) | Out-Null 
return; 
} 

elseif (($process).Count -eq 2) 
{ 
$psi.WorkingDirectory = "C:\Csharp" 
[System.Diagnostics.Process]::Start($psi) | Out-Null 
return; 
} 
} 
} checkRights 
関連する問題