Jan 16

Write out all the available colors Write-Host can use in one line.

Also, you can find some great stuff here from the ScriptingGuy in Use .NET Classes to Configure the Windows PowerShell Console.

Aug 20

Writting to Excel

I like to watch the twitter feed for #PowerShell, It’s amazing the stuff you learn! Someone recently posted this:

can anyone point me to posts, scripts where #powershell can generate/populate Excel spreadsheets with values I (or the code) can pass? #PoSH

Always eager to help, Here is my write-ExcelCell function

This function has it’s limitation (not being able to write past the Z column) and I’m sure there are others, but for the 80% of Excel writting, it should be fine.

Here is how to use it:

Enjoy!

Jun 17

A better logging method

Recently, I was asked to make some changes to lots of users in Active Directory. For output, they wanted a log of everything that was changed and a log of where changes failed. Of course, I wanted a third log with verbose output of everything that was happening so, if need be, I could go back and fix anything.

Another thing I wanted to do was not write 5 times a second. Rather than slow down the process of making changes to users, I wanted to collect strings for the logs and write out every time I collected X number of strings.

To do this, I created a new Type using C# code. I’ve been reading Windows PowerShell for Developers (Dougals Finke) which has been an eye-opener to say the least.

Here is the code I came up with…

This type compiles a “Logger” object which has a few properties and a few methods.

Logger Properties

Type Name Description
Int count the count of strings currently collected
Int MaxLines Number of lines (strings) to collect before being forced to write to the file.
Sting LogFile the logfile to write to (including path).

Logger Methods

Name Description
Logger (Constructor) Sets default values for the object
Add Adds a string to the object. All strings will be stored until either a “flush” command is called or the ‘maxLines’ limit is reached.
AppendLog Appends the collection of strings to the specified log file
Flush Writes all currently collected strings to the log file.
SetLogFile Sets the logfile to be written to.
SetMaxLines Sets the maximum number of lines to store until being forced to write to the log file.

Example usage

Results

As a result of this, I was able to add this type to the script I was creating and create 3 logger objects:

Logging to any one log was easy, just use the example above. If I wanted to log to all the logs, like setting the start point for the running of the script I would do something like this:

The $logPrefix was a scriptblock I specified earlier on as

This way each line wold have a date/time header.

May 24

A better “test file” maker

Recently I needed to do some testing in Exchange 2010 OWA using different sized files. Rather than e-mail some file I found on my system which happened to be “close enough” to the right file size, I decieded to write a Powershell type file which I could use to create these files.

I wanted to make sure I could create some dynamicly sized files so I created a type called TestFiles.

Here is the code I came up with:

The nice thing about this is if I want to create some other kind of test file, I can just add the code for it and all my test files can be found in one place. :d

Here is an example usage:

Jan 22

connecting disabled mailbox works… mostly.

Hi all,

Recently I was playing with some mailboxes in Exchange 2010 and I disabled a test mailbox. I then went on to do some other things and a little while later decided to re-connect that disabled mailbox. After the account was connected and everything looked good, I sent a test message to that account. When I did, I got the following error:

Diagnostic information for administrators:
Generating server: ExCAS01.ad.bu.edu
oitmsg@bu.edu #554-5.2.1 mailbox disabled 554 5.2.1 STOREDRV.Deliver.Exception:AccountDisabledException.MapiExceptionMailboxDisabled; Failed to process message due to a permanent exception with message Cannot open mailbox /o=MyOrg/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Configuration/cn=Servers/cn=ExMBX01/cn=Microsoft System Attendant. 16.55847:C1000000, 17.43559:0000000020010000000000001F00000000000000, 255.23226:00000000, 255.27962:FE000000, 255.17082:6C090000, 0.26937:00000000, 4.21921:6C090000, 255.27962:FA000000, 255.1494:00000000, 255.26426:FE000000, 0.22086:05000780, 0.25876:EC030000, 4.7588:0F010480, 0.25840:EC030000, 4.6564:0F010480, 0.56333:00000000, 4.6372:05000780, 0.24916:1DFAFFFF, 4.6276:05000780, 0.51399:0F010480, 4.2567:6C090000, 0.19452:0F010480, 4.2199:6C090000, 0.56415:05000780, 4.48223:6C090000, 4.17097:6C090000, 4.8620:6C090000, 255.1750:2C000000, 0.26849:0F010480, 255.21817:6C090000, 0.26297:0E000000, 4.16585:6C090000, 0.32441:0F010480, 4.1706:6C090000, 0.24761:0F010480, 4.20665:6C090000, 0.25785:00000000, 4.29881:6C090000 ##
Original message headers:
To fix this error simply run the Clean-MailboxDatabase command against the database where the mailbox exists.
My assuption is that the system hasn’t removed the disabled flag on the mailbox itself and running Clean-MailboxDatabase takes care of this. Once I did this, all was fine.
Dec 04

Viewing reports in Windows Server 2012 WSUS

Recently, I setup a WSUS server in our environment but was unable to view reports. I constantly recieved this message:

WSUS Report Error Message

Clicking on the link, I would get a link to a page that said “The document that you are attempting to access is not available yet…” Understandable, Windows Server 2012 is new. So, I go look for Microsoft Report Viewer 2008 Redistributable on Microsoft Download. I download it and attempt to install it but then I get this error during the installation:

“Shouldn’t there be a 2012 version of this? Something where I don’t have to install some 2008 software?” I ask myself. So then I go looking for a 2012 version of the same software, maybe that will solve my issue. Ah ha! I find the Microsoft Report Viewer 2012 Runtime CTP. Quickly, I install and quickly I get this new error:

Seems I need to go back to the 2008 version. I’m using the default windows database not SQL for my WSUS server. Easily solved. Go back to the Install Roles and Features

 Step through the Wizard until you get to features and check the box next to “.Net Framework 3.5 (includes .Net 2.0 and 3.0) under “.NET Framework 3.5 Features.”

Finish up the install of .NET Framework 3.5 and retry the Microsoft Report Viewer Redistributable 2008…

Voi la!

And here is my report:

Dec 04

Introducing Windows 8: An Overview for IT Professionalls

Microsoft Press has made their “Introducing Windows 8: An Overview for IT Professionalls” book free for download.

Download PDF, Mobi or ePub versions of this book here:

http://blogs.msdn.com/b/microsoft_press/archive/2012/11/13/free-ebook-introducing-windows-8-an-overview-for-it-professionals-final-edition.aspx

Topics include:

• Performance, reliability, and security features

• Deployment options

• Windows Assessment and Deployment Kit

• Windows PowerShell™ 3.0 and Group Policy

• Managing and sideloading apps

• Internet Explorer® 10

• Virtualization, Client Hyper-V, and Microsoft Desktop Optimization Pack

• Recovery features

Aug 01

PowerShell: get group membership using .NET

Hi All,

So recently I needed to get all the members of a group and all the members of the groups that a members of each of those groups… right on down the line. My head began to swim thinking about writting a function to get the members of one group, then degermine if a member was a user, computer, or another group, then getting the members of that group and so on, all while collecting the users in some form of global collection. really, it is messy!

Add to that, the fact that I don’t have a 2008 domain so everything here is of no value.

So, off I started my hunt and came across the .NET object GroupPrincipal in System.DirectoryServices.AccountManagement.

First thing I need to do is load the assembly so I have access to GroupPrincipal.

GroupPrincipal has a FindByIdentity method to get a group by name. But, the FindByIdentity requires a PrincipalContext.

PrincipalContext is defined as:

This says to me that it is some kind of logical grouping for where the FindByIdentity should be looking. Looking into the simplest of the contstructor for the PrincipalContext “PrincipalContext(ContextType)” says it requires a ContextType. The Context Type is an enum for Machine, Domain, or ApplicationDirectory. For what I’m doing, I want the Domain option.

So, in PowerShell I need to create a new ContextType object for the domain.

Then, I need to use that to find my starting group by identity (name).

Lastly, I need to get the members of the group

Well, that’s great, but it doesn’t give me all the members of the group! Looking at that GetMembers member, I can see there are two signatures. GetMembers() & GetMembers(bool recursive)! So now, instead of $group.GetMembers() |ft -auto, I can use $group.GetMembers($true) |ft -auto! and Poof! There is everyone in that group and that group’s groups straight on down the line!

Sweet!

Now, here it is in it’s entirety:

May 24

Sort PowerShell output by property name

Using the exchange command ‘get-mailbox’

or the single-line version…

–Gene

Feb 27

Exchange 2010 New-MoveRequest and the dumpster

So, you’re migrating from Exchange 200* to Exchange 2010 and you’re using the new fangled “New-MoveRequest.” You might have some questions about the dumpster. You know that thing that if the Admins could just get people to understand it, you would never have to restore from backup except for practice!

If you wanted to find out about New-MoveRequest, you might be tempted to go to the on-line help at New-MoveRequest. But there you wouldn’t find any information about the dumpster itself, only information about the command.

You may then find yourself at Understanding Move Requests which talks about alot of stuff about move requests. You may even find an area that talks about limitations in “Move-Mailbox” (Exchange 2007) which states “The Dumpster folder isn’t moved with the mailbox.” That’s all well and fine, but we want to know about the 2010 Move Request. A few lines down on that same Understanding Move Requests, you may find “Advantages of Move Requests” Where it doesn’t say anything about the dumpster. You might then assume that if it is a limitation in 2007, it must not be a limitation in 2010. And, you’d be right! In Exchange 2010, the New-MoveRequest will move the dumpster…

… if you’re moving from 2010!

If you’re moving from 200*. Sorry, no dumpste for you! Have a look at Troubleshooting Mailbox Moves under “The mailbox dumpster size exceeds the target quota.” A little ways down under “Resolution” you’ll find a line that says “Move requests don’t support moving an Exchange 2003 or Exchange 2007 dumpster to Exchange 2010.” That’s your answer!

Here is a blog entry from howExchangeWorks.com that spells this out quite clearly.

–Gene