I am trying to code a PowerShell script to self-elevate using a user/pass. The problem is, runas and -Credential can't be used together in Start-Process. I tried running a powershell in a powershell, but it does not work. The idea is to bypass the UAC with user/pass. Does anyone have a suggestion?
EDIT: It's not possible!
$username = "test"
$password = "123456"
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))
# Self-elevate the script if required
if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
if ([int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000) {
Write-Host -ForegroundColor 'Red' "Bypassing UAC..."
Start-Sleep -s 2
$CommandLine = "-File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments
$CommandLine = "Start-Process -FilePath PowerShell.exe -verb runas -ArgumentList " + $CommandLine
Start-Process powershell.exe -Credential $credentials -ArgumentList $CommandLine
Write-Host -ForegroundColor 'Red' "Bypassing UAC..." $CommandLine
Start-Sleep -s 30
Exit
}
}
Write-Host -ForegroundColor 'Green' "Admin mode..."
Write-Host -ForegroundColor 'Gray' "Shell will exit in 15 seconds..."
Start-Sleep -s 15