Showing posts with label get-WmiObject. Show all posts
Showing posts with label get-WmiObject. Show all posts

Wednesday, 13 January 2016

PowerShell: List all IPv4 Addresses

A very short blog post today. I'm not sure why this is so complicated, maybe there's an easier way, or a more recent cmdlet that allows it to be obtained in an easier way.

I want to list all IP (v4) addresses and nothing else with a single line PowerShell command that is backwards compatible with all versions of PowerShell.

Here's what I came up with:

foreach ( $i in (Get-WmiObject Win32_NetworkAdapterConfiguration -Namespace "root\CIMV2" | where { $_.IPEnabled -eq "True" } | select IPAddress) ) { $i.IPAddress[0] }

Here's a quick breakdown of the command

It's basic structure uses a foreach loop

Foreach ( $TempVariable in $MyVariableList ) { do this }

$i is my temporary variable, that exists only for the purposes of the loop. The Foreach loop will cycle through each object in the second variable, and each time it will be represented by $i

$MyVariableList is a variable that contains many objects, and is often obtained in a script by a separate command. As I want a one-liner, I have substituted this variable with a command that produces a list of objects.

This is my command:

Get-WmiObject Win32_NetworkAdapterConfiguration -Namespace "root\CIMV2" | where { $_.IPEnabled -eq "True" } | select IPAddress)

It uses Get-WMIObject to return the WMI class Win32_NetworkAdapterConfiguration. I chose the Get-WMIObject command to return the IP address data because I know it is compatible with all version of PowerShell. This command on its own returns a large amount of data, so I have piped the results to the Where-Object command. This filters to return only network adapters that are enabled. These results are then piped to the Select-Object command, and this selects the single property of IP address.


IP Address properties






The result is an IP address object for each adapter. Each contains both an IPv4 and IPv6 address.

{ do this } Finally the Foreach loop will carry out an action on each object that is returned. In this case it simply takes the object and calls the IPAddress property (which is the only one available). The square brackets specify the 1st result only will be displayed.

IP Address list




Simple.




Monday, 12 January 2015

PowerShell and IIS 6

IIS 6

Windows Server 2003 running IIS 6 does not support the latest version of PowerShell, and will not allow the use of the WebAdministration module or the most recent IIS cmdlets

(see this site for WebAdministration Cmdlets http://technet.microsoft.com/en-us/library/ee790599.aspx)

To use PowerShell with IIS 6, you must use WMI, specifically the IIS WMI provider. To do this, use the Get-WmiObject cmdlet.
The WMI namespace is root/Microsoftv2. The following link gives a list of classes that can be used:
http://msdn.microsoft.com/en-us/library/ms525265(v=vs.90).aspx


 

WMI Classes

I experimented with the different classes to find the properties that I needed. For example IISWebServer will give information on each site, IISWebServerSetting will give more detailed information on each site. IISWebVirtualDir will give information on virtual directories and applications and IISWebVirtualDirSetting will again give more detail on virtual directories and applications.

For example, try running each of these commands, then view the results to see how the output changes.

Get-WmiObject -Namespace "root/MicrosoftIISv2" -Class IIsWebServer
Get-WmiObject -Namespace "root/MicrosoftIISv2" -Class IIsWebServerSetting
Get-WmiObject -Namespace "root/MicrosoftIISv2" -Class IIsWebVirtualDir
Get-WmiObject -Namespace "root/MicrosoftIISv2" -Class IIsWebVirtualDirSetting

The next step is to use the where-object and select-object cmdlets to display only the results you want.

Eg.
This command will return a list of sites by site ID
Get-WmiObject -Namespace "root/MicrosoftIISv2" -Class IIsWebServer | Select-Object -ExpandProperty Name

Whereas this command will return a specific site by specifying the site ID
Get-WmiObject -Namespace "root/MicrosoftIISv2" -Class IIsWebServer | Where-Object { $_.Name -like "W3SVC/123456789" }

Also, this command will return a specific site by specifying the site ID along with all the settings for that site
Get-WmiObject -Namespace "root/MicrosoftIISv2" -Class IIsWebServerSetting | Where-Object { $_.Name -like "W3SVC/123456789" }

I was interested in listing all Physical Paths that were being used by each site in IIS. For this I need the IISWebVirtualDirSetting class and the Path property

Eg.
Get-WmiObject -Namespace "root/MicrosoftIISv2" -Class IIsWebVirtualDirSetting | Select-Object -ExpandProperty Path
 

I can also add the -unique switch to eliminate any duplicates, and pipe the results to sort-object to put the output in alphabetical order

Eg,
Get-WmiObject -Namespace "root/MicrosoftIISv2" -Class IIsWebVirtualDirSetting | Select-Object -ExpandProperty Path -Unique | Sort-Object
 

Script


I put this all together into a script that will output the site name followed by a list of all Physical Paths in use by IIS. One of the challenges here was identifying a class that contained both the site name and physical path. This was not possible with one class, so I had to do it in stages.

I obtained the Name property from IIsWebServer class. This is the Site ID, and I can use this to filter both ServerComment property from IIsWebServerSetting class for a user friendly website name, and the path property from IIsWebVirtualDirSetting for the Physical Path

Script text


$WebSiteID = Get-WmiObject -Namespace "root/MicrosoftIISv2" -Class IIsWebServer | Select-Object -ExpandProperty Name

ForEach ( $Site in $WebSiteID )

{
$WebSiteName = Get-WmiObject -Namespace "root/MicrosoftIISv2" -Class IIsWebServerSetting | Where-Object { $_.Name -like "$site" } | Select-Object -Expandproperty ServerComment

write-host "`r`n" $WebSiteName

$AppPath = Get-WmiObject -Namespace "root/MicrosoftIISv2" -Class IIsWebVirtualDirSetting | Where-Object { $_.Name -like "$site/*" } | select -expandproperty path

$AppPath = $AppPath | select-object -unique | sort-object
$AppPath
}


One more thing to note is that I use -ExpandProperty rather than -Property to filter the results from the where-object and select-object cmdlets. This is to convert the output to a string. If I don't do this, I get the object itself, rather than a string containing the objects. To see what this actually means, try running these two commands and notice the difference in the results and the available methods and properties.

Get-WmiObject -Namespace "root/MicrosoftIISv2" -Class IIsWebServer | Select-Object -Property Name | Get-Member
 

Get-WmiObject -Namespace "root/MicrosoftIISv2" -Class IIsWebServer | Select-Object -ExpandProperty Name | Get-Member