Я написал функцию о том, что вы пытаетесь сделать
<#
.SYNOPSIS
Creates new powershell consoles
.DESCRIPTION
Used to create new powershell consoles running as same rights unless Elevated is selected in which case it runs as administrator
.EXAMPLE
New-PowershellConsole -Count 2 -Elevated -Exit
.PARAMETER Count
Starts up this many consoles
.PARAMETER Elevated
Starts Consoles as Administrator
.PARAMETER Exit
Closes the current powershell console
#>
function New-PowershellConsole {
param(
[int]$Count = 1,
[switch]$Elevated,
[switch]$Exit
)
if ($(new-object System.Security.Principal.WindowsPrincipal([System.Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator) -or $($Elevated -eq $True)) {
while ($Count -gt 0){
Start-Process powershell -Verb runAs -ArgumentList "-NoExit -Command `"Set-Location $((Get-Location).path)`"";
$Count--
}
} else {
while ($Count -gt 0){
Start-Process powershell -ArgumentList "-NoExit -Command `"Set-Location $((Get-Location).path)`"";
$Count--
}
}
If($Exit){
exit
}
}
Исходя из вашего сообщения, похоже, что вы хотите запустить новую консоль PowerShell как администратор
Start-Process powershell -Verb runAs -ArgumentList "-NoExit";
Я предлагаю, чтобы избежать ошибок, чтобы проверить, если пользователь является администратором
if ($(new-object System.Security.Principal.WindowsPrincipal([System.Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator) -or $($Elevated -eq $True)) {
Start-Process powershell -Verb runAs -ArgumentList "-NoExit";
}