Wednesday, February 8, 2012

Compare DNS Zones with PowerShell

cls
Remove-Variable -Name * -Force -ErrorAction SilentlyContinue
$comp1 = 'DNSServer1'
$comp2 = 'DNSServer2'
$bonefish = Get-WmiObject -Class MicrosoftDNS_Zone -ComputerName $comp1 -Namespace root\microsoftDNS | Where {$_.ZoneType -eq '2' -or  $_.ZoneType -eq '1'} | Select Name
$bonefish2 = Get-WmiObject -Class MicrosoftDNS_Zone -ComputerName $comp2 -Namespace root\microsoftDNS | Where {$_.ZoneType -eq '2' -or  $_.ZoneType -eq '1'} | Select Name
$comparison = compare-object $bonefish $bonefish2  -Property name
if ($comparison -ne $null)
{
$mycol = @()
foreach ($c in $comparison)
{
    switch ($c.SideIndicator)
    {
       
    "=>" {
      {
            write-host $comp2 "has the zone" $c.Name  $comp1 "does not"
   $output = "`r" + $comp2 +  " has the zone " + $c.Name + " " + $comp1 + " does not."
   $output
   $myCol += $output
   }
          }
         
    "<="  {
            write-host $comp1 "has the zone" $c.Name  $comp2 "does not"
   $output = "`r" +  $comp1 + " has the zone " + $c.Name + " " + $comp2 + " does not."
   $output
   $myCol += $output
          }
    } # End Switch
  # End foreach
}

Tuesday, February 7, 2012

When was my server last rebooted?

foreach ($server in 'SERVER1','SERVER2')
   {
    #Test WMI Connection
    $testCon = 0
    $testWmi = Get-WmiObject Win32_OperatingSystem -computerName $server
    if (-not $testWmi) { $testCon += 1; write-host "Cannot connect to WMI on remote server, $server" }
    $myCol = @()
    #Get WMI info
    $wmi=Get-WmiObject -class Win32_OperatingSystem -computer $server -ErrorAction SilentlyContinue
        $Time=$wmi.ConvertToDateTime($wmi.Lastbootuptime)
    [TimeSpan]$uptime=New-TimeSpan $Time $(get-date)
    $uptimeDays = $uptime.days
    $uptimeHours = $uptime.hours
    $uptimeMins = $uptime.minutes
    $Object = "" | Select Server,Days,Hours,Minutes
    $Object  = New-Object -TypeName psobject
    $Object  | Add-Member -MemberType noteproperty -Name Server $server
    $Object | Add-Member -MemberType noteproperty -Name "Days" $uptimeDays
    $Object  | Add-Member -MemberType noteproperty -Name "Hours" $uptimeHours
    $Object | Add-Member -MemberType noteproperty -Name "Minutes" $uptimeMins
    $myCol += $Object
    Write-Output $myCol
    }
Write-Host "Completed Uptime Check"