PowerShell: Working With WMI class To create Functions

Referred article 
There are Several WMI classes in WMI name space and are Very useful for backward Compatibility while writing PowerShell scripts.

 


WMI classes in PowerShell

Windows Management Instrumentation (WMI) is a core technology for Windows system administration because it exposes a wide range of information in a uniform manner. Because of how much WMI makes possible, the Windows PowerShell cmdlet for accessing WMI objects, Get-WmiObject, is one of the most useful for doing real work. We are going to discuss how to use Get-WmiObject to access WMI objects and then how to use WMI objects to do specific things.

Get-WmiObject cmdlet

​Gets insatances Of windows managment Instrumentation(WMI) classes or Information available classe. 
 Alias for Get-Wmiobject cmdlet in powershell is gmwi.

Some Functions

This is a simple Function to get the UserAccounts in Computer.

function ($Cname)
{
   ($param)
   Get-WmiObject Win32_Useraccount -ComputerName $Cname
}

Advanced Function

This Function  will give you the the Local User Accounts For the Computer Specified as parameter.

Using Some Advanced Funtions, We can make a Script to a Cmdlet.
for a function to behave like a cmdlet, We only need to add a single Line." [cmdletbinding()]".
here is a simple cmdlet to Get Local useraccounts From Multiple computers using WMI.

function Get-Useraccount
{
    [CmdletBinding(ConfirmImpact='Medium')]
    [Alias("GUA")]
      Param
    (
        # Target ComputerName(s).
        [Parameter(ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true,
                   ValueFromRemainingArguments=$false)
                   ]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [Alias("CN")]
        [string[]]
        $ComputerName=$env:COMPUTERNAME,
 
        #Credentials     [not Mandatory]
        #using get-credenttial to pass credentials for the computers listed
        [parameter()]
        [System.Management.Automation.PSCredential]$Credential=$null
 
    )
 
    Begin
    {
    }
    Process
    {
         
         foreach($c in $computerName)
        {
            try
            {
               if ($Credential -ne $null)
               {
                 
                $share=Get-WmiObject Win32_Useraccount -ComputerName $C -Credential $Credential |?{$_.localaccount -eq $true} -ErrorAction Stop
               Write-Output "`nThe user Local User Account(s) in $C are Listed Below:"
                $share
               }
               else
               {
                 Write-Output "`nThe user Local User Account(s) in $C are Listed Below:"
                Get-WmiObject Win32_Useraccount -ComputerName $C |?{$_.localaccount -eq $true} -ErrorAction Stop
               }
            }
            catch
            {
                Write-Warning "`nThe Specified computer $C Is not Online Or Not Accessible"
               "`n"
            }
        }
         
         
    }
    End
    {
    }
}

Here are some better usage for  wmi classes which is backward compatible in all version of powershell..

Get-WmiObject win32_bios
This cmdlet gives the BIOS information of a computer.

Get-WmiObject win32_operatingsystem
This cmdlet shows the operating System Information Of a computer.

Get-WmiObject win32_product.
This cmdlet will shows you the installed Softwares in a Computer.

Get-WmiObject win32_networkadapterconfiguration
This cmdlet will show  you the complete NIC details of a computer.

To get available WMI classes,Use
Get-WmiObject -List
This will give you a complete list of WMI classes in a Computer.


See also

WMI