Tag Archives: Scripting

PowerShell: Simple GUI Message Boxes

One way to build simple message boxes in PowerShell is to “borrow” from .NET COM objects. Basically you initialize the object…(a.k.a. load the code for generating a messagebox), and then you create instances of the object which are the actual message boxes. These messagebox code may  be familiar to anyone who has programmed in vbScript, or any of the Microsoft programming languages such as Visual Basic or C#.  The boxes can look pretty good, even though the coding is a bit arcane.

To get started, initialize the messagebox object. You only need to do this once per session.

PS> $wshell=New-Object -comObject Wscript.Shell

Now you can make message boxes until the cows come home.

PS> $wshell.Popup(“Hi…This is a lovely messagebox”,0,”My Window Title”)

messagebox1

You can have multi-line boxes by including the newline (back-tick+’n’) to separate lines.

PS> $wshell.Popup(“Hi! A list of shipping addresses will appear in the next window.`n

If you need to edit the addresses then… `n

  1. Go back to BrightPearl`n
  2. Make the address changes.`n
  3. Run this program again.”,0,”Send To Warehouse”)

MessageBox2

The Buttons

You can have a buttons which return an integer based on which button is pressed.

In PowerShell, the button combinations are designated by integers:

0: OK
1: OK, Cancel
2: Abort, Retry, Ignore
3: Yes, No, Cancel,
4: Yes, No,
5: Retry, Cancel

The parameters for showing a window are:

<messagetext>, <duration>, <window title>, <button integers>

PS> $wshell.Popup(“Hi…This is a lovely messagebox”,0,”My Window Title”,1)

The first parameter is the message that you want to appear in the box. This is a string.

The second parameter “duration”, is an integer that specifies how long you want the messagebox to stay open if there is no activity from the user. If you specify ‘0’, then the box stays open forever. Note that your script is completely paused when this is the case. If you use a positive integer, that is the length in seconds that the messagebox stays open.

The third parameter is a string that is the title of the box. This appears in the top of the window frame.

In the example above, the messagebox call specifies “1” for the OK and Cancel button combination. When the user clicks on a button the messagebox closes, and returns an integer, in this case, 1 for OK or 2 for Cancel. The integers are displayed on the PowerShell command line.  (Technically, the integers are returned to the pipeline).

Note that these returned integers are have nothing to do with the integer used to determine which buttons are displayed. (Why do I have to remind myself of this?) 

Button Return Values

OK Cancel Abort Retry Ignore Yes No
1 2 3 4 5 6 7

Instead of having the result go to the pipeline, you can store it in a variable for further use. Capture the result variable by assigning the message box to a variable.

 PS> $result=$wshell.Popup(“Hi…This is a lovely messagebox”,0,”My Window Title”,1)

PS> $result

The Icons

Messageboxes can also have an icon. There are four to choose from, and each has a designated integer.

Stop 16
Question 32
Exclamation 48
Information 64

Now, here’s the arcane part.  To display an icon,  you take the icon’s numeric value and add it to the numeric value of the button numeric value.  So, for example to place a Stop icon on our sample message box with OK and Cancel buttons  we add 16 to 1. Its not like they couldn’t provide another parameter?

PS> $result=$wshell.Popup(“Hi…This is a lovely messagebox”,0,”My Window Title”,65)

Finally:

If you get an error message, when putting up a messagebox, like the one below…   it means that you didn’t run the New-Object cmdlet one time in your PowerShell session, prior to making a call to put up a messagebox.

PS> $wshell=New-Object -comObject Wscript.Shell

Having run that once, you can create as many messageboxes as you need within that particular PowerShell session.

You cannot call a method on a null-valued expression.
At line:1 char:1
+ $result=$wshell.Popup("Hi…This is a lovely messagebox",0,"My Window Title",65)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

MessageboxFinal

Advertisements

Powershell: Basic Address List Processing

One of the great things that was always a little bit fun (well, for some us… what passes for fun) is processing lists using Unix/Linux shell scripts and tools. When you are in practice, you can perform miracles; leap tall buildings in a single bound. Let’s see what we can do with a file using Powershell commands.

I received a file of “lapsed” donors. These are donors to our organization that gave to one of our campaigns in the past, but haven’t given recently. We know they were our friends in the past, and we think they still are, so we’d like to contact them for a one-time mailing, and/or add them to our master mailing lists.

The file is named “lapsed.csv”. The csv extension suggests this is a text file with a “comma separated values. And indeed, if I look at this file, at the powershell prompt, it is easily visible.

PS C:UsersLarrypowershell> cat lapsed.csv

This shows a comma delimited file with a header line:

Addressee,first,spouse,Organization,Street,City,State,ZIP
Joe Blow, Joe,,,123 W 57TH St Apt 123,New York,NY,10019
Jill Smith,Jill,Howard Services,123 Poor Farm Rd,Colchester,VT,05446

Our standard is:

Org, fname, lname, address1, address2, city, state, zip

So, among other things, we’re going to want to change the order of the information in the fields, as well as the field names.

First thing to do is to import the file into a single PowerShell variable, and then see what we’ve got.

PS>$lapsed= Import-CSV lapsed.csv
PS>$lapsed

Addressee : Joe Blow
 first : Joe
 spouse :
 Organization : Paul C Bunn Elementary
 Street : 123 W 57TH St Apt 123
 City : New York
 State : NY
 ZIP : 10019
Addressee : Jill Smith
 first : Jill
 spouse :
 Organization : Howard Services
 Street : 123 Poor Farm Rd
 City : Colchester
 State : VT
 ZIP : 05446

Using import-CSV, the file is converted into a series of custom objects with members that correspond to the existing field names …so further manipulations can be done using object manipulations, instead of just a bunch of searching and replacing. (Well that’s the theory anyway).

We can find out the number of records we have by looking at the count attribute.
 PS>$lapsed.count
 553

And we can see the “members” or field names of each record by using Get-Member

PS>$Lapsed |Get-Member

TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
 ---- ---------- ----------
 Equals Method bool Equals(System.Object obj)
 GetHashCode Method int GetHashCode()
 GetType Method type GetType()
 ToString Method string ToString()
 Addressee NoteProperty System.String Addressee=Joe Blow
 City NoteProperty System.String City=New York
 first NoteProperty System.String first=Joe
 Organization NoteProperty System.String Organization=Paul C Bunn Elementary
 spouse NoteProperty System.String spouse=
 State NoteProperty System.String State=NY
 Street NoteProperty System.String Street=123 W 57TH St Apt 123
 ZIP NoteProperty System.String ZIP=10019

Ok…we knew the field names before by just looking at the raw .CSV file. But now we have seen how the Import-CSV command converts the .CSV file to an array of objects with a type of PSCustomObject.  Since each address record is an object, the way we manipulate it is to use object methods.

1. Add a lname field for the last name
PS>$lapsed | Add-Member -Name “lname”

That takes the $lapsed table and pipes it to the Add-Member cmdlet. This adds the the member to EACH object in the table, rather than adding it to the table itself.

2. Add a fname and lname fields for the first and last name
PS>$lapsed | Add-Member -Name “fname” -MemberType NoteProperty -Value “”

PS>$lapsed | Add-Member -Name “lname” -MemberType NoteProperty -Value “”

Now the fields look like this:
Addressee : Joe Blow
 first : Joe
 spouse :
 Organization : Paul C Bunn Elementary
 Street : 123 W 57TH St Apt 123
 City : New York
 State : NY
 ZIP :
 lname : ""
 fname : ""

3. Copy data from the first name and organization fields to their new fields.
PS>$lapsed | ForEach-Object ($_.fname) {$_.fname=$_.first}
PS>$lapsed | ForEach-Object ($_.org) {$_.org=$_.Organization}
This leaves us with a record looking like this.

Addressee : Joe Blow
 first : Joe
 spouse :
 Organization : Paul C Bunn Elementary
 Street : 123 W 57TH St Apt 123
 City : New York
 State : NY
 ZIP : 10019
 lname :
 fname : Joe
 org : Paul C Bunn Elementary

6. Having copied the data from the old fields to the new ones, we can delete the old fields.
There isn’t a cmdlet to remove an object member, so the you have to use a different nomenclature. (Note To Self… opportunity to make a custom cmdlet?)

PS> $lapsed | ForEach-Object ($_){$_.PsObject.Members.Remove(‘Organization’)}
PS>$lapsed | ForEach-Object ($_){$_.PsObject.Members.Remove(‘first’)}
PS>$lapsed | ForEach-Object ($_){$_.PsObject.Members.Remove(‘spouse’)}

Now a typical record is starting to look much more like what we want it to look like.

 Addressee : Joe Blow
 Street : 123 W 57TH St Apt 123
 City : New York
 State : NY
 ZIP : 10019
 lname :
 fname : Joe
 org : Paul C Bunn Elementary

7. We still need to pick out the last name from the Addressee field.
There might be a couple approaches to this using regular text search methods:
a. Given a string “Joe Blow”, we could find the first blank character, and then take anything to the right of if as our last name.
b. We could start at the right hand side and count backwards until we get to a space.
c. If there are word functions, we can choose the right-most word in the string.
d. Use the split function. This is what we’ll use.

PS> $lapsed | ForEach-Object ($_){$_.lname=$_.Addressee.split()[-1]}

8. Finally, we can eliminate the “Addressee” field
PS> $lapsed | ForEach-Object ($_){$_.PsObject.Members.Remove(‘Addressee’)}

Street : 123 W 57TH St Apt 123
 City : New York
 State : NY
 ZIP : 10019
 lname : Blow
 fname : Joe
 org : Paul C Bunn Elementary

9. Time to export back to a CSV file.
PS> $lapsed | Export-Csv -Confirm -Path “C:UsersLarryPowershellulapsed.csv” -NoTypeInformation

10. Almost done. The one frost is that the field order isn’t exactly as I’d like. This can be fixed with the Select-Object cmdlet.

PS > $lapsed | Select-Object -Property fname,lname,org,street,city,state,zip |
Export-CSV -Path “C:UsersLarryPowershellulapsed.csv” -NoTypeInformation

Recall when typing a string of commands with a pipeline, the pipe delimter will also act as a “newline”, so you can break the command up over the course of a couple of lines and have the full pipeline execute as one command.

Notes:

As they say on public television: “Many thanks to the following:”

“jrv” on Microsoft Technet
http://goo.gl/1Kyw07

“Root Loop” on StackOverflow
http://stackoverflow.com/questions/22029944/batch-or-powershell-how-to-get-the-last-word-from-string

More Info:
http://windowsitpro.com/powershell/csv-excel-or-sql-it-doesnt-matter-powershell

More about the split function in Powershell help
PS> Get-Help about_Split

PowerShell Functions II – Output function results as a hash table

In a previous post I wrote a butt-simple function with three parameters, called it a couple ways, and talked about how the function returns its data either as a single string or as an array of variables. Before leaving this, I’m going to experiment about returning data as a hash table.

function Get-ReallySimple($fname,$lname,$age) {

$OutTable=@{"FirstName"=$fname;"LastName"=$lname;"Age"=$age}
return $OutTable

}

This prints out nicely, except it is in the wrong order.

 PS>Get-ReallySimple Joe Dokes 32

 Name                           Value
----                            -----
Age                            32
FirstName                      Joe
LastName                       Dokes

By adding the ordered keyword in front of the hash table definition, we can get the hash table to print in the order in which we asked for the function’s input paramenters.

function Get-ReallySimple($fname,$lname,$age) {

$OutTable= [ordered] @{"FirstName"=$fname;"LastName"=$lname;"Age"=$age}
return $OutTable

}

PS>Get-ReallySimple Joe Dokes 32

Name                           Value
—-                                —–
FirstName                     Joe
LastName                     Dokes
Age                               32

Since the result is a hash table we can also return a portion of the table using dot notation. We surround the functional call with parentheses to force the call to be evaluated first.

PS>(Get-ReallySimple Joe Dokes 32).FirstName

Joe

Of course instead of the the parentheses we probably should assign the call to a variable; and then dot notate that.

$Zilch=Get-ReallySimple Joe Dokes 32

$Zilch.FirstName
Joe

There is  an Output Type attribute that can be applied to a function, but this appears to be cosmetic. It doesn’t enforce or do any error checking.  If your function returns a string, and Output type says “hashtable”, nothing happens; the function still return the string.  According to the help documentation the purpose of the Output Type attribute is to provide documentation; but when I queried with Get-Command Get-ReallySimple,  it returned nothing for the output.

Windows Remoting

Remoting allows you to log into the command line of the target machine. It is similar to the SSH command on Linux, in that it provides access only to the command line.

It has to be set up on beforehand by using PowerShell with Admin privileges on both the controlled and controlling machine.

Note:  You will eventually need to the name of your machine. You can find this out with the following command:

Get-ChildItem env:ComputerName

Here are the steps to set it up:  At the PowerShell prompt on each machine

1. Turn on PSRemoting.

PS> Enable-PSRemoting  -Force  # The Force parameter eliminates a lot of annoying questions.

2. For machines that are not on a domain-based nework, you need to configure the trusted hosts lists on each machine.

PS> Set-Item wsman:localhostclienttrustedhosts *

This allows any computer to connect to this machine.  If you want to restrict the trusted hosts, you can use a comma-separated list of IP addresses, or computer names.

3. Restart the WinRM service

PS> Restart-Service WinRM

Repeat the three steps above for any machine that you wish to access via Remoting.

To test the connection:

Note:  You will eventually need to the name of your machine. You can find this out with the following command:  Get-ChildItem env:ComputerName

Test-WsMan <computename>

If the connection is made you’ll get a reply…if not then the connection will appear to hang at the prompt.

Now for the real deal:

To execute a single command from the source machine on the target machine.

Invoke-Command -ComputerName <computername> -ScriptBlock {command } -credential <username>

The credential paraeter will put up a name-password gui box to get your user credentials.

You can start an entire remote session with a similar command

Enter-PSSession -ComputerName <computer> – Credential <username>

This will give you a command line prefaced with the computer name.

[Win7VM] PS >

You can run commands that reside only on the remote computer….or run PS commands on your own computer that execute on the remote computer.

To return to your own machine:

[Win7VM] PS> Exit

Because Remoting runs as a service, you don’t have to have PowerShell open on the remote machine to be able to connect to it.

If you set this up first using a Virtualbox virtual machine, you can see how both ends of the connection work without inconveniencing a user.

Create U.S. State Abbreviations from State Names

function shortenState([string]$longName) {
# LK 3/17/15
# Takes upper, lower, or mixed case state name
# and returns the two-letter abbreviation.
# Example call:
#   shortenstate Vermont     <-don't use parentheses
#   "Vermont" | shortenState <- sent via a pipe
# Note that this function has to appear *before* it is called in a Powershell script.
Switch ($LongName.ToUpper()) {

  ($LongName="ALABAMA")        {$shortenState="AL"}
  ($LongName="ALASKA")         {$shortenState="AK"}
  ($LongName="ARIZONA")        {$shortenState="AZ"}
  ($LongName="ARKANSAS")       {$shortenState="AR"}
  ($LongName="CALIFORNIA")     {$shortenState="CA"}
  ($LongName="COLORADO")       {$shortenState="CO"}
  ($LongName="CONNECTICUT")    {$shortenState="CT"}
  ($LongName="DELAWARE")       {$shortenState="DE"}
  ($LongName="FLORIDA")        {$shortenState="FL"}
  ($LongName="GEORGIA")        {$shortenState="GA"}
  ($LongName="HAWAII")         {$shortenState="HI"}
  ($LongName="IDAHO")          {$shortenState="ID"}
  ($LongName="ILLINOIS")       {$shortenState="IL"}
  ($LongName="INDIANA")        {$shortenState="IN"}
  ($LongName="IOWA")           {$shortenState="IA"}
  ($LongName="KANSAS")         {$shortenState="KS"}
  ($LongName="KENTUCKY")       {$shortenState="KY"}
  ($LongName="LOUISIANA")      {$shortenState="LA"}
  ($LongName="MAINE")          {$shortenState="ME"}
  ($LongName="MARYLAND")       {$shortenState="MD"}
  ($LongName="MASSACHUSETTS")  {$shortenState="MA"}
  ($LongName="MICHIGAN")       {$shortenState="MI"}
  ($LongName="MINNESOTA")      {$shortenState="MN"}
  ($LongName="MISSISSIPPI")    {$shortenState="MS"}
  ($LongName="MISSOURI")       {$shortenState="MO"}
  ($LongName="MONTANA")        {$shortenState="MT"}
  ($LongName="NEBRASKA")       {$shortenState="NE"}
  ($LongName="NEVADA")         {$shortenState="NV"}
  ($LongName="NEW HAMPSHIRE")  {$shortenState="NH"}
  ($LongName="NEW JERSEY")     {$shortenState="NJ"}
  ($LongName="NEW MEXICO")     {$shortenState="NM"}
  ($LongName="NEW YORK")       {$shortenState="NY"}
  ($LongName="NORTH CAROLINA") {$shortenState="NC"}
  ($LongName="NORTH DAKOTA")   {$shortenState="ND"}
  ($LongName="OHIO")           {$shortenState="OH"}
  ($LongName="OKLAHOMA")       {$shortenState="OK"}
  ($LongName="OREGON")         {$shortenState="OR"}
  ($LongName="PENNSYLVANIA")   {$shortenState="PA"}
  ($LongName="RHODE ISLAND")   {$shortenState="RI"}
  ($LongName="SOUTH CAROLINA") {$shortenState="SC"}
  ($LongName="SOUTH DAKOTA")   {$shortenState="SD"}
  ($LongName="TENNESSEE")      {$shortenState="TN"}
  ($LongName="TEXAS")          {$shortenState="TX"}
  ($LongName="UTAH")           {$shortenState="UT"}
  ($LongName="VERMONT")        {$shortenState="VT"}
  ($LongName="VIRGINIA")       {$shortenState="VA"}
  ($LongName="WASHINGTON")     {$shortenState="WA"}
  ($LongName="WEST VIRGINIA")  {$shortenState="WV"}
  ($LongName="WISCONSIN")      {$shortenState="WI"}
  ($LongName="WYOMING")        {$shortenState="WY"}
 
Default {$shortenState="XX"}
  } #switch

return $shortenState

} # function shortenState

# The following lines import a .csv file, and modify the state field
# to the two-letter abbreviation 

$Deliveries=Import-Csv "c:userslarrypowershellworldship.csv"

$Deliveries | Foreach-Object ($_) {

  if ($_.State.length -gt 2) {$_.State = shortenState($_.State)}

}

Debugging in PowerShell

The PowerShell integrated scripting environment (ISE) has many of the trappings of a full-fledged programming GUI, including a capability for debugging with breakpoints. There is a TechNet article on debugging from which this discussion is cribbed.  The ISE has a subset of the command-driven breakpoint capability, in that it only allows the setting of line breakpoints. In the command environment, you can also set variable breakpoints, which execute when the value of a variable changes, and command breakpoints, which execute when a certain command is reached.

Breakpoints can only be set for a script that has been saved.

In the ISE window a breakpoint can be set from the debug menu, (Toggle Breakpoint)  or by pressing F9 when the cursor is on the line that you want to use for the breakpoint.  Once set, the line will be highlighted.

After setting a least one breakpoint, you can run the single-stepper. This executes the script one line at time.

Step Into: Executes the current statement and stop at the next statement. If the statement is a function or script, it goes into the function or script and then stops.  F11

Step Over: Execute the current statement and stop at the next statement.  If the statement calls a function or script, it executes the function or script and then returns to the next statement in the original script. F10

Step Out: Executes the current function and then returns to the level above in the call stack. If there are statements remaining in the sub-function, those are executed before the return. Essentially this is something like “finish running this function, and return…”

Continue: Execute to the next breakpoint without single-stepping.

What I’ve been doing as I’ve been learning PowerShell is single stepping through a script, which allows me to look at the effect of a single statement before moving on to the next statement.  This involves setting a breakpoint a the top of the script, and then hitting F11 to toggle through the script.

Calling sub-scripts.

Scripts can be called from other scripts using the Invoke-Expression commandlet.  Example:

Invoke-Expression -Command ./PSFTPDEMO.ps1

If the subscript is located in the current working directory, or within the same directory as the main script, it needs to be prefaced with the ./ path as shown above.

The subscript inherits all variables from the main script unless those variables are declared private in the main script.