使用Win 7上的powershell脚本将IP更改为用户

时间:2017-06-16 09:52:27

标签: powershell networking windows-7-embedded

在我的Windows 7 Embedded机器上,我想通过Powershell脚本以用户身份更改IP地址。

为此,我将我的用户添加到"网络配置操作员"分组并编写以下脚本。

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()
}

该脚本可以作为管理员运行,但不能作为用户运行。我忘了为脚本或用户添加一些权限吗?

修改 当我点击"以管理员身份运行"并使用黑色它是第一次使用。将其更改为 rf (有效)后,黑色网络只会更改网关和DNS,但不会更改IP和网络掩码,这会让我感到困惑。

2 个答案:

答案 0 :(得分:1)

您是正确的,设置静态IP地址需要管理员权限。我在Windows Embedded Standard 7图像上执行此操作。基本上,我在桌面上使用“运行方式管理器”创建了一个快捷方式,用于使用特定脚本启动PowerShell。另请注意,启用DHCP不需要此类提升权限,但不会受到影响。

使用netsh命令从PowerShell设置IP地址的方法稍微简单一些。这种方法的好处是你也可以从命令提示符中看到特定的错误。尝试从提升的命令提示符和非提升的命令提示符来回切换。

netsh interface ip set address "${InterfaceName}" static addr=${IPAddr}  mask=${Mask} gateway=${Gateway}
netsh interface ip set address "${InterfaceName}" dhcp

答案 1 :(得分:0)

我通过大量修改我的Powershell脚本解决了这个问题:

$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