PowerShell stuph...: Difference between revisions

From munkjensen.net/wiki
mNo edit summary
No edit summary
Line 1: Line 1:
= Scripts i (ab)use =
= Scripts i (ab)use =
<blockquote>
<blockquote>
<div class="toccolours mw-collapsible mw-collapsed">
{| class="mw-collapsible mw-collapsed wikitable"
==== Config NIC as DHCP Client ====
! Config NIC as DHCP Client
<div class="mw-collapsible-content">
|-
 
| <pre>$IPType = "IPv4"
<pre>$IPType = "IPv4"
$adapter = Get-NetAdapter | ? {$_.Status -eq "up"}
$adapter = Get-NetAdapter | ? {$_.Status -eq "up"}
$interface = $adapter | Get-NetIPInterface -AddressFamily $IPType
$interface = $adapter | Get-NetIPInterface -AddressFamily $IPType
Line 21: Line 20:
     $interface | Set-DnsClientServerAddress -ResetServerAddresses
     $interface | Set-DnsClientServerAddress -ResetServerAddresses
}</pre>
}</pre>
</div>
|}
</div>
{| class="mw-collapsible mw-collapsed wikitable"
! Config NIC with static IP
|-
| $IP = "172.29.106.41"
$MaskBits = 27 # 24 = 255.255.255.0 | 27 = 255.255.255.224
$Gateway = "172.29.106.62"
$Dns = "172.29.10.69"
$IPType = "IPv4"
 
# Retrieve the network adapter that you want to configure
$adapter = Get-NetAdapter | ? {$_.Status -eq "up"}
 
# Remove any existing IP, gateway from our ipv4 adapter
If (($adapter | Get-NetIPConfiguration).IPv4Address.IPAddress) {
    $adapter | Remove-NetIPAddress -AddressFamily $IPType -Confirm:$false
}
 
If (($adapter | Get-NetIPConfiguration).Ipv4DefaultGateway) {
    $adapter | Remove-NetRoute -AddressFamily $IPType -Confirm:$false
}
 
# Configure the IP address and default gateway
$adapter | New-NetIPAddress `
    -AddressFamily $IPType `
    -IPAddress $IP `
    -PrefixLength $MaskBits `
    -DefaultGateway $Gateway
 
# Configure the DNS client server IP addresses
$adapter | Set-DnsClientServerAddress -ServerAddresses $DNS
|}
</blockquote>
</blockquote>
[[Category:Windows]]
[[Category:Windows]]

Revision as of 12:46, 16 November 2016

Scripts i (ab)use

Config NIC as DHCP Client
$IPType = "IPv4"
$adapter = Get-NetAdapter | ? {$_.Status -eq "up"}
$interface = $adapter | Get-NetIPInterface -AddressFamily $IPType

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

    # Enable DHCP
    $interface | Set-NetIPInterface -DHCP Enabled

    # Configure the  DNS Servers automatically
    $interface | Set-DnsClientServerAddress -ResetServerAddresses
}
Config NIC with static IP
$IP = "172.29.106.41"

$MaskBits = 27 # 24 = 255.255.255.0 | 27 = 255.255.255.224 $Gateway = "172.29.106.62" $Dns = "172.29.10.69" $IPType = "IPv4"

  1. Retrieve the network adapter that you want to configure

$adapter = Get-NetAdapter | ? {$_.Status -eq "up"}

  1. Remove any existing IP, gateway from our ipv4 adapter

If (($adapter | Get-NetIPConfiguration).IPv4Address.IPAddress) { $adapter | Remove-NetIPAddress -AddressFamily $IPType -Confirm:$false }

If (($adapter | Get-NetIPConfiguration).Ipv4DefaultGateway) { $adapter | Remove-NetRoute -AddressFamily $IPType -Confirm:$false }

  1. Configure the IP address and default gateway

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

  1. Configure the DNS client server IP addresses

$adapter | Set-DnsClientServerAddress -ServerAddresses $DNS