30 May 2013

Powershell script to check for stopped "Automatically started" services for multiple servers

The powershell script below checks for services that are automatically started when the system starts.
If a service is found stopped, the scripts starts the service and sends a notification email to an admin or a group.

Multiple servers are supported, and services that stop automatically because they have no need to run al the time (software protection for instance) are filtered out.

Modify the following to adjust for your environment:
  • The service name you want to exclude
  • The server array server names
  • The email receipients
  • The smtp server

The script:

#####################################################################
# Author:  Edwin van Brenk                                                                                             #
# date:  23-05-2013                                                                                                         #
# Purpose: Check if auto start services for a number of                                                 #
#                 servers are running, if not send a notification                                              #
#                 email and start the service                                                                           #
#####################################################################

$Computers = "server1","server2","server3","server4"
foreach ($Computer in $Computers)
 {
  #$exclusion = "clr_optimization"
  Write-Host "checking server: $computer"
  #Create an array of all services running
  $GetService = Get-WmiObject win32_service -ComputerName $Computer | where {($_.startmode -eq "Auto") -and ($_.state -ne "Running")}
  foreach ($service in $GetService)
   {
    if ($service.name -match "clr_optimization*")
     {
      Write-Host "`tService exclusion: " $service.name
     }
    elseif ($service.name -match "MMCSS")
     {
      Write-Host "`tService exclusion: " $service.name
     }
    elseif ($service.name -match "sppsvc")
     {
      Write-Host "`tService exclusion: " $Computer $service.name
     }
     elseif ($service.name -match "BMR Boot Service")
     {
      Write-Host "`tService exclusion: " $Computer $service.name
     }
     elseif ($service.name -match "NetBackup SAN Client Fibre Transport Service")
     {
      Write-Host "`tService exclusion: " $Computer $service.name
     }
     elseif ($service.name -match "SysmonLog")
     {
      Write-Host "`tService exclusion: " $Computer $service.name
     }
     elseif ($service.name -match "Mapsvc")
     {
      Write-Host "`tService exclusion: " $Computer $service.name
     }
     elseif ($service.name -match "NetBackup Vault Manager")
     {
      Write-Host "`tService exclusion: " $Computer $service.name
     }
     elseif ($service.name -match "NetBackup SAN Client Fibre Transport Service")
     {
      Write-Host "`tService exclusion: " $Computer $service.name
     }
     elseif ($service.name -match "NetBackup Key Management Service")
     {
      Write-Host "`tService exclusion: " $Computer $service.name
     }
     elseif ($service.name -match "RtcSrv")
     {
      Write-Host "`tService exclusion: " $Computer $service.name
     }
     elseif ($service.name -match "ShellHWDetection")
     {
      Write-Host "`tService exclusion: " $Computer $service.name
     }
    else
     {
      $servicename = $service.name
      Send-Mailmessage -to "
admin1@domain.com","admin2@domain.com" -Subject "$servicename is stopped on $Computer" -from ServicesCheck@domain.com -Body "The $servicename service was found stopped on $computer. Check if the script started the service" -SmtpServer smtp.domain.lan
      Write-Host "`tStarting service " $service.name ".." -NoNewline
      $service.StartService()
      Write-Host "Done!" -BackgroundColor Green
     }
   }
 }

23 May 2013

How to delete an activesync partnership

Over time users buy new phones, add tablets or Windows 8 mail apps to their corporate mail.
This clutters the ActiveSync partnership as only 10 devices are max supported.

This shows how to delete the unwanted ActiveSync partnership(s).

Get-ActiveSyncDeviceStatistics –Mailbox alias | Format-Table DeviceID, DeviceType, DeviceOS

Output:

DeviceID                          DeviceType    DeviceOS
--------                          ----------    --------
Appl82XXXXT6A4S                   iPhone        iOS 6.1.3 10B329
XXXX99427EA69C4070E3A9FFA5D7EA84  WindowsMail   WINDOWS
XXXX59A3A6CE935CA7A176D6616793E2  WindowsMail   WINDOWS
XXXX66426C02FEFAD9A8E189F7CB4746  WindowsMail   WINDOWS
XXXX1D99A11C747C075A0828FD1F0358  WindowsMail   WINDOWS
XXXXF213DACD1001CA7B97AF346F4C47  WindowsMail   WINDOWS
XXXX6F8CDBB2D75B3C32898543996CAB  WP8           Windows Phone 8.0.10211


Delete the Windows 8 Phone:

Get-ActiveSyncDeviceStatistics -Mailbox alias | ? {$_.DeviceOS -match "windows phone 8.0.10211"} | Remove-ActiveSyncDevice

Set mailboxes to usedatabasequotadefaults for multiple users

Get-Mailbox -resultsize unlimited | where {$_.usedatabasequotadefaults -ne $true} | fl alias | out-file "d:\temp\users.csv"

Edit the csv file so that there are no empty lines and and empty spaces after the users alias.
Must look something like this:

alias
alias1
alias2
alias3
alias4

Import-Csv "d:\temp\users.csv" | foreach {Set-Mailbox -identity $_.alias -UseDatabaseQuotaDefaults $true}

Check to see the result;
import-csv "d:\temp\users.csv" | foreach {Get-Mailbox -identity $_.alias} | fl usedatabasequotadefaults,alias