Skip to Content

powershell

Powershell related posts.

PowerShell neat function tricks for free

Finished going through a recent NA TechEd2012 video Turn PowerShell Commands into Reusable CLI and GUI Tools by Don Jones.

Some of the advice I sort of knew and and followed.

  • I knew how to make a PowerShell script module.
  • I knew about the .SYNOPSIS, .EXAMPLE stuff.
  • I sort of knew about using the param/paremeters though I didn't really understand it, just used it in a limited framework copy/paste sort of way. After the video I have a much better handle on how to leverage it.

What I really got out of it though was some new and better ways to leverage built in switches more effectively which I had no real clue about.

  • Documenting not though #comments but using Write-Verbose so that the -verbose switch works and you can see what a script is doing without pulling it up in an editor.
  • useing write-debug to leverage the -debug switch for your functions
  • Using try/catch in your scripts so if a first part fails, then it skips the next parts on that one server.
  • A clear example on how to use Write-Object with a PSObject which I just used without understanding it.
  • Using $errorLogPath to create an error log

Below is the script he built for the presentation with the various examples. Save the below code as MyTools.psm1 and load it up. Then run it against multiple systems without switches and using the -debug and -verbose switches. Better yet, go watch the video.

TechEd 2012 Videos are out

Microsoft provides some of the best free technical resources out there for self training and keeping aware of changes in their technology.

TechEd sounds like a great event which I have never gotten to go to, but through the magic of the Internet and Microsofts Channel 9, you have access to the presentations.

If you are new to PowerShell I recommend viewing them with Ed Wilson's video series.

TechEd North America 2012

1. Windows PowerShell Crash Course
2. 5 part set of PowerShell video's from Ed Wilson as well
3. Turn PowerShell Commands into Reusable CLI and GUI Tools

Then check out these as well.
PowerShell Remoting in Depth
Advanced Automation Using Windows PowerShell 3.0

This year is an insane year for change. The changes in Microsoft's technology is vast and looks to be seriously fun.

Using PowerShell to add numbers in a text file

Recently had a conversation where someone needed help adding up the last column in a tab separated text file. It looked like a calling plan report. In channel tons of people brought up all sorts of *nix based tools which I thought odd since it was a Windows based IRC channel.

I've occasionally had a need for something like this anyway so poked at it for a few minutes and came up with the below one liner which I will leave here so my bad memory can find it again.

import-csv .\test.txt -Header "date","something","type","cost" -delimiter "`t" | Measure-Object cost -sum
 
Count    : 12
Average  :
Sum      : 2.38
Maximum  :
Minimum  :
Property : cost

Since the text file doesn't have any header rows, you have to 'add them' through the import command.

Here is that test.txt file example.

14-Dec-11 	0-234 	Long Distance 	0.32
14-Dec-11 	0-960 	Long Distance 	0.04
09-Dec-11 	1-237 	Directory Assistance Call Comp 	1.25
09-Dec-11 	1-960 	Directory Assistance Call Comp 	0.15
22-Nov-11 	0-234 	Long Distance 	0.12
22-Nov-11 	0-960 	Long Distance 	0.01
16-Nov-11 	0-234 	Long Distance 	0.16
16-Nov-11 	0-960 	Long Distance 	0.02
12-Nov-11 	0-234 	Long Distance 	0.20
12-Nov-11 	0-960 	Long Distance 	0.02
11-Nov-11 	0-234 	Long Distance 	0.08
11-Nov-11 	0-960 	Long Distance 	0.01

Update:
A user (spade) in #PowerShell contributed this approach as well.

gc data2.txt | % {$sum=0} { $sum += ($_ -split "`t")[-1] } { $sum }

PowerShell-Windows.Forms RDP Launcher

In a given environment there can be many short term solutions that over time grow into long term headaches. We have a situation where we need to provide access to RDP sessions through Citrix. The original solution by previous admins was to create a custom RDP icon configured to the server in question and publish it in Citrix. This resulted in a rather 'busy' screen in Citrix and intermittant update issue.

A co-worker asked about PowerShell doing this and I said something off handed about Windows Forms to generate a menu selection should be do-able. He found a nice Windows.Forms example and then did a hard coded elseif menu (which seemed as bad as all the links to me). I came up with an improvement where we pulled the server/ip list from a file and used Select and Eric Woodford added a nicer $choice option with the added bonus of turning the line for launching RDP into a variable. This is good because we have a similar issue with putty links. This should wipe out 80 some odd custom published Citirx apps.

SIM IE9 Trusted Sites

HP SIM with IE9 really wants it to be in the Trusted sites list for some functioanlity to work reliably. However, maybe there is a GPO preventing you from adding to the trusted sites list yet you need to get to something 'now'. Grrr...

Let's go with a hypothetical SIM server of SimServerName.

PowerShell launched with administrator credentials (you can do this manually too)

PowerShell-Check-Mailbox

We are beginning to transition to Windows 7 across the Enterprise, which is nice and change is often a time to revisit some basic tools and short comings.

Our support center currently fields basic calls from users on mailbox issues. Currently they are using the Exchange 2007 Admin tools which after about 6 clicks gets them most of the information they need, usually. It does miss mailbox size limits as generally that is set on a store level and on the account screen says merely 'default' and we have different sizes depending on job function.

We also run into the occasional issue of multiple accounts found based on our naming policy those then have to be filtered out based on looking closer at account infomration. I hacked together this quick function to provide basic information with one command and no need for the GUI.

Here's the output

PS:\> Check-MailBox jdoe
 
        Display Name:  John Doe
      Mailbox Server:  mailserver01
 
       Issue Warning:  219 MB
       Prohibit Send:  244 MB
Storage Limit Status:  BelowLimit
 
Current Mailbox Size:  220 MB

Syndicate content