You are currently browsing the category archive for the ‘Powershell’ category.

Split-Path is a very useful command which allows you to do string manipulation with file paths.  It chops up paths into Parent/Child directory names, file names and can also determine if the path in question is relative or absolute etc.  Check out http://technet.microsoft.com/en-us/library/dd315377.aspx for more details.

However, there are times when output from another command is in a format similar to:

a.b@example.com\Some_GUID
OR
child.example.com/Some_OU/Display_Name

Note: In example two, they are forward slashes.  Your goal here could be to extract the mailbox address from the first example or Domain/OU information from the second.  Instead of doing the usual string manipulation on this output, you could make it easier by using Split-Path to split the information by either piping the relevant property into the split-path command or using something similar to:

split-path((get-user -anr $_.Address).Identity)

where “Address” is the mail address(es) passed to the command by some mechanism e.g. Import-CSV or ForEach etc.  This particular example will give you the Domain/OU information for that user.

It’s not the most obvious use of Split-Path and certainly not the most authentic but is definitely easy and effective in most cases.  Have a go and see what happens!

I like exporting and sending the results of my Powershell scripts via SMTP mail.  That obviously require some sort of temporary location to hold my data while I am working on things.  However, I also try to stay away from dumping the temporary contents into TEMP or TMP.  I want to keep my junk separate from everyone, know exactly what should be in my temporary environment at a given time and clean everything up when I am done.

To achieve that, I always have a temporary location in my code where I keep my junk.  When I started to use that in conjunction with my mail-enabled scripts, I found that my temporary location was not being cleaned up consistently.  It was a bit hit-and-miss!  For example: Suppose you have a script that has a temporary file location in it but also has mail sending code similar to this:

$SMTPClient = new-object system.net.mail.smtpclient
$MailMessage = new-object system.net.mail.mailmessage
$SMTPClient.Host = $SMTPHost
$MailMessage.From = $Sender
$MailMessage.To.Add($Recipients)
$MailMessage.Subject = "Some Random Subject" 
$MailMessage.Body = "Some Random Body"
$SMTPClient.Send($MailMessage)

You may find that if you try to cleanup your temporary files and folder after sending the mail, it might not work consistently.  It’s also possible that upon debugging, you come across an error: “The process cannot access the file ‘somefile.csv’ because it is being used by another process” or something similar.

This is because good .NET programming (which Powershell is based on) requires releasing the resources and file handles after using a class, in this case, after sending the mail.  So, to avoid the problem, simply add the method:

$MailMessage.Dispose()

after sending the mail and before the cleanup code.  This will release the resources that are held up in memory and the cleanup should be successful.

Hope this helps!

Categories

Follow Me on Twitter

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 190 other subscribers