A N

October 8, 2015

Get server architecture with CimSession

Filed under: Tips — donrsh @ 12:26 pm
<#   
    .NOTES
    ===========================================================================
     Created on:    2015-10-08 
     Created by:    Arash Nabi
     Email:         arash@nabi.nu 
 
    ===========================================================================
    .DESCRIPTION 
        Get server architecture info through CimInstance.
        The function creates a new cimSession to a remote computer and collects
        server architecture information and then creats a powershell object of them.
    .Exampel
        PS C:\> Get-Architecture -Servers $value1
     
#>


function Get-Architecture
{
	[CmdletBinding()]
	[OutputType([int])]
	Param
	(
		[Parameter(Mandatory = $true,
				   ValueFromPipelineByPropertyName = $false,
				   Position = 0)]
		$Servers
		
		
	)
	
	Process
	{
		try
		{
			$serverarch_obj = @()
			
			foreach ($server in $servers)
			{
				
				# CIM stuff
				$objOption = New-CimSessionOption -Protocol Dcom -ErrorAction Stop
				$objSession = New-CimSession -ComputerName $server -SessionOption $objOption -ErrorAction Stop
				
				$WinOS = Get-CimInstance -CimSession $objSession -Namespace ROOT/cimv2 -ClassName Win32_OperatingSystem -ErrorAction Stop
				$Model = (Get-CimInstance -CimSession $objSession -Namespace ROOT/cimv2 -ClassName Win32_ComputerSystem -ErrorAction Stop ).Model
				$IP    = (Get-CimInstance -CimSession $objSession -Namespace ROOT/cimv2 -ClassName Win32_NetworkAdapterConfiguration -ErrorAction Stop | 
                    where { $_.ipaddress -like "1*" } | select -ExpandProperty ipaddress | select -First 1 -ErrorAction SilentlyContinue)
				$CPU   = Get-CimInstance -CimSession $objSession -Namespace ROOT/cimv2 -ClassName Win32_Processor -ErrorAction Stop
				
				
				$params = @{
					'ServerName'        = $server
					'Architecture'      = $WinOS.OSArchitecture
					'Operating System'  = $WinOS.Caption
					'Model'             = $Model
					'CPU'               = ($CPU.Name | Select-Object -First 1)
					'IP'                = $IP
					'RAM'               = ([math]::round($WinOS.TotalVisibleMemorySize/1MB)).ToString() + ' GB'
					'Cores'             = ($CPU.NumberOfCores).count
					'LogicalProcessors' = ($CPU.NumberOfLogicalProcessors).Count
					
					
				}
				
				$allDisks = Get-CimInstance -CimSession $objSession -Namespace ROOT/cimv2 -ClassName Win32_Volume -ErrorAction Stop |
                     select driveletter, capacity
				
				foreach ($disk in $alldisks)
				{
					$DisksizeGB = [math]::round($disk.capacity/1GB)
					$diskLetter = $disk.Driveletter.Replace(':', '')
					$params.Add($("$diskLetter Drive"), $("$disksizeGB GB"))
					
				}
				
				# Create objects
				$objresult = New-Object psobject -Property $params
				
				# Add object to result
				$serverarch_obj += $objresult
				
			}
			
		}
		catch [System.Net.WebException], [System.Exception]
		{
			$errorMessage = $_.Exception.Message
            Write-Warning -Message $errorMessage
		}
		finally
		{
			$serverarch_obj
		}
	}
	
}
Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: