2017-06-21 66 views
2

我想檢查PowerShell是否有管理權限。這很簡單,工作正常,但我的問題是,PowerShell不會在腳本打開新實例後繼續執行新腳本。新的實例在System32中運行。重新啓動PowerShell的管理權限,並繼續使用當前腳本

的新實例只是表明:PS C:\windows\system32>

是否也可以關閉第二個實例啓動後運行的第一個實例?

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

這段代碼運行在新的PowerShell進程相同的腳本和退出老shell.Script作用域是必要的myinvocation(從一個函數,調用時),並增加了一個退出函數調用:-):

 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)你可以殺死運行過程遲早

$process = Get-Process | ? {$_.name -like '*powersh*'} 
    #(use -le or -ge) 
    if ($process[0].StartTime -le $process[1].StartTime) 
    { Stop-Process $process[1]} 

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 
相關問題