powershellnetwork-programmingwindows-7-embedded

Change IP as user with powershell script on Win 7


On my Windows 7 Embedded machine I want to change the IP address via Powershell script as an user.

For that I added my user to the "Network Configuration Operators" group and wrote the following script.

param(
[string]$Type
)

Write-Host "Networkchanger"

$adapter = Get-WmiObject win32_NetworkAdapterConfiguration -filter "Index = 11"

if($Type -eq "black"){
    Write-Host "Using black"
    $IP = "192.168.1.172"
    $Netmask = "255.255.255.0"
    $Gateway = "192.168.1.1"
    $DNS = "192.168.1.254"
    $adapter.EnableStatic($IP, $NetMask)
    Sleep -Seconds 4
    $adapter.SetGateways($Gateway)
    $adapter.SetDNSServerSearchOrder($DNS)
} else {
    Write-Host "Using rf"
    $adapter.SetDNSServerSearchOrder()
    $adapter.EnableDHCP()
}

The script runs fine as admin, but not as an user. Did I forget to add some rights to the script or user?

Edit: When I click "Run as admin" and use black it works for the first time. After changing it to rf (which works), the black net just changes the Gateway and DNS, but not the IP and Netmask, which confuses me.


Solution

  • I've solved the problem by heavily modifing my Powershell script:

    $id=[System.Security.Principal.WindowsIdentity]::GetCurrent()
    $principal=New-Object System.Security.Principal.WindowsPrincipal($id)
    if(!$principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)) {
    $powershell=[System.Diagnostics.Process]::GetCurrentProcess()
    $psi=New-Object System.Diagnostics.ProcessStartInfo $powershell.Path
    $script=$MyInvocation.MyCommand.Path
    $prm=$script+$Type
    foreach($a in $args) {
      $prm+=' '+$a
    }
    $psi.Arguments=$prm
    $psi.Verb="runas"
    [System.Diagnostics.Process]::Start($psi) | Out-Null
    return;
    }
    
    Write-Host "Networkchanger"
    Write-Host ""
    Write-Host "[0] cancel"
    Write-Host "[1] net1:    10.0.0.172"
    Write-Host "[2] net2:    192.168.178.(172,235,237,248,251)"
    
    $adapter = Get-WmiObject win32_NetworkAdapterConfiguration -filter "Index = 0"
    $loop = 1;
    
    while($loop -eq 1){
        $Type = Read-Host -Prompt '0, 1 OR 2'
        switch($Type){
            0 {
                Write-Host "Cancel Process"
                Sleep -Seconds 3
                exit
            }
            1 {
                Write-Host "Using sb"
                $IP = "10.0.0.172"
                $Netmask = "255.255.255.0"
                $Gateway = "10.0.0.1"
                $DNS = "10.0.0.254"
                $adapter.EnableStatic($IP, $NetMask)
                Sleep -Seconds 4
                $adapter.SetGateways($Gateway)
                $adapter.SetDNSServerSearchOrder($DNS)
                $loop = 0
            }
            2 {
                Write-Host "Using rf"
                $macaddress = $adapter | select -expand macaddress
                Write-Host $macaddress
                $IP = ""
                if ($macaddress -eq "xx:xx:xx:xx:xx:xx"){
                    $IP = "192.168.178.172"
                } elseif ($macaddress -eq "xx:xx:xx:xx:xx:xx") {
                    $IP = "192.168.178.235"
                } elseif ($macaddress -eq "xx:xx:xx:xx:xx:xx") {
                    $IP = "192.168.178.237"
                } elseif ($macaddress -eq "xx:xx:xx:xx:xx:xx") {
                    $IP = "192.168.178.248"
                } elseif ($macaddress -eq "xx:xx:xx:xx:xx:xx") {
                    $IP = "192.168.178.251"
                } else {
                    Write-Host "Mac address not in list"
                    Sleep -Seconds 5
                    exit
                }
                $Netmask = "255.255.255.0"
                $Gateway = "192.168.178.1"
                $DNS = "192.168.178.2","192.168.178.3"
                $adapter.EnableStatic($IP, $NetMask)
                Sleep -Seconds 4
                $adapter.SetGateways($Gateway)
                $adapter.SetDNSServerSearchOrder($DNS)
                $loop = 0
            }
        }
    }
    
    Write-Host "Current IP: "
    ipconfig
    
    Start-Sleep -seconds 5