Posts Tagged ‘PowerShell’

Automagically Fixing FailedAndSuspended Exchange 2010 Databases with PowerShell

Another quick script, this one finds all databases located on Exchange 2010 servers with a status of ‘FailedAndSuspended’ and then reseeds them. Since this scripts makes changes to the systems, instead of just reading information, all activities are logged via PowerShell’s transcript feature. You’ll need to change the path in the 5th line of the script to reflect an actual location on your system.
Note that there are other options besides a reseed, this just makes the most sense the majority of the time.

add-pssnapin *0* -ErrorAction SilentlyContinue
$startstring="Start script run at:  "
$startendtime=date
$startannounce=$startstring+$startendtime
Start-Transcript -Append -Force -Path 'C:\<path>\DBHealthFix.log'
$startannounce
#gets list of mailboxservers, locates 2010 servers, gets db copy status, finds copies that are failed, updates failed copies
$mailboxservers = get-mailboxserver | get-exchangeserver | ?{$_.IsE14OrLater -eq 'True'}
foreach ($mailboxserver in $mailboxservers){
get-mailboxdatabasecopystatus -Server $mailboxserver.name | ?{$_.Status -like 'FailedAndSuspended'} | update-mailboxdatabasecopy -deleteexistingfiles -confirm:$false
}
stop-transcript

Here are some screenshots of what happens along the way:

The script shown in the first and last screenshot is available here.

Possibly Related Posts:

Posted: August 16th, 2010
Categories: Exchange Server, PowerShell
Tags: ,
Comments: No Comments.

Checking Exchange 2010 Database Health with PowerShell

Just a quick script that checks your databases. Anything besides ‘Healthy’ or ‘Mounted’ should probably be investigated. :)

Add-PSSnapin *0* -ErrorAction SilentlyContinue
$mailboxservers = get-mailboxserver | get-exchangeserver | ?{$_.IsE14OrLater -eq 'True'}
$A = (get-host).UI.RawUI
$A.WindowTitle = "Database Health Check"
$B = $A.windowsize
$B.width = 110
$B.height = 30
$A.WindowSize = $B
while ($true) {cls; foreach ($mailboxserver in $mailboxservers){Get-MailboxDatabaseCopyStatus -Server $mailboxserver.name | ft -AutoSize Name,*Status,ContentIndexState,CopyQueueLength,ReplayQueueLength} ;sleep 5}

This it the output, refreshed every 5 seconds:

An example of when databases are actually doing something:

Possibly Related Posts:

Posted: August 16th, 2010
Categories: Exchange Server, PowerShell
Tags: ,
Comments: No Comments.

How to set a preferred DC in PowerShell

It’s quick and easy but keep in mind this does not set a default for PowerShell sessions. You’ll have to enter this command in each window you want to specify a DC in.

Set-ADServerSettings –PreferredServer <fqdn>

On the same subject, it’s easy to find out what DC & GC you’re using in a particular PowerShell session. Just execute the following command:

Get-ADServerSettings | fl

Possibly Related Posts:

Posted: August 16th, 2010
Categories: Exchange Server, PowerShell
Tags: ,
Comments: No Comments.

Remotely Create A Registry Key With PowerShell

Something new I learned today is how to remotely create a registry key using remote PowerShell. You can also modify, query, etc.. See TechNet for a reference on the use of the reg command including valid value types, examples and such.

Invoke-Command -ComputerName <server fqdn> -ScriptBlock {reg add <registry path> /v <value name> /t <value type> /d <data in the key>}

Possibly Related Posts:

Posted: August 16th, 2010
Categories: Exchange Server, PowerShell
Tags: ,
Comments: No Comments.

Free PowerShell Script: Monitoring SCR Health

Let me know if you see anything obviously wrong with it. Use at your own risk! :)

#################################
#Copyright 2009 Cohesive Logic LLC
#SCR Health Check Script
#Description – Determines if any of the storage groups on the SCR target server are in an unhealthy state.
#################################

#Options – Specify the following options for the target Enviroment
#################################
#SCR Target Server – Specify the FQDN SCR Target server to be analyzed
$SCRTarget = “Target Server”
#Exchange Mailbox Server – Specify the FQDN for the Exchange Mailbox Server Source for your SCR Target
$MailboxServer = “Source Mailbox Server”
#SMTP Server – Specify FQDN of a local SMTP server that can be used to relay the warning email
$SMTPServer = “SMTP SEERVER NAME by DNS or IP” -
#EmailRecipient – Specify the email address where the warning email shoudl be sent.
$EmailRcpt = “JoeDoe@server.com”
#################################

#The email function used to transmit email over using an SMTP relay
function Send-Email([string]$rcpt,$smtpServer,$body) {

$msg = New-Object net.Mail.MailMessage
$smtp = New-Object net.Mail.SmtpClient($smtpServer)

$msg.From = “ExchangeSCR@Domain.com”
$msg.To.Add($rcpt)
$msg.Subject = “SCR HEALTH WARNING”

$msg.Body = $body
$smtp.Send($msg)
}
#The Check-SCR function determines if replication on any of strorage groups of the SCR target server are in an unhealthy state.
#If any  storage groups are found to be unheathy
function Check-SCR($MBX,$SCR,$SMTP,$rcpt) {
$MessageBody = $NULL
$ReplicatedSG = Get-StorageGroup -Server $MBX | where {$_.StandbyMachines -ne $NULL} | Select Identity
foreach($SG in $ReplicatedSG) {
if((Get-StorageGroupCopyStatus $SG.Identity -StandbyMachine $SCR).SummaryCopyStatus -ne “Healthy”){
$MessageBody += Get-StorageGroupCopyStatus $SG.Identity -StandbyMachine $SCR | Select Identity,SummaryCopyStatus,CopyQueueLength,ReplayQueueLength LastInspectedLogTime,ServiceDown,Failed,Suspend
}
}
if($MessageBody -ne $NULL){
Send-Email $rcpt $SMTP $MessageBody
}
}

Check-SCR $MailboxServer $SCRTarget $SMTPServer $EmailRcpt

Possibly Related Posts:

Posted: August 16th, 2010
Categories: Exchange Server, PowerShell
Tags: ,
Comments: No Comments.