Monthly Archives: March 2011

PowerShell ISE and ASCII Files

If any of you have ever created a file in the PowerShell ISE and then wondered why it doesn't look right when you open it in some other editor, or try to run it, you've just run into a "feature" that I wish were not the default--New files are saved as Unicode files. There's no problem with you're working with an existing ASCII file, the ISE will preserve the format. But if you're creating a new file, it saves as Unicode. Now this has always annoyed me, but I never really did anything about it, just worked around it when I got hit with it. But the other day I came across an old blog post from Doug Finke, a fellow PowerShell MVP. He adds the following line to his ISE $profile:

$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Save File ASCII",{$psISE.CurrentFile.Save([Text.Encoding]::ASCII)}, $null) | out-null

That's it. One line of code, and there's a new menu item on the AddOns Menu to solve the problem. Sweet.

(For those who haven't created an ISE $profile yet, it goes in the same directory as your regular PowerShell $profile, but has the filename: Microsoft.PowerShellISE_profile.ps1. )

Charlie.

Starting Exchange Services after a Power Failure

In my environment, with a virtualized SBS 2011 Standard, there are occasionally Microsoft Exchange 2010 services that don't properly restart if there has been an abrupt power failure on the Hyper-V host. (Don't ask.)

Now, of course, the first time this happened, I just logged in to the server and started the services. But when it happened again, it's time to write a script. And it was a fun script, since it uses WMI and PowerShell remoting and other fun stuff.

# Script to start Exchange services on SBS 2011 Server after power failure
#
# Accepts a parameter of the exchange server name, but defaults to SRV2 if none entered
# Assumes you are logged in to the domain with Domain Admin credentials
#
# Created: 14/03/2011 by Charlie
# ModHist: 15/03/11 -switched to using WMI in the session to get StartMode
#
#
param ($ExchSrv = "SRV2" )

# first, open a session to the Exchange server
$srv = New-PSSession $ExchSrv

#Now use Invoke-Command with -Session
Invoke-Command -Session $srv -scriptblock {
   $exsvc = gwmi win32_service | Where-Object {$_.Name -like "MsExch*" `
     -and $_.StartMode -eq "Auto"   -and $_.State -eq "Stopped" }
   if ($exsvc ) {
      foreach ($svc in $exsvc ) {
         Start-Service $svc.name
      }
   }
}

The if statement in there is to prevent an error if all the services are running. Of course, for this script to work as it's written, you'll need to run it from a workstation in the SBS domain, and you'll have to enable PowerShell remoting on both the server and the client. If you haven't done that yet, I've posted a quick setup guide on TechNet.

I've posted this script up to the Microsoft Script Center, so if you have comments or suggestions to improve it, please comment there.

Charlie.