Tag: Powershell v3

Replace netsh with Windows PowerShell – Basic network cmdlets

Some days ago I made a blog post about how you can replace diskpart.exe with the new cmdlets in Windows PowerShell v3. Now my next goal was to replace netsh with PowerShell.

Microsoft offers a lot of new cmdlets for networking tasks lets see what we have here. We got a lot of new PowerShell modules which are related to networking.

get-module

Now the two modules which sound really interesting are

NetAdapter
NetTCPIP

lets check which cmdlets are included

Get-Command -Module NetAdapter

net-adpater module

 Get-Command -Module NetTCPIP

Now lets start with NetAdpater.

List all Network Adapters

 Get-NetAdapter

get-netadapter

Just list Wireless Network Adapters

 Get-NetAdapter -Name *Wireless* 

get-netadapter wireless

Get more information VLAN ID, Speed, Connection status

 Get-NetAdapter | ft Name, Status, Linkspeed, VlanID 

get-netadapter information

Get driver information

 Get-NetAdapter | ft Name, DriverName, DriverVersion, DriverInformation, DriverFileName 

get-netadapter driver

Get adapter hardware information. This can be really usefull when you need to know the PCI slot of the NIC.

 Get-NetAdapterHardwareInfo 

get-netadapterhardwareinfo

Disable and Enable a Network Adapter

 Disable-NetAdapter -Name "Wireless Network Connection"
Enable-NetAdapter -Name "Wireless Network Connection" 

disable enable network adapter

Rename a Network Adapter

 Rename-NetAdapter -Name "Wireless Network Connection" -NewName "Wireless" 

rename network adapter

Now with the NetTCPIP PowerShell module we can work with IP settings

Get IP and DNS address information

 Get-NetAdapter -Name "Local Area Connection" | Get-NetIPAddress 

get-ipaddress

Get IP address only

 (Get-NetAdapter -Name "Local Area Connection" | Get-NetIPAddress).IPv4Address 

get-ipaddress only

Get DNS Server Address information

 Get-NetAdapter -Name "Local Area Connection" | Get-DnsClientServerAddress 

get-dnsserver

Set IP Address

 New-NetIPAddress -InterfaceAlias "Wireless" -IPv4Address 10.0.1.95 -PrefixLength "24" -DefaultGateway 10.0.1.1 

or if you want to change a existing IP Address

 Set-NetIPAddress -InterfaceAlias "Wireless" -IPv4Address 192.168.12.25 -PrefixLength "24"

Remove IP Address

 Get-NetAdapter -Name "Wireless" | Remove-NetIPAddress 

Set DNS Server

 Set-DnsClientServerAddress -InterfaceAlias "Wireless" -ServerAddresses "10.10.20.1","10.10.20.2"

Set interface to DHCP

 Set-NetIPInterface -InterfaceAlias "Wireless" -Dhcp Enabled

Now with this basic knowledge we can replace some of the basic netsh commands and ipconfig

netsh

ipconfig
netsh interface set interface "Local Area Connection" newname="Management"
netsh interface ipv4 set address "Management" static 192.168.10.101 255.255.255.0 192.168.10.1

Windows PowerShell

Get-NetAdapter
Rename-NetAdapter -Name "Local Area Connection" -NewName "Management"
New-NetIPAddress -InterfaceAlias "Management" -IPv4Address 192.168.10.101 -PrefixLength "24" -DefaultGateway 192.168.10.1


Hyper-V 3: Attach multiple VHDs via PowerShell

Powershell Hyper-V Attach VHDs
We are already testing some Windows Server 8 installations and to test some of the new storage features I needed to attach a lot of VHDs to a Virtual Machine. Now with this Windows PowerShell script its very easy and fast.

Important:

  • Needs Windows Server 8 (Developer Preview)
  • Needs Hyper-V 3 (Inculded in Windows Server 8 )
  • Needs PowerShell v3 (Also included in Windows Server 8 )
$filePath = "\\storage01\VHDStorage\Virtual Hard Disks\"
$fileName = "server03-disk" # Your VHD will be called yourvhdnameX.vhdx (X = Number)
$serverName = "server03"
$controllerNumber = "0"
[int64]$vHDSizeGB = "100" # in GB
$vHDFormat = "vhdx" # vhdx or vhd
$vHDType = "Dynamic"
$numberofVHDs = "8"
 
 
for ($i=1;$i -le $numberofVHDs; $i++){
 
    $vHDPath = $filePath + $fileName + $i + "." + $vHDFormat
    New-VHD -Path $vHDPath -SizeBytes ($vHDSizeGB * 1073741824) -VHDFormat $vHDFormat -VHDType $vHDType
    Add-VMHardDiskDrive -VMName $serverName -ControllerType SCSI -ControllerNumber $controllerNumber -Path $vHDPath
 
}

The best thing overall is how you fast you can create such scripts with PowerShell v3. It took me like 10-12 minutes to get this thing done, and if you think how I would have to create and attach all of those VHDs with the wizard, I saved myself a lot of time.