0

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??

jjquintero245
  • 49
  • 1
  • 8
  • You must pass `-Verb RunAs` to [`Start-Process`](https://learn.microsoft.com/powershell/module/microsoft.powershell.management/start-process), not to `powershell.exe` – mklement0 Oct 13 '22 at 14:00
  • 1
    [⚠️ **Important**](https://learn.microsoft.com/nl-nl/dotnet/api/system.security.securestring?view=net-6.0#securestring-operations) A **SecureString** object should never be constructed from a **String**, because the sensitive data is already subject to the memory persistence consequences of the immutable **String** class. The best way to construct a **SecureString** object is from a character-at-a-time unmanaged source, such as the **Console.ReadKey** method. – iRon Oct 13 '22 at 14:03
  • 1
    This means that you actually shouldn't use `ConvertTo-SecureString -String $password -AsPlainText -Force`. Besides, it is a security risk to have plain passwords in your script. You should either prompt for this or if it is your own script (that runs only from your computer/account), encrypt it, see: [How to encrypt/hide ClearText password in PowerShell Transcript](https://stackoverflow.com/a/62609833/1701026) – iRon Oct 13 '22 at 14:04
  • While your last command does this, it needlessly calls `powershell.exe` _twice_, and the \`"` before the closing `'` shouldn't be there. – mklement0 Oct 13 '22 at 14:04
  • 1
    If your intent is to ultimately run an elevated command _as a specific user_ (that isn't the current user), [this answer](https://stackoverflow.com/a/43281908/45375) may help - but note that that user will still receive a UAC confirmation prompt. If so, we can close your question as a duplicate. Separately, iRon's security caveats are worth heeding. – mklement0 Oct 13 '22 at 14:23
  • Thanks for your comments mklement0 --> What lines are you saying? iRon --> Thanks for letting me know, this script will be running after start some process but not in my computer, i'll deploy the script in an environment where all the computers will have an admin local account with same credentials, that's why I want to do the automation process Thanks again – jjquintero245 Oct 13 '22 at 14:24

0 Answers0