Get Uptime with Powershell

Use the following script to determine the uptime of your servers which are defined in a text file:

 

# Writer = Ritesh Parab; fb.com\parab.ritesh  
# File Name = Get-uptime 1.2
# Description = Get Server up time 
# function Date and time copied from Technet :)
# Error Handling : TRY and CATCH
# Bugs are welcome 2 [email protected]
# Fix > WMI Query time function added {from technet forum}


cls                                             
$erroractionpreference = "SilentlyContinue"
# Helper Function - convert WMI date to TimeDate object
function WMIDateStringToDate($Bootup) {
 [System.Management.ManagementDateTimeconverter]::ToDateTime($Bootup)
}

# Main script
$Computer = Get-Content "c:\Servers.txt"

foreach ($system in $Computer) {
$rtn = Test-Connection -ComputerName $system -Count 1 -BufferSize 16 -Quiet
	 	IF($rtn -match 'True') {
		$NameSpace = "Root\CIMV2"
		$wmi = [WMISearcher]""
	 	$wmi.options.timeout = '0:0:15' #set timeout to 10 seconds
	 	$query = 'Select * from Win32_OperatingSystem'
	 	$wmi.scope.path = "\\$system\$NameSpace"
	 	$wmi.query = $query
	  	Try{
	  	$wmiresult = $wmi.Get()
	  	#	$wmiresult
			foreach ($wmioutput in $wmiresult){
			   $Bootup = $wmioutput.LastBootUpTime
			   $LastBootUpTime = WMIDateStringToDate($Bootup)
			   $now = Get-Date
			   $Uptime = $now - $lastBootUpTime
			   $d = $Uptime.Days
			   $h = $Uptime.Hours
			   $m = $uptime.Minutes
			   $ms= $uptime.Milliseconds
			   $a = "$System Up for: {0} days, {1} hours, {2}.{3} minutes" -f $d,$h,$m,$ms
			   Write-Host "$a" -ForegroundColor Green
	  		   }
	  		}
		Catch {
	  			Write-host "WMI not available on : $System" -ForegroundColor Red
				Write-Output "WMI failed on $system because : $_" | Out-File "c:\failed.txt" -Force -Append 
	  	}
		}
		else{
			write-host "$system Offline" -ForegroundColor DarkRed
			Write-Output "$system Offline" | Out-File 'c:\failed.txt' -Force -Append 
		}
	} 

 

Enabling Change Block Tracking (CBT) on a Virtual Machine (VMware vSphere 5.1)

Source: http://www.lazywinadmin.com/2013/01/enabling-change-block-tracking-cbt-on.html+

 

How to Enable CBT on your VM ? (PowerShell/PowerCli)

You can do the following even if your VM is Powered ON.

# Check and Add the PowerCli Snaping if not already present
if(-not(Get-PSSnapin -Registered -Name "VMware.VimAutomation.Core"){
    Add-PSSnapin -Name VMware.VimAutomation.Core}

# Connect to my Vcenter
Connect-VIServer -Server vcenter.fx.lab

#Here is aRunning the script on TESTSERVER04 to enable CBT
$vmtest = Get-vm TESTSERVER04 | get-view
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.changeTrackingEnabled = $true
$vmtest.reconfigVM($vmConfigSpec)

How to Apply this CBT configuration ?

Once you enable CBT, the VM must go through a stun-unstun cycle (power on, resume after suspend, migrate, or snapshot create/delete/revert) before the reconfiguration takes effect.

How to Check if CBT is enabled on your VM (PowerShell/PowerCli)

# Check if your VM has (Change Block Tracking) enabled or not
(Get-VM -Name TESTSERVER04).ExtensionData.Config.ChangeTrackingEnabled

# Find VMs where CBT (Change Block Tracking) is Enabled
Get-VM| Where-Object{$_.ExtensionData.Config.ChangeTrackingEnabled -eq $true}