powershell 使用PowerShell配置IP地址(在Windows上)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell 使用PowerShell配置IP地址(在Windows上)相关的知识,希望对你有一定的参考价值。

# Edit by: Ngoc Khuong (me@ngockhuong.com)
# Website: https://ngockhuong.com

########################## Network Information ##########################

param (
	[string]$Name = "Wi-Fi", # Ethernet / Wi-Fi
    [string]$Type = "DHCP", # Static / DHCP
	[IPAddress]$IP = "192.168.1.2",
    [string] $CIDR = 24, # This means subnet mask = 255.255.255.0,
	[string]$Gateway = "192.168.1.1",
	[string]$Dns = "8.8.8.8,8.8.4.4",
	[string]$IPType = "IPv4"
    
)

#########################################################################

# Get the ID and security principal of the current user account
$myWindowsID = [System.Security.Principal.WindowsIdentity]::GetCurrent();
$myWindowsPrincipal = New-Object System.Security.Principal.WindowsPrincipal($myWindowsID);

# Get the security principal for the administrator role
$adminRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator;

# Check to see if we are currently running as an administrator
if ($myWindowsPrincipal.IsInRole($adminRole))
{
    # We are running as an administrator, so change the title and background colour to indicate this
    $Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)";
    $Host.UI.RawUI.BackgroundColor = "DarkBlue";
    Clear-Host;
}
else {
    # We are not running as an administrator, so relaunch as administrator

    # Create a new process object that starts PowerShell
    $newProcess = New-Object System.Diagnostics.ProcessStartInfo "PowerShell";

    # Specify the current script path and name as a parameter with added scope and support for scripts with spaces in it's path
    $newProcess.Arguments = "& '" + $script:MyInvocation.MyCommand.Path + "'"

    # Indicate that the process should be elevated
    $newProcess.Verb = "runas";

    # Start the new process
    [System.Diagnostics.Process]::Start($newProcess);

    # Exit from the current, unelevated, process
    Exit;
}

################### Run your code that needs to be elevated here... ###################

# Retrieve the network adapter that you want to configure
$adapter = Get-NetAdapter | ? {$_.Name -eq $Name}

if ($Type -eq "Static") {

    # Remove any existing IP, gateway from our ipv4 adapter
    If (($adapter | Get-NetIPConfiguration).IPv4Address.IPAddress) {
        Write-Host "Removing existing IP"
        $adapter | Remove-NetIPAddress -AddressFamily $IPType -Confirm:$false
    }

    If (($adapter | Get-NetIPConfiguration).Ipv4DefaultGateway) {
        Write-Host "Removing existing gateway"
        $adapter | Remove-NetRoute -AddressFamily $IPType -Confirm:$false
    }

    # Configure the IP address and default gateway
    Write-Host "Configuring new IP"

    $adapter | New-NetIPAddress `
        -AddressFamily $IPType `
        -IPAddress $IP `
        -PrefixLength $CIDR `
        -DefaultGateway $Gateway

    # Configure the DNS client server IP addresses
    Write-Host "Configuring new gateway"
    $adapter | Set-DnsClientServerAddress -ServerAddresses $DNS
} else {
    $interface = $adapter | Get-NetIPInterface -AddressFamily $IPType

    If ($interface.Dhcp -eq "Disabled") {
        # Remove existing gateway
        Write-Host "Removing existing gateway"
        If (($interface | Get-NetIPConfiguration).Ipv4DefaultGateway) {
            $interface | Remove-NetRoute -Confirm:$false
        }

        # Enable DHCP
        Write-Host "Enabling DHCP on interface"
        $interface | Set-NetIPInterface -DHCP Enabled

        # Configure the  DNS Servers automatically
        Write-Host "Enabling automatic DNS"
        $interface | Set-DnsClientServerAddress -ResetServerAddresses
    }
}
Write-Host "Restarting adapter"
$adapter | Restart-NetAdapter
Start-Sleep -s 5

以上是关于powershell 使用PowerShell配置IP地址(在Windows上)的主要内容,如果未能解决你的问题,请参考以下文章

Powershell 远程配置文件

使用 powershell 将 Microsoft 表单 webpart 配置到现代页面

使用 PowerShell 自动化 Cygwin 配置

Powershell 加载自定义程序集配置文件

PowerShell 应用程序配置

如何使用 Powershell 配置新的 DCOM RunAs 用户 [重复]