A N

February 11, 2016

Get LastBootUpTime and Export to CSV

Filed under: Tips — donrsh @ 10:51 am
# Created by Arash Nabi / arash@nabi.nu
# Date: 2016-02-11

try
{
	$Source = "C:\temp\Serverlists\2.txt"
    $Export = "C:\temp\Serverlists\2.csv"
    $VerbosePreference = 'Continue'
    $xObj = @()
	$Servers = Get-Content -Path $Source
	
	foreach ($server in $servers)
	{		
		# CIM stuff
		$objOption = New-CimSessionOption -Protocol Dcom -ErrorAction SilentlyContinue
		$objSession = New-CimSession -ComputerName $server -SessionOption $objOption -ErrorAction SilentlyContinue
		if ($objSession)
		{
			$params = @{
			'ServerName' = $server
			}
			$LastReboot =  Get-CimInstance -CimSession $objSession -Namespace ROOT/cimv2 -ClassName Win32_OperatingSystem | select LastBootUpTime
			$params.Add('LastBootUptime',"$($LastReboot.LastBootUpTime)")
			# Create objects
			$objresult = New-Object psobject -Property $params
			# Add object to result
			$xObj += $objresult
			Write-Verbose -Message $server
		}
		else
		{
			Write-Warning "No CimSession connection to $server. Aborting"
		}
	}
	$xObj | Export-Csv -Path $Export -NoTypeInformation
}
catch [System.Net.WebException] #, [System.Exception]
{
	$errorMessage = $_.Exception
	Write-Warning -Message $errorMessage
}
finally
{
	Write-Verbose -Message 'End of Script'
}
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: