Good day
I have two scripts to run and I need help
The first one is to do some checks and deploy two .ps1 files in a folder. This code to deploy the file is
#Create Remove-Gateway.ps1 and Recover-Gateway.ps1 in the Script folder
$remove = '
$newGateway = "0.0.0.0"
$destination = "0.0.0.0/0"
$index = (Get-NetRoute -DestinationPrefix "0.0.0.0/0" -NextHop "192.168.*").ifIndex
$gateway = (Get-NetRoute -InterfaceIndex $index -DestinationPrefix $destination).NextHop
$gateway | Out-File -FilePath $env:PUBLIC"\gateway.txt"
#Remove actual gateway and set new route
Remove-netroute -InterfaceIndex $index -NextHop $gateway -Confirm:$false
New-NetRoute -InterfaceIndex $index -NextHop $newGateway -DestinationPrefix $destination -Confirm:$false '
$remove | Out-File $folderPath"\Remove-Gateway.ps1"
'
$recover = '
#Get 0.0.0.0 IP Gateway
$oldGateway = "0.0.0.0"
$destination = "0.0.0.0/0"
$index = (Get-NetRoute -DestinationPrefix $destination -NextHop $oldGateway).ifIndex
$oldGateway = (Get-NetRoute -InterfaceIndex $index -DestinationPrefix $destination).NextHop
#Get old Gateway
$file = $env:Public+"\gateway.txt"
$newgateway = Get-Content $file
#Recover old gateway
Remove-netroute -InterfaceIndex $index -NextHop $oldgateway -DestinationPrefix $destination -Confirm:$false
New-NetRoute -InterfaceIndex $index -NextHop $newGateway -DestinationPrefix $destination -Confirm:$false
Remove-Item -Path $env:PUBLIC"\gateway.txt"'
$recover | Out-File $folderPath"\Recover-Gateway.ps1"
'
Once those files are in the folder, I want to run them with admin credentials (the user who run the script will not have admin permissions).
I'm trying this:
$folderPath = "C:\Scripts"
$removegw = "$folderPath"\Remove-Gateway.ps1"
$recovergw = "$folderPath"\Recover-Gateway.ps1"
$user = "User"
$password= "Complex Password"
$credential = New-Object System.Management.Automation.PSCredential -ArgumentList @($user,(ConvertTo-SecureString -String $password -AsPlainText -Force))
Start-Process Powershell.exe -Credential $credential -ArgumentList "-noprofile -file $removegw -verb RunAs"
But the last part where i'm calling the script is not working
I was doing some tests but instead of modifying the gateway I was disabling the NetAdapter and was working:
powershell -Command "Start-Process powershell.exe -ArgumentList '-ExecutionPolicy Bypass -NoExit -Command Disable-Netadapter -name Wi-Fi -confirm:`$false`"' -Verb RunAs"
Do you know how can I elevate the script??