Contact

Subscribe via Email

Subscribe via RSS

Categories

Creative Commons Attribution 4.0 International License
© Rakhesh Sasidharan

Quickly find out which version of .NET you are on

I had to install WMF 3.0 on one of my Server 2008 R2 machines, a pre-requisite to which is that the machine must have .NET 4.0 (standalone installer here, Server Core installer here). So how do I find whether my machine has .NET 4.0? Simple – there’s a registry key you can check. See this link for more details. Very quickly, in my case, since I only want to see whether .NET 4.0 is present or not, the following command will suffice: 

In my case .NET 4.0 was not installed:

By the way if you try installing WMF 3.0 without .NET 4.0 present, you’ll get an not so helpful error “The update is not applicable to your computer.”

Get a list of links in a web page (part 2)

A continuation to my previous post on getting links.

Internet Explorer is an application with a rich COM interface. And PowerShell can work with COM objects. Thus you can do the following:

The Document property is very powerful and lets you see a lot of details of the page. It has a subproperty Link that gives all the link elements in the page (it has nearly 800 properties, methods, and events!). The output is as objects, and since we are only interested in the actual link href elements we can select that property.

If you are PowerShell v3 things are even easier. There’s a cmdlet called Invoke-WebRequest who is your friend.

To get an object representing the website do:

To get all the links in that website:

And to just get a list of the href elements:

Like the System.Net.Webclient class Invoke-WebRequest has parameters to specify proxy, headers, encoding, etc.

Using Remove-NetRoute to mass remove routes

Was checking my Windows 8 machine routing table (route print) when I noticed many entries like this:

Not sure what they are. I didn’t create them, and the 192.168.1.1 address is not on my network.

To be on the safe side I wanted to remove them. One could do it via route delete but that’s so old fashioned and slow (I would have to do it for each entry). What I want is a quick and easy way of mass removing routes. Enter the routing table related cmdlets in PowerShell 3.0.

The following one-liner will remove all routes whose NextHop address is 192.168.1.1:

Easy peasy!

For PowerShell 2.0 am sure there would be a WMI way of achieving the same. Will post that later.

Managing network interfaces with PowerShell v3

Update: Learnt from this StackOverflow post that the cmdlets below are only available on PowerShell v3 running on Windows 8/ Windows Server 2012 (and later). They are not available on PowerShell v3 running on Windows 7/ Windows Server 2008.

Windows Server 2012 comes with PowerShell v3 and that has the ability to manipulate the network interface from within PowerShell. Meaning you can view the IP address, set IP address, change name of the interface, and so on. I find that cool!

I explored these new cmdlets by typing Get-Net at the PowerShell prompt and pressing TAB. This shows all the commands and I kept trying the ones I felt interested in and discovered new ones from reading the help pages.

For starters, Get-NetIPInterface shows you the available network interfaces. You can pass the cmdlet parameters to filter the results in terms of (say) showing only the interfaces that are connected or showing only the interfaces that are assigned an IP from DHCP.

image.png

To see the IP addresses, use the Get-NetIPAddress cmdlet:

image.png

We can combine the two cmdlets. For instance, to find the IP addresses of the DHCP assigned interfaces one can pipe the two commands: Get-NetIPInterface –Dhcp Enabled | Get-NetIPAddress

image.png

To change an interface settings such as enable/ disable DHCP use the Set-NetIPInterface cmdlet.

To assign a new IP address use the New-NetIPAddress cmdlet (this automatically disables DHCP on that interface if it’s enabled). To change the property of an existing IP address (such as the subnet mask, for instance) use the Set-NetIPAddress cmdlet. And To un-assign an IP address use the Remove-NetIPAddress cmdlet.

image.png

image.png

I find the Set-NetIPAddress cmdlet slightly confusing. One would expect it to be able to set an IP address too, for instance, but it does not work that way. To add to the confusion this cmdlet too has switches similar to the New-NetIPAddress cmdlet to specify an IP Address (the -IPAddress switch) so you’d think it’s possible to set an IP address this way. But don’t be fooled. All this –IPAddress switch does with the Set-NetIPAddress cmdlet is to let you select interfaces matching that IP address.

If you try and set an IP address using the Set-NetIPAddress cmdlet you get an error:

image.png

The error message is obvious. You can see the cmdlet is trying to find interfaces matching the IP address you specify – and failing – rather than set that as the IP address of an interface.

Moving on, I like to rename the network adapters in my VMs. That too is possible using PowerShell now. Rename-NetAdapter is your friend!

image.png

I also like to disable some of my network adapters. You can do that too now through PowerShell using the Disable-NetAdapter cmdlet.

image.png

Would have been handy if cmdlets such as Disable-NetAdapter were a part of Set-NetAdapter (via a switch).

PowerShell is also now cool enough to fiddle with the bindings. So, for instance, if you want to disable IPv6 for one of your interfaces – possible now via PowerShell! Use Get-NetAdapterBinding to see the available bindings (IPv4, IPv6, etc) and disable using Disable-NetAdapterBinding.

image.png

Goes without saying – all these Disable-* cmdlets have an Enable-* counterpart too. So you can enable whatever you disable.

This TechNet topic gives a list of all the Network related cmdlets in PowerShell.

When assigning a new IP address to an interface using New-NetIPAddress you can pass a default gateway IP too via the –DefaultGateway switch. If you forget to do that, there’s no way to add a default gateway – perhaps using the Set-NetIPAddress cmdlet as I was expecting. The only alternatives are to remove the IP address via the Remove-NetIPAddress cmdlet and then re-add the IP address but this time specifying the default gateway; or use the New-NetRoute cmdlet to manipulate the routing table directly.

The Get-NetRoute cmdlet can be used to view the existing routing table. And the New-NetRoute can be used to add a new route. To make a route the default gateway set the destination prefix as 0.0.0.0/0 (for IPv4) or ::/0 (for IPv6). Examples below:

image.png

image.png

image.png

Lastly, there are cmdlets to configure the DNS resolvers. The Get-DnsClient cmdlet shows you DNS configuration information for each interface. It doesn’t show the resolver addresses; rather this cmdlet is about the DNS client itself and so shows information such as the DNS suffixes and the search list for these suffixes. The Get-DnsClientServerAddress cmdlet does what it says – it shows you the resolver server address for each interface – and is probably what most of us will commonly use.

image.png

To set DNS resolves you can use the Set-DnsClientServerAddress cmdlet. To specify multiple addresses put them in brackets with the entries comma separated and in double quotes. The double quotes are important because without them the addresses are ignored.

image.png

The same cmdlet can be used with a –ResetServerAddresses switch to remove the server addresses.

image.png

And that’s more or less it. These cmdlets only touch the tip of the ice-berg, but I think these are the ones most of us will regularly use.

Just to summarize here’s a table with all the cmdlets:

Get-NetIPInterfaceShows you the available network interfaces. Can pass parameters to filter the results (e.g. only the DHCP assigned ones).
Get-NetIPAddressShows you the IP addresses. Again, can filter using parameters.
Set-NetIPInterfaceChange an interface settings. Such as turn off/ on DHCP, IPv6 neighbor discovery settings, router settings (advertising, packet forwarding), and Wake on LAN.
New-NetIPAddressAssign a new IP address to an interface. Use the –DefaultGateway switch to specify the default gateway.
Remove-NetIPAddressRemove an assigned IP address from an interface.
Set-NetIPAddressChange IP address properties. For instance: change the subnet mask.
Rename-NetAdapterRename a network adapter.
Disable-NetAdapterDisable a network adapter. To enable use Enable-NetAdapter.
Get-NetAdapterBindingView the network adapter bindings. Such as IPv4, IPv6, Client for Microsoft Networks.
Disable-NetAdapterBindingDisable network adapter bindings. To enable use Enable-NetAdapterBinding.
Get-NetRouteView the routing table.
New-NetRouteAdd an entry to the routing table. Use destination prefix as 0.0.0.0/0 (for IPv4) or ::/0 (for IPv6) to set default gateway.
Remove-NetRouteRemove a routing table entry.
Get-DnsClientView the DNS client settings. Such as DNS suffix, search list, and so on.
Get-DnsClientServerAddressView the DNS client server addresses.
Set-DnsClientModify the DNS client settings.
Set-DnsClientServerAddressAdd DNS client server addresses. Put multiple address as (“x.x.x.x”, “x.x.x.x.”, …"). Use the -ResetServerAddresses switch to remove the server addresses

Now that’s a good reference for me too to check whenever I forget these commands!