Reputation: 43
i am trying to check if powershell has admin rights. That is pretty simple and works fine but my problem is that powershell doesn't continue in the new instance with the script after it opens the new instance. The new instance runs in System32.
The new instance just shows: PS C:\windows\system32>
Is it also possible to close the first instance that is running after the second instance started?
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;
}
}
Upvotes: 4
Views: 2467
Reputation: 75
Not quite clear what you want, but as far as I understood it (IMHO):
1) You can kill the process that running sooner or later
$process = Get-Process | ? {$_.name -like '*powersh*'}
#(use -le or -ge)
if ($process[0].StartTime -le $process[1].StartTime)
{ Stop-Process $process[1]}
2) You can use param:
WorkingDirectory
This parameter will running PS in the new location:
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
Upvotes: 1
Reputation: 4059
This snippet runs the same script in the new powershell process and exits the older shell.Script scoping was necessary for myinvocation (when called from a funcion) and added an exit function call :-) :
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)
}
Upvotes: 2