1

在我的 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 和网络掩码,这让我感到困惑。

4

2 回答 2

2

您是正确的,设置静态 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
于 2017-07-19T16:16:10.797 回答
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
于 2017-07-20T07:11:34.027 回答