使用管理员权限重新启动powershell并继续使用当前脚本
我正在尝试检查powershell是否具有管理员权限。这非常简单,工作正常,但我的问题是powershell在打开新实例后不会在新实例中继续使用脚本。新实例在System32中运行 新实例只显示:使用管理员权限重新启动powershell并继续使用当前脚本,powershell,Powershell,我正在尝试检查powershell是否具有管理员权限。这非常简单,工作正常,但我的问题是powershell在打开新实例后不会在新实例中继续使用脚本。新实例在System32中运行 新实例只显示:PS C:\windows\system32> 是否也可以关闭在第二个实例启动后运行的第一个实例 function checkRights { $identity = [System.Security.Principal.WindowsIdentity]::GetCurrent() $princ = N
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;
}
}
此代码段在新的powershell进程中运行相同的脚本,并退出旧的shell。myinvocation(从函数调用时)需要脚本作用域,并添加了退出函数调用:-):
不太清楚你想要什么,但据我所知(IMHO): 1) 您迟早可以终止正在运行的进程
$process = Get-Process | ? {$_.name -like '*powersh*'}
#(use -le or -ge)
if ($process[0].StartTime -le $process[1].StartTime)
{ Stop-Process $process[1]}
2) 您可以使用参数:
工作目录
此参数将在新位置运行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
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