Sunday, September 29, 2019

The Blog is Dead, Long Live the New Blog!

I know I have been pretty bad at posting here, but I have again changed direction slightly to focus on M365 and all its facets, so I am starting a new blog, "M365 Musings and More", which will be found at https://m365musings.blogspot.com. Please look there for new info and scripts.

Friday, January 27, 2017

Generating Multiple Strong Passwords with PowerShell

One of my customer accounts created many "service" accounts for Common Area Phones, and there is a requirement that the passwords on those accounts be changed periodically. There are a several hundred of these accounts, so I needed a way to generate strong passwords for all the accounts and give them to me in a format I can use to either hand over to the team that will be changing those passwords, or that I can "pull" into another script to change the passwords. With this in mind,  I wrote a script to generate strong passwords for the service accounts. It can run on any computer that has PowerShell. You will need a list of account names to generate the passwords for. This list needs to be in a CSV and the accounts need to be in a column named “SamAccountName”.  



When you run the script, you’ll be prompted for the file path and name that needs to be imported. If the file is in the current directory, then just type the filename, i.e. caps.csv.



The output file will open when the script is complete, and can be found in the current directory:



 And will look like:



 

To use this, cut and paste the following into a file and save it as “New-Passwrods.ps1” :

 

<# 

.SYNOPSIS 

       This script generates a new AD Password for each SamAccountName given from a CSV file.

 

.EXAMPLE

    .\New-Passwords.ps1

 

    Reads Input file and creates a new password for each username and then outputs them

    to a csv called NewPWList.csv

 

 

.NOTES 

  Version                         : 0.1

  Rights Required                 : Standard

  Lync Version                    : 2013 (Tested on CU May 2016)

  Author                          : Sean McNamara - sean@sean-mcnamara.me

  Last Update                     : 27-January-2017

  Disclaimer                      : It's up to you to thoroughly test this script before using.
                                    I accept no responsibility for any unplanned issues this may cause.

 

.VERSION

  0.1 - Initial Version

 

#>

 

$File = Read-Host -Prompt 'Full path and name of file to import, please.'

$CAPS = Import-csv $File

 

$resultsarray = @()

Add-Type -AssemblyName System.Web

 

 

foreach($CAP in $CAPS){

    $NewPW = [System.Web.Security.Membership]::GeneratePassword(15,2)

    #$Result += $CAP.SamAccountName, $NewPW

    $PWObject = New-Object PSObject

    $PWObject |Add-Member -MemberType NoteProperty -Name "SamAccountName" -Value $CAP.SamAccountName

    $PWObject |Add-Member -MemberType NoteProperty -Name "NewPassword" -Value $NewPW

    $resultsarray += $PWObject

    }
$resultsarray |Export-csv .\NewPWList.csv -notype

Invoke-Item .\NewPWList.csv


 
I hope some of you out there will find this useful. Feel free to play with it. Thanks.


Wednesday, January 14, 2015

Lync CMS Replication Script

There are any number of reasons why you would invoke replication of your CMS database in Lync. Say you've made a change and need to ensure that change is replicated out prior to testing. It's not hard to do with Lync, you can use the Invoke-CsManagementDatabase cmdlet, which works, but doesn't give any indication that replication is complete. To check replication status, you can use the Get-CsManagementStoreReplicationStatus cmdlet, but it can be a real pain, especially if replication takes a few minutes or so. Also, I wanted to have a block of code I code use in a script to kick off replication and wait until that replication is complete prior to moving on in the script.

Below, you'll find my quick solution. The code looks like this:

Invoke-Expression 'Invoke-CsManagementStoreReplication'
    Write-Host "Invoking Replication, please wait until it's complete:" -fore Yellow
    do
    {
        $result = Get-CsManagementStoreReplicationStatus | Select ReplicaFqdn, UpToDate
        write-host -nonewline "." -Fore Yellow
        sleep 3
    } While ($result.UpToDate -ne "True")
    Write-Host
    Get-CsManagementStoreReplicationStatus | Select ReplicaFqdn, UpToDate
    Write-Host
    Write-Host "Replication has completed. Thank you for your patience." -Fore Green


Paste the above code into Notepad and save as ScriptName.ps1, and you can run it from any PS prompt. When you do, it will look like:


The script has invoked replication and is in a do-while loop, checking to see if replication is complete every 3 seconds. It will write a period every iteration to give the user an indication that is still running:


Once it detects that replication is completed, it will print out all the CMS replicas and their status:


It's just easier for me than typing in the commands and hitting up arrow and enter every few seconds to see if replication is complete. You can paste the code directly into a script if you wish, or wrap it as a function that you can call several times within your script:

Function Replicate-CMS{
Invoke-Expression 'Invoke-CsManagementStoreReplication'
    Write-Host "Invoking Replication, please wait until it's complete:" -fore Yellow
    do
    {
        $result = Get-CsManagementStoreReplicationStatus | Select ReplicaFqdn, UpToDate
        write-host -nonewline "." -Fore Yellow
        sleep 3
    } While ($result.UpToDate -ne "True")
    Write-Host
    Get-CsManagementStoreReplicationStatus | Select ReplicaFqdn, UpToDate
    Write-Host
    Write-Host "Replication has completed. Thank you for your patience." -Fore Green

 }

So, as you can see, this is a very simple script and makes it easy to invoke and track the status of replication.

I hope some of you out there will find this useful. If you do, please follow and comment. If you have any suggestions to improve this, I welcome the feedback.

Wednesday, January 7, 2015

Convert Phone Numbers For Use In Lync

Having done my fair share of Lync Implementations, I find that there are so many little tasks that take time, and one of the biggest time-bandits (nod to Terry Gilliam) is sort and converting data. Not the least of which is the conversion of telephone numbers, which when given to me from clients can come in any sort of format. It's always my goal to automate whatever I can, and I always bulk add users from a csv. It just makes sense. That said, getting the proper information into a csv in the proper format can be a real pain in the rear. Today's post will share a script I use to take a phone number provided to me in almost any format, normalize it to a string of numbers only, then format it into the LineURI and Display Number for Lync. PowerShell is really easy to use for such tasks.

So, the first task is to input your data as provided. I really wanted to modify an existing csv by adding 2 empty columns if they didn't already exist: LineUri and DisplayNumber. Doing that programmatically is a huge pain (at least as far as I can figure it out). So, once you have your CSV modified, You can begin.

Typically, I require my CSVs to have the user's Display name, ADName, and DID at a minimum. If you are building out different policies, you may want to have a column for each policy (Client, Conferencing, Archiving, Mobile, External Access, etc.), it really depends on what you are scripting. For me, I am a huge proponent of automating any task.
A truer diagram has never been made. I'm not sure who created the above diagram, but I found it here. Anyway, Back to the subject.

Once your CSV is ready, time to run the script. It first will ask for the CSV file to get the content from:

PS C:\Users\me\SkyDrive\Area51\Playground> C:\Users\me\SkyDrive\Area51\Playground\Convert-DIDtoUriDisplay.ps1
Enter file path for conversion: 

Enter your CSV file name, and hit enter:

PS C:\Users\me\SkyDrive\Area51\Playground> C:\Users\me\SkyDrive\Area51\Playground\Convert-DIDtoUriDisplay.ps1
Enter file path for conversion: test.csv
....................................................................................................

Conversion complete. Please look in C:\Users\me\SkyDrive\Area51\Playground\Conversionresults.csv for results.

That's all you need do, you now have a new CSV file in the local directory called Conversionresults.csv that will have the LineURI and DisplayNumber columns populated, and leave all other columns from your original CSV untouched. It will carry over any columns you had in the original. 

Below is a representation of the original CSV opened in Notepad:

Name,DID,LineURI,DisplayNumber
Aileen Smith,1 (123) 456-7890,,
Amy Jones,1 (123) 456-1111,,
Angel Johnson,1 (123) 456-1112,,
Adam Garcia,1 (123) 456-1122,,
Conference Room 1,1 (123) 456-9999,,
Switchboard,1 (123) 456-0000,,
Reception Copy,,,

Note the Reception Copy entry doesn't have any DID info. This is to demonstrate that if the DID field is empty for a user, the script will insert "TBD" in the DID, LineYri and DisplayNumber fields for that user in the output. 

After the script has completed, the CSV looks like:

I will be working on the next version that will be more flexible with the extension bit in the LineURI. I have clients that don't use it at all, others that use the last 4 digits of their DID (as shown here) and some use other, non-DID related numbers. I'll be adding logic to make some choices with those in mind. If you need to modify the current script to accommodate your needs, you really only need to worry about changing one line, Line 41, of the script. To not use and ";ext=" just comment or delete that code: 
Take the code:  $item.LineURI = "tel:+" + $DashFree + ";ext=" + $DashFree.substring(7) 
insert a "#" as shown: $item.LineURI = "tel:+" + $DashFree #+ ";ext=" + $DashFree.substring(7)
this will comment out that portion of the line. 

To set the extension as, say, the last 3 digits of the DID, but add a digit to the beginning (in this case, a 2), say to designate a location:
Take the code:  $item.LineURI = "tel:+" + $DashFree + ";ext=" + $DashFree.substring(7) 
Modify it like so:  $item.LineURI = "tel:+" + $DashFree + ";ext="  + "2" + $DashFree.substring(8

I hope this post proves to be of some assistance to some of you out there. 

The Script:

<#
 NAME:  Convert-DIDtoUriDisplay.ps1

 AUTHOR: Sean William McNamara
 EMAIL: sean.mcnamara@outlook.com

 COMMENT: Input a CSV file path, the script converts a field called DID, formated in most phone number
            formats, into a field called LineURI in a format like: tel:+19182345566;ext=5566 AND a field 
            called DisplayNumber in a format like: 1(918)234-5566. It will then output 
            the resultant CSV called "LineURIConversionresults.csv" to the current working directory.
            This file can then be used to populate a script to enable EV users.
            The input csv must have 2 columns called LineURI and DisplayNumber to convert. 
            
NOTE: This script assumes US locale. If using for a locale with more than single digit country code, 
        modifications to string manipulation code will be needed.
          
      
 You have a royalty-free right to use, modify, reproduce, and
 distribute this script file in any way you find useful, provided that
 you agree that the creator, owner above has no warranty, obligations,
 or liability for such use.

 VERSION HISTORY:
 1.0 2/4/2014 - Initial release (no regex matching, that would make this much more flexible)
 2.0 1/5/2015 - Added DisplayNumber and RegEx Matching. Will now take almost any phone number format
                as DID and convert.
#>
$InFile = Read-Host 'Enter file path for conversion' 
$filedata = ipcsv $InFile

ForEach ($item in $filedata){
    If ([string]::IsNullOrWhiteSpace($item.DID)){
        $item.DID = "TBD"
        $item.LineURI = "TBD"
        $item.DisplayNumber = "TBD"
    }
    Else{
        #format number for manipulation (remove all non numbers)
        $DashFree = $item.DID
        $DashFree = $DashFree  -replace "[+).( -]"
        $item.LineURI = "tel:+" + $DashFree + ";ext=" + $DashFree.substring(7) #if not using the ext= portion, comment it out.
        $item.DisplayNumber = $DashFree.substring(0,1) + "(" + $DashFree.substring(1,3) + ")" + $DashFree.substring(4,3) + "-" + $DashFree.substring(7)
        Write-host "." -NoNewline
    }
}

#$filedata | select Name, DID, LineURI, DisplayNumber | FT
$filepath = Get-Location
Write-Host ""
Write-Host ""
Write-Host "Conversion complete. Please look in $filepath\Conversionresults.csv for results."  -foregroundcolor Green

$filedata | Export-csv -NoTypeInformation .\Conversionresults.csv

Thursday, December 11, 2014

Some Useful PowerShell One Liners for Lync

Useful PowerShell One-Liners for Lync

Below are some one-liners I use often in administering Lync systems, so I thought I would share this as a reference. These should be run from a Lync PowerShell Instance, unless otherwise indicated:

List a User's Response Group Memberships
get-csrgsagentgroup | Where-Object {$_.AgentsByUri -like "sip:first.last@domain.com"} | Select name

Get users in specific OU and output to csv (run from Exchange Powershell)
get-mailbox -OrganizationalUnit "ou=YourOU,dc=DOMAIN,dc=COM" -resultsize unlimited | select-object DisplayName,SamAccountName,PrimarySmtpAddress | Export-CSV e:\files\DomainUsers.csv

Invoking CMS Replication
Invoke-CSManagementStoreReplication

Getting CMS Replication Status of only Lync Servers
Get-CsManagementStoreReplicationStatus | Where-Object {$_.ReplicaFqdn -match "ServerNamingConvention" -Or $_.ReplicaFqdn -match "SBANamingConvention"} | Select ReplicaFqdn, UpToDate

Get users where External Access is…
get-csuser | Where {$_.ExternalAccessPolicy -like "PolicyName"}

Counting users who have a telephone number set in Lync
get-csuser | Sort-Object LineURI | where {$_.LineURI -ne ""} | Measure-Object

Finding AD Disabled Accounts Who are Still Lync Enabled
Get-CsAdUser | ?{$_.UserAccountControl -match "AccountDisabled" -and $_.Enabled -eq $true} | ft Name,Enabled,SipAddress -auto

Counting AD Disabled Accounts Who are Still Lync Enabled
Get-CsAdUser | ?{$_.UserAccountControl -match "AccountDisabled" -and $_.Enabled -eq $true} | Measure-Object

List all users in a pool
get-csuser | where-object {$_.RegistrarPool -like "lyncpoolfqdn"}  | Select-Object DisplayName, LineUri

List all Common Area Phones in a pool
get-cscommonareaphone | where-object {$_.RegistrarPool -like "lyncpoolfqdn"}  | Select-Object DisplayName, LineUri

List all Analog Devices in a pool
get-csanalogdevice | where-object {$_.RegistrarPool -like "lyncpoolfqdn"} | Select-Object DisplayName, LineUri

Count How Many Users are on Lync
(Get-CsUser -OnLyncServer).Count

Get PowerShell Version (run from any PowerShell Session)
$PSVersionTable.PSVersion.Major
Or
Get-Host | Select-Object Version

Getting Relative Dates (run from any PowerShell Session)
Here's a quick and fast way of generating relative dates in any format:
(Get-Date).AddDays(-1).ToString('yyyy-MM-dd')<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office"/>

This returns all error events from the System event log in the past 48 hours: (run from any PowerShell Session)

Get-EventLog -LogName System -EntryType Error -After (Get-Date).AddDays(-2)

I'll add more as need arises. Hope these are helpful.

Monday, October 20, 2014

Exporting and Importing Lync Contacts for a User

Exporting Contacts:

To make your user's experience more seamless when moving pools or if, for some reason the user's account has to be deleted and re-created, the following procedure will allow you to export his/her contacts and then import them into the new account.

In Lync 2010:

To export a user’s contact list, we’re going to use a Lync utility called “dbimpexp.exe”. This utility is included in the Resource Kit, so obviously, that must be installed before you can access the utility. Open a command prompt and type, “cd C:\Program Files\Common Files\Microsoft Lync Server 2010\Support”. Once you’re in the support directory, type:

dbimpexp.exe /user:<sipaddress> /sqlserver:domain-LYNCSQL1.domain.com\lync /hrxmlfile:E:\contacts\userfirst_last_contacts.xml

Your display will look like:

C:\Program Files\Common Files\Microsoft Lync Server 2010\Support>dbimpexp.exe /user:first.last@domain.com /sqlserver:domain-LYNCSQL1.domain.com\lync /hrxmlfile:E:\contacts\userfirst_last_contacts.xml
SQL Schema Version: 59
User Data Schema Version : 21
Exporting 1 Homed Resource(s) from database...
100% complete.
Export completed successfully.

You have now exported the user’s contact list.

In Lync 2013:

This functionality has been incorporated into Lync PowerShell. You simply use the following code:

Export-CsUserData -PoolFqdn "pool.domain.com" -FileName "e:\Contacts\First_Last_ExportedUserData.zip" -UserFilter first.last@domain.com

Importing Contacts:

In Lync 2010:

You can restore the contact list now using the process below:

From a command prompt in the C:\Program Files\Common Files\Microsoft Lync Server 2010\Support directory, type:

dbimpexp.exe /import /user:<sip address>/sqlserver:domain-LYNCSQL1.domain.com\lync /hrxmlfile:”<path where the xml file resides>” /restype:user

Your display will look like:

C:\Program Files\Common Files\Microsoft Lync Server 2010\Support>dbimpexp.exe /import /user:first.last@domain.com /sqlserver:domain-LYNCSQL1.domain.com\lync /hrxmlfile:E:\contacts\smcnamaracontacts.xml /restype:user
SQL Schema Version: 59
User Data Schema Version : 21
Importing Homed Resources into database...
1 Resource(s) imported.
Import completed successfully.

In Lync 2013:

This functionality has been incorporated into Lync PowerShell. You simply use the following code:

Import-CsUserData -PoolFqdn "pool.domain.com" -FileName "e:\contacts\First_Last_ExportedUserData.zip" -UserFilter first.last@domain.com

Export from Lync 2010 then Import to Lync 2013:

To import your 2010 contacts into 2013, you must convert the data, as 2013 uses a different format than 2010. Thankfully, there is a 2013 PowerShell cmdlet that does the heavy lifting. To Export a User's Contacts from 2010:

Export from 2010:

To export a user’s contact list, we’re going to use a Lync utility called “dbimpexp.exe”. This utility is included in the Resource Kit, so obviously, that must be installed before you can access the utility. Open a command prompt and type, “cd C:\Program Files\Common Files\Microsoft Lync Server 2010\Support”. Once you’re in the support directory, type:

dbimpexp.exe /user:<sipaddress> /sqlserver:domain-LYNCSQL1.domain.com\lync /hrxmlfile:E:\contacts\userfirst_last_contacts.xml

Your display will look like:

C:\Program Files\Common Files\Microsoft Lync Server 2010\Support>dbimpexp.exe /user:first.last@domain.com /sqlserver:domain-LYNCSQL1.domain.com\lync /hrxmlfile:E:\contacts\userfirst_last_contacts.xml
SQL Schema Version: 59
User Data Schema Version : 21
Exporting 1 Homed Resource(s) from database...
100% complete.
Export completed successfully.

Convert Your Contacts:

Copy the E:\contacts\userfirst_last_contacts.xml file to your 2013 Front End server, then run the following command:

Convert-CsUserData -InputFile "E:\contacts\userfirst_last_contacts.xml" -OutputFile "e:\contacts\First_Last_ExportedUserData.zip" -TargetVersion Current

Import Your Contacts to Lync 2013:

Run the following command to import the converted contact data:
Import-CsUserData -PoolFqdn "pool.domain.com" -FileName "e:\contacts\First_Last_ExportedUserData.zip" -UserFilter first.last@domain.com

Monday, August 5, 2013

Changing StartMode and State for Exchange Services

When patching Exchange Mailbox servers, there is often a need to reboot several times, and we have often stopped and disabled the exchange services to allow for quicker boot times. I have written two powershell scripts as follows:
1.     Stop-ExchangeServices.ps1 - This script will list all the exchange services (DisplayName, StartMode, State), stop the services and then set any that have a StartMode of Automatic to Disabled. It transcripts its every action and makes an entry to the Application Event Log of the server. It also will leave a text file (called Stop-ExchangeServices.txt) transcript "C:\Transcripts" folder of the server.
2.     Start-ExchangeServices.ps1 - This script will list all the exchange services (DisplayName, StartMode, State), set any that have a StartMode of Disabled to Automatic and then starts the services. It transcripts its every action and makes an entry to the Application Event Log of the server. It also will leave a text file (called Start-ExchangeServices.txt) transcript "C:\Transcripts" folder of the server.
Both scripts require elevated privileges, but you should be logged into the server with your admin account, so that won’t be a worry. To prep the mailbox server for patching, first dismount the mail databases, through the EMC or powershell. I will write a robust script to do this later on. Once all databases are dismounted, open a powershell prompt and navigate to the E:\scripts directory. Then type “.\Stop-ExchangeServices.ps1” and hit ENTER. You will see the following:
PS C:\Scripts> .\Stop-ExchangeServices.ps1
Transcript started, output file is C:\Transcripts\Stop-ExchangeServices.txt
Stop-ExchangeServices.ps1 - Exchange Service States at beginning:
DisplayName                                                         StartMode              State
-----------                                                                ---------                  -----
Microsoft Exchange Active Directory Topology         Auto                      Running
Microsoft Exchange Information Store                      Auto                      Running
Microsoft Exchange Mail Submission Service            Auto                     Running
Microsoft Exchange Mailbox Assistants                    Auto                      Running
Microsoft Exchange Monitoring                               Manual                  Stopped
Microsoft Exchange Replication Service                   Auto                      Running
Microsoft Exchange RPC Client Access                    Auto                      Running
Microsoft Exchange Search Indexer                         Auto                      Running
Microsoft Exchange Service Host                            Auto                      Running
Microsoft Exchange System Attendant                      Auto                      Running
Microsoft Exchange Throttling                                Auto                      Running
Microsoft Exchange Transport Log Search               Auto                      Running

Stopping Exchange Services

Disabling Exchange Services
Exchange Service States at conclusion:

DisplayName                                                         StartMode              State
-----------                                                                ---------                  -----
Microsoft Exchange Active Directory Topology        Disabled                Stopped
Microsoft Exchange Information Store                     Disabled                Stopped
Microsoft Exchange Mail Submission Service          Disabled                Stopped
Microsoft Exchange Mailbox Assistants                   Disabled                Stopped
Microsoft Exchange Monitoring                              Manual                  Stopped
Microsoft Exchange Replication Service                  Disabled                Stopped
Microsoft Exchange RPC Client Access                   Disabled                Stopped
Microsoft Exchange Search Indexer                         Disabled                Stopped
Microsoft Exchange Service Host                            Disabled                Stopped
Microsoft Exchange System Attendant                      Disabled                Stopped
Microsoft Exchange Throttling                                Disabled                Stopped
Microsoft Exchange Transport Log Search               Disabled                Stopped

Server is now ready for Patching. For a transcript of the activities, go to  C:\Transcripts\Stop-ExchangeServices.txt
Transcript stopped, output file is C:\Transcripts\Stop-ExchangeServices.txt
Script concluded, Services stopped and disabled.

The transcript file that is written looks like:
**********************
Windows PowerShell transcript start
Start time: 20130804173135
Username  : DOMAIN\AdminAccount
Machine          : YOUR-SERVER (Microsoft Windows NT 6.1.7601 Service Pack 1)
**********************
Transcript started, output file is C:\Transcripts\Stop-ExchangeServices.txt
Stop-ExchangeeServices.ps1 - Exchangee Service States at beginning:
   
DisplayName                                                         StartMode             State
-----------                                                                ---------                 -----
Microsoft Exchange Active Directory Topology       Auto                     Running
Microsoft Exchange Information Store                    Auto                     Running
Microsoft Exchange Mail Submission Service          Auto                     Running
Microsoft Exchange Mailbox Assistants                   Auto                     Running
Microsoft Exchange Monitoring                              Manual                 Stopped
Microsoft Exchange Replication Service                  Auto                     Running
Microsoft Exchange RPC Client Access                  Auto                     Running
Microsoft Exchange Search Indexer                        Auto                     Running
Microsoft Exchange Service Host                           Auto                     Running
Microsoft Exchange System Attendant                    Auto                     Running
Microsoft Exchange Throttling                               Auto                     Running
Microsoft Exchange Transport Log Search              Auto                     Running

Stopping Exchange Services

Disabling Exchange Services

Exchange Service States at conclusion:

DisplayName                                                         StartMode             State
-----------                                                                ---------                  -----
Microsoft Exchange Active Directory Topology         Disabled                Stopped
Microsoft Exchange Information Store                      Disabled                 Stopped
Microsoft Exchange Mail Submission Service            Disabled                Stopped
Microsoft Exchange Mailbox Assistants                     Disabled                Stopped
Microsoft Exchange Monitoring                                Manual                  Stopped
Microsoft Exchange Replication Service                    Disabled                 Stopped
Microsoft Exchange RPC Client Access                     Disabled                Stopped
Microsoft Exchange Search Indexer                           Disabled                Stopped
Microsoft Exchange Service Host                              Disabled                Stopped
Microsoft Exchange System Attendant                       Disabled                Stopped
Microsoft Exchange Throttling                                  Disabled                Stopped
Microsoft Exchange Transport Log Search                 Disabled                Stopped

Server is now ready for Patching. For a transcript of the activities, go to  C:\Transcripts\Stop-ExchangeServices.txt
Transcript stopped, output file is C:\Transcripts\Stop-ExchangeServices.txt

**********************
Windows PowerShell transcript end
End time: 20130804173136
**********************



The Script also makes an entry into the Application Event Log of the server that will look like this:
- <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
- <System>
  <Provider Name="Start-AdobeServices" />
  <EventID Qualifiers="0">1111</EventID>
  <Level>4</Level>
  <Task>1</Task>
  <Keywords>0x80000000000000</Keywords>
  <TimeCreated SystemTime="2013-08-05T01:18:57.000000000Z" />
  <EventRecordID>20005</EventRecordID>
  <Channel>Application</Channel>
  <Computer>YOUR-SERVER </Computer>
  <Security />
  </System>
- <EventData>
  <Data>**********************Windows PowerShell transcript start Start time: 20130804173135 Username  : DOMAIN\AdminAccount Machine      : YOUR-SERVER (Microsoft Windows NT 6.1.7601 Service Pack 1) ********************** Transcript started, output file is C:\Transcripts\Stop-ExchangeServices.txt Stop-ExchangeeServices.ps1 - Exchangee Service States at beginning:
DisplayName StartMode State ----------- --------- ----- Microsoft Exchange Active Directory Topology Auto  Running Microsoft Exchange Information Store  Auto Running Microsoft Exchange Mail Submission Service     Auto Running Microsoft Exchange Mailbox Assistants Auto                Running Microsoft Exchange Monitoring Manual Stopped Microsoft Exchange Replication Service Auto Running Microsoft Exchange RPC Client Access Auto Running Microsoft Exchange Search Indexer       Auto Running Microsoft Exchange Service Host                     Auto Running Microsoft Exchange System Attendant Auto Running Microsoft Exchange Throttling Auto Running Microsoft Exchange Transport Log Search Auto Running Stopping Exchange Services Disabling Exchange Services Exchange Service States at conclusion: DisplayName  StartMode State ----------- --------- ----- Microsoft Exchange Active Directory Topology Disabled Stopped Microsoft Exchange Information Store Disabled Stopped Microsoft Exchange Mail Submission Service Disabled Stopped Microsoft Exchange Mailbox Assistants Disabled Stopped Microsoft Exchange Monitoring Manual Stopped Microsoft Exchange Replication Service Disabled Stopped Microsoft Exchange RPC Client Access    Disabled Stopped Microsoft Exchange Search Indexer Disabled Stopped Microsoft Exchange Service Host Disabled Stopped Microsoft Exchange System Attendant Disabled             Stopped Microsoft Exchange Throttling Disabled Stopped Microsoft Exchange Transport Log Search              Disabled Stopped Server is now ready for Patching. For a transcript of the activities, go to  C:\Transcripts\Stop-ExchangeServices.txt Transcript stopped, output file is C:\Transcripts\Stop-ExchangeServices.txt ********************** Windows PowerShell transcript end End time: 20130804173136 **********************</Data>
  <Binary>0A14</Binary>
  </EventData>
  </Event>

The server is now ready for patching and can be rebooted as many times as necessary to apply the appropriate number of passes from Windows Update. Once all patching is complete, you can run the next script. The Start-ExchangeServices.ps1 script has all the same outputs, except, of course that it sets the StartMode of the disabled services to automatic and starts the services.
Once all patches are applied, open a powershell prompt and navigate to the E:\scripts directory. Then type “.\Start-ExchangeServices.ps1” and hit ENTER. You will see the following:

PS E:\Scripts> .\Start-ExchangeServices.ps1
Transcript started, output file is C:\Transcripts\Start-ExchangeServices.txt
Start-ExchangeServices.ps1 - Exchange Service States at beginning:

DisplayName                                                         StartMode              State
-----------                                                                ---------                  -----
Microsoft Exchange Active Directory Topology        Disabled                Stopped
Microsoft Exchange Information Store                     Disabled                Stopped
Microsoft Exchange Mail Submission Service          Disabled                Stopped
Microsoft Exchange Mailbox Assistants                   Disabled                Stopped
Microsoft Exchange Monitoring                              Manual                  Stopped
Microsoft Exchange Replication Service                  Disabled                Stopped
Microsoft Exchange RPC Client Access                   Disabled                Stopped
Microsoft Exchange Search Indexer                         Disabled                Stopped
Microsoft Exchange Service Host                            Disabled                Stopped
Microsoft Exchange System Attendant                      Disabled                Stopped
Microsoft Exchange Throttling                                Disabled                Stopped
Microsoft Exchange Transport Log Search               Disabled                Stopped


Enabling Exchange Services

Starting Exchange Services

Exchange Service States at conclusion:

DisplayName                                                         StartMode              State
-----------                                                                ---------                  -----
Microsoft Exchange Active Directory Topology        Auto                     Running
Microsoft Exchange Information Store                     Auto                     Running
Microsoft Exchange Mail Submission Service           Auto                     Running
Microsoft Exchange Mailbox Assistants                    Auto                     Running
Microsoft Exchange Monitoring                               Manual                Stopped
Microsoft Exchange Replication Service                   Auto                     Running
Microsoft Exchange RPC Client Access                    Auto                     Running
Microsoft Exchange Search Indexer                         Auto                      Running
Microsoft Exchange Service Host                            Auto                      Running
Microsoft Exchange System Attendant                      Auto                      Running
Microsoft Exchange Throttling                                Auto                      Running
Microsoft Exchange Transport Log Search               Auto                      Running

Server is now restored to normal. For a transcript of the activities, go to  C:\Transcripts\Stop-ExchangeServices.txt
Transcript stopped, output file is C:\Transcripts\Start-ExchangeServices.txt
Script concluded, Services stopped and disabled.

The transcript file that is written looks like:
**********************
Windows PowerShell transcript start
Start time: 20130804212013
Username  : DOMAIN\AdminAccount
Machine          : YOUR-SERVER (Microsoft Windows NT 6.1.7601 Service Pack 1)
**********************
Transcript started, output file is C:\Transcripts\Start-ExchangeServices.txt
Start-ExchangeServices.ps1 - Exchange Service States at beginning:

DisplayName                                                         StartMode             State
-----------                                                                ---------                  -----
Microsoft Exchange Active Directory Topology         Disabled                Stopped
Microsoft Exchange Information Store                      Disabled                 Stopped
Microsoft Exchange Mail Submission Service            Disabled                 Stopped
Microsoft Exchange Mailbox Assistants                     Disabled                Stopped
Microsoft Exchange Monitoring                                Manual                  Stopped
Microsoft Exchange Replication Service                    Disabled                Stopped
Microsoft Exchange RPC Client Access                    Disabled                Stopped
Microsoft Exchange Search Indexer                          Disabled                Stopped
Microsoft Exchange Service Host                             Disabled                Stopped
Microsoft Exchange System Attendant                      Disabled                Stopped
Microsoft Exchange Throttling                                 Disabled                Stopped
Microsoft Exchange Transport Log Search                Disabled                Stopped

Starting Exchange Services

Enabling Exchange Services

Exchange Service States at conclusion:
DisplayName                                                         StartMode             State
-----------                                                                ---------                  -----
Microsoft Exchange Active Directory Topology         Auto                     Running
Microsoft Exchange Information Store                      Auto                     Running
Microsoft Exchange Mail Submission Service            Auto                     Running
Microsoft Exchange Mailbox Assistants                     Auto                     Running
Microsoft Exchange Monitoring                                Manual                 Stopped
Microsoft Exchange Replication Service                    Auto                     Running
Microsoft Exchange RPC Client Access                    Auto                     Running
Microsoft Exchange Search Indexer                          Auto                     Running
Microsoft Exchange Service Host                             Auto                      Running
Microsoft Exchange System Attendant                      Auto                      Running
Microsoft Exchange Throttling                                 Auto                     Running
Microsoft Exchange Transport Log Search                Auto                     Running

Server is now restored to normal. For a transcript of the activities, go to  C:\Transcripts\Stop-ExchangeServices.txt
Transcript stopped, output file is C:\Transcripts\Stop-ExchangeServices.txt

**********************
Windows PowerShell transcript end
End time: 20130804212014
**********************

The Script also makes an entry into the Application Event Log of the server that will look like this:
- <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
- <System>
  <Provider Name="Start-AdobeServices" />
  <EventID Qualifiers="0">1111</EventID>
  <Level>4</Level>
  <Task>1</Task>
  <Keywords>0x80000000000000</Keywords>
  <TimeCreated SystemTime="2013-08-05T01:18:57.000000000Z" />
  <EventRecordID>20005</EventRecordID>
  <Channel>Application</Channel>
  <Computer>YOUR-SERVER.DOMAIN.COM</Computer>
  <Security />
  </System>
- <EventData>
  <Data>**********************Windows PowerShell transcript start Start time: 20130804173135 Username  : DOMAIN\AdminAccount Machine      : YOUR-SERVER (Microsoft Windows NT 6.1.7601 Service Pack 1) ********************** Transcript started, output file is C:\Transcripts\Stop-ExchangeServices.txt Stop-ExchangeeServices.ps1 - Exchangee Service States at beginning:
DisplayName StartMode State ----------- --------- ----- Microsoft Exchange Active Directory Topology Auto  Running Microsoft Exchange Information Store  Auto Running Microsoft Exchange Mail Submission Service     Auto Running Microsoft Exchange Mailbox Assistants Auto                Running Microsoft Exchange Monitoring Manual Stopped Microsoft Exchange Replication Service Auto Running Microsoft Exchange RPC Client Access Auto Running Microsoft Exchange Search Indexer       Auto Running Microsoft Exchange Service Host  Auto Running Microsoft Exchange System Attendant Auto Running Microsoft Exchange Throttling Auto Running Microsoft Exchange Transport Log Search Auto Running Stopping Exchange Services Disabling Exchange Services Exchange Service States at conclusion: DisplayName  StartMode State ----------- --------- ----- Microsoft Exchange Active Directory Topology Disabled Stopped Microsoft Exchange Information Store Disabled Stopped Microsoft Exchange Mail Submission Service Disabled Stopped Microsoft Exchange Mailbox Assistants Disabled Stopped Microsoft Exchange Monitoring Manual Stopped Microsoft Exchange Replication Service Disabled Stopped Microsoft Exchange RPC Client Access Disabled Stopped Microsoft Exchange Search Indexer Disabled Stopped Microsoft Exchange Service Host Disabled Stopped Microsoft Exchange System Attendant Disabled Stopped Microsoft Exchange Throttling Disabled Stopped Microsoft Exchange Transport Log Search Disabled Stopped Server is now ready for Patching. For a transcript of the activities, go to  C:\Transcripts\Stop-ExchangeServices.txt Transcript stopped, output file is C:\Transcripts\Stop-ExchangeServices.txt ********************** Windows PowerShell transcript end End time: 20130804173136 **********************</Data>
  <Binary>0A14</Binary>
  </EventData>
  </Event>


The server is now in a normalized state, and the Mail Databases can be mounted and normal operation can be resumed. 

The Scripts:
Note: Log_this function used in both scripts written by: Som Dutt Tripathi, used from: http://powershell-tips.blogspot.com/2012/10/how-to-write-in-windows-event-log-with.html

Stop-ExchangeServices.ps1
<#
    .SYNOPSIS
    Stops Exchange Services and sets their start mode to disabled. 
   
                Sean McNamara sean.mcnamara@spx.com
                Log_this function written by: Som Dutt Tripathi, used from: http://powershell-tips.blogspot.com/2012/10/how-to-write-in-windows-event-log-with.html
               
                THIS CODE IS MADE AVAILABLE AS IS, WITHOUT WARRANTY OF ANY KIND. THE ENTIRE 
                RISK OF THE USE OR THE RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER.
               
                Version 1 - August 4, 2013
               
    .DESCRIPTION
               
    This script Stops Exchange Services and sets their start mode to disabled. It writes a transcript to the Application 
                Event log on the server. 
               
                .EXAMPLE
    .\Stop-ExchangeServices.ps1 
               
    #>
$SCRIPT_NAME="Stop-ExchangeServices" 
$Script_Path="C:\Sandbox\Stop-ExchangeServices.txt"

function log_this([string]$MESSAGE) { 

                if ( !([System.Diagnostics.EventLog]::SourceExists($SCRIPT_NAME)) ) 
                { 
                                New-EventLog -LogName Application -Source $SCRIPT_NAME 
                } 

                Write-EventLog -logname Application -source $SCRIPT_NAME -eventID 1111 -entrytype Information -message $MESSAGE -category 1 -rawdata 10,20 
}


Start-Transcript -path $Script_Path 

# Show Current Service Status
Write-Host "Stop-ExchangeServices.ps1 - Exchange Service States at beginning:"
Get-WmiObject Win32_Service | where{$_.DisplayName -like '*Exchange*'} | Select DisplayName, StartMode, State
write-host ""

# Stop Exchange Services
Write-host "Stopping Exchange Services"
# Note ** Using -Force because MsExchangeADTopology Service has dependancies and will not stop without it
Get-Service | where{($_.DisplayName –Like ‘*Exchange*’) -and ($_.Status -eq "Running")}  | Stop-Service -Force

# Disable Services
Write-host""
Write-host "Disabling Exchange Services"
Get-WmiObject Win32_Service | where{($_.DisplayName -like '*Exchange*') -and ($_.StartMode -eq "Auto")} | set-Service –StartupType ‘Disabled’
Write-Host""

# Show current Start Mode
Write-Host "Exchange Service States at conclusion:"
Write-Host""
Get-WmiObject Win32_Service | where{$_.DisplayName -like '*Exchange*'} | Select DisplayName, StartMode, State
Write-Host "Server is now ready for Patching. For a transcript of the activities, go to " $Script_Path
stop-transcript

# Write information to Event Log
$Trans_Info = Get-Content $Script_Path  

log_this $Trans_Info

#End Script

Start-ExchangeServices.ps1

<#
    .SYNOPSIS
    Sets Exchange Services StartMode to Auto and starts the service. 
   
                Sean McNamara sean.mcnamara@spx.com
                Log_this function written by: Som Dutt Tripathi, used from: http://powershell-tips.blogspot.com/2012/10/how-to-write-in-windows-event-log-with.html
               
                THIS CODE IS MADE AVAILABLE AS IS, WITHOUT WARRANTY OF ANY KIND. THE ENTIRE 
                RISK OF THE USE OR THE RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER.
               
                Version 1 - August 4, 2013
               
    .DESCRIPTION
               
    This script sets the startup type of any Exchange service that are disabled to automatic, starts the service and writes a transcript
                to the application event log on the server. 
               
                .EXAMPLE
    .\Start-ExchangeServices.ps1 
               
    #>
$SCRIPT_NAME="Start-ExchangeServices" 
$Script_Path="E:\files\Transcripts\Start-ExchangeServices.txt" 

function log_this([string]$MESSAGE) { 

                if ( !([System.Diagnostics.EventLog]::SourceExists($SCRIPT_NAME)) ) 
                { 
                                New-EventLog -LogName Application -Source $SCRIPT_NAME 
                } 

                Write-EventLog -logname Application -source $SCRIPT_NAME -eventID 1111 -entrytype Information -message $MESSAGE -category 1 -rawdata 10,20 
}


Start-Transcript -path $Script_Path

# Show Current Service Status
Write-Host "Start-ExchangeServices.ps1 - Exchange Service States at beginning:"
Get-WmiObject Win32_Service | where{$_.DisplayName -like '*Exchange*'} | Select DisplayName, StartMode, State
write-host ""

# Enable Services
Write-host""
Write-host "Enabling Exchange Services"
Get-WmiObject Win32_Service | where{($_.DisplayName -like '*Exchange*') -and ($_.StartMode -eq "Disabled")} | set-Service –StartupType ‘Auto’
Write-Host""

# Start Exchange Services
Write-host "Starting Exchange Services"
Get-WmiObject Win32_Service | where{($_.DisplayName –Like ‘*Exchange*’) -and ($_.StartMode -eq "Auto")} | Start-Service 

# Show current Start Mode
Write-Host "Exchange Service States at conclusion:"
Write-Host""
Get-WmiObject Win32_Service | where{$_.DisplayName -like '*Exchange*'} | Select DisplayName, StartMode, State
Write-Host "Server is now restored to normal. For a transcript of the activities, go to " $Script_Path
Stop-transcript

# Write information to Event Log
$Trans_Info = Get-Content $Script_Path  

log_this $Trans_Info


Write-host "Script concluded, Services enabled and started."

#End Script