Stuffing the output of the last command into an automatic variable

June 26, 2008 at 5:06 AMAndy Schneider

UPDATE:

/\/\o\/\/ and Joel Bennet both chimed in and provided a much more elegant solution, just override out-default. This is really what I had wanted to do, but didn't know how. Thanks to both /\/\o\/\/ and Joel for your input. Please do check out their comments on this post. But here is the code they provided:

   1: # From /\/\o\/\/
   2:  
   3: function out-default {
   4:     $input | Tee-Object -var global:lastobject | 
   5:     Microsoft.PowerShell.Utility\out-default
   6: }
   7:  
   8: # And from Joel 
   9: # In case you are using custom formatting
  10: # You will need to override the format-* cmdlets and then
  11: # add this to your prompt function
  12:  
  13: if($LastFormat){$LastOut=$LastFormat; $LastFormat=$Null }
  14:  
  15:  
  16:  

A couple of days ago, an intern that is working for us, was helping me with a Powershell script to manage our Hyper V Cluster. The script ran fine but we were querying 7 different computers and then rolling up all the output into a custom object, so the thing took a while to run. It was just long enough to be annoying. During this process, he asked if there was a way to have PowerShell automatically store the output of the last command in a variable automatically.

I first went down the road of using Tee-object,  According to the built-in help,

The Tee-Object cmdlet send the output of a command in two directions (like the letter T). It stores the output in a file or variable, and also sends it down the pipeline. If Tee-Object is the last command in the pipeline, the command output is displayed in the console.

Here's Tee-Object in action

PS C:\> get-process notepad | tee-object -variable note

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
     50       2     1264       6596    60     0.06   6984 notepad


PS C:\> $note

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
     50       2     1264       6596    60     0.06   6984 notepad


PS C:\>

But of course, my Intern (rightfully so) declares this to be unsatisfactory. He wants it to just happen automagically. Picky intern, huh?

Then on the bus ride home this afternoon, I was thinking about Jeffrey's post on Push-Noun. He basically shows us how to set up a loop that goes forever, taking input and executing it only for a specific noun in Powershell.

Considering this, I realized I could do something similar for my issue. So here's the code, stolen from Push-Noun.

function Set-LastObjectAvailable {
while ($TRUE)
{
    Write-Host "[LASTOBJECT]> " -NoNewLine
    $line = $Host.UI.ReadLine().trim()
    switch ($line)
    {
    "exit"   {return}
    "quit"   {return}
    "?"      {"Just type a command and the output will be displayed and stored in `$lastobject" }
    {$_.StartsWith("!")}
             {
                $Cmd = $_.SubString(1)
                Invoke-Expression $line |Tee-Object -varialbe lastobject | Out-Host
             }
    default  {

                Invoke-Expression $line |Tee-Object -Variable lastobject | out-host
             }
    }
}
}

And here it is in action:

PS C:\> Set-LastObjectAvailable
[LASTOBJECT]> get-process notepad

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
     50       2     1264       6600    60            6984 notepad
     52       2     1256       4412    58     0.06  10152 notepad


[LASTOBJECT]> $lastobject

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
     50       2     1264       6600    60            6984 notepad
     52       2     1256       4412    58     0.06  10152 notepad


[LASTOBJECT]> ?
Just type a command and the output will be displayed and stored in $lastobject
[LASTOBJECT]> gps notepad

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
     50       2     1264       6600    60            6984 notepad
     52       2     1256       4412    58     0.06  10152 notepad


[LASTOBJECT]> $lastobject.count
2
[LASTOBJECT]> exit
PS C:\>

This isn't exactly bulletproof. I think I would like to have an automatic variable that stores the output of the last command that was executed, just in case you want to mess with it and you don't want to have to run it again. Basically just override out-host to use a Tee-Object -variable lastcommandoutput or something along those lines.

Posted in:

Tags:

Sysadmin Meme

June 16, 2008 at 3:06 PMAndy Schneider

Shay Levi called me out with a blog post that was started by Mind of Root.  Just to summarize:

A meme  consists of any unit of cultural information, such as a practice or idea, that gets transmitted verbally or by repeated action from one mind to another.

So here it goes:

How old were you when you started using computers?

11

What was  your first machine?

The first machine I got to use was an Apple IIE. Later on, my family purchased a Mac IIsi.

What was the first real script you wrote?

This really isn't scripting, but my first introduction to any type of programming was on a Mac in Junior High School where we learned to use Hyper Card. It was basically a computerized flip book, and I had  a guy walk across the screen and open a door.

The first real script was a batch file that ran a bunch of installers silently for a workstation build for our EE building's computer lab in college.

What languages have you used?

  • Powershell
  • VBScript
  • Ruby
  • C# (I'm calling this scripting since you can use inline C# with the new Add-Type Cmdlet in the Powershell V2 CTP2 :) )

What was your first professional Sysadmin gig?

I worked for a small consulting firm called Starr Technologies and we did IT for several Venture Capital firms in Menlo Park, CA.

If you knew then what you know now, would you have started in IT?

Absolutely! I studied Electrical Engineering in college, but transistors, resistors, and capacitors just never really did it for me. My senior year I took a class on Internet communications and haven't turned back sense. Looking back though, I wish I had taken more CS classes though.]

If there is one thing you learned along the way that you would tell new Sysadmins, what would it be?

Two things.

Get involved in the community. This is something I am learning about now. The Powershell community is really the first "online" community I have actively participated in and I am loving it. It is so much easier to learn with people who are passionate about the same things as you are.

Second is find a great mentor whom you can trust. This probably is not your manager, and may not be someone you work with directly. Buy him/her lunch. People rarely turn down free food, even if they are crazy busy. If people like what they do, they would love the opportunity to teach people. I have a mentor for my professional career as well as for my personal life. I also love to teach younger people as well. It's a two way street: always teach, and always be willing to be taught.

What is the most fun you have had scripting?

The best part of scripting is when someone asks you if you can do x, y, or z and you come back to them and say, "Hey, I wrote this little script for you." I love watching their jaw drop after I show them the one or two lines of Powershell I used to make it happen.

Who Am I calling out ?

/\/\o\/\/ - the PowerShell Guy

Marco Shaw

Kirk Munro

Jeffrey Snover

Posted in:

Tags:

Building Script Cmdlets as objects

June 15, 2008 at 6:06 AMAndy Schneider

I have been using CTP2 on my computers for sometime and I recently came across a situation where I needed a function that would easily accept parameters from the pipeline and also as a standard parameter. This was the perfect excuse to start playing with Script Cmdlets. These things have all kinds of cool attributes that you can use to make your scripts easier to use, which is all wonderfulness.

So I cracked open Powershell and started playing. It took me a little while but I figured out how to convert my function over to a ScriptCmdlet.

But there was a problem. I had to read lots of documentation to figure it out. What I love about Powershell is how it is so discoverable with use of get-member.

Buried in Powershell is something called a CommandInfo object. This object describes a command and what it can do. Wouldn't it be cool if these objects had information about the parameters, whether or not they accepted values from the pipeline, and what position they were in. The list of options goes on and on.

I think the best way to explain is to show some code that could exist.

   1: # Build Up A parameter object
   2: $param.Name = "File"
   3: $param.HelpMessage = "Please Enter a file name"
   4: $param.acceptsValueFromPipeline = $TRUE
   5:  
   6: # Build a block that will go into the processBlock of the ScriptCmdlet
   7: $scriptblock = "Get-Content `$File"
   8:  
   9: #Build The CmdLet
  10: $cmdlet.verb = "Get"
  11: $cmdlet.noun = "DemoTextFile"
  12: $cmdlet.p1 = $param # p1 is short for parameter1 in the scriptCmdlet
  13: $cmdlet.beginBlock = "write-host `"Beginning`""
  14: $cmdlet.processBlock = $scriptblock
  15: $cmdlet.endBlock = "write-host `"Ending`""
  16: $cmdlet.Write()

At the very end, the $cmdlet.write() method would write the code for you.

Well, now here comes the fun part (With warnings)

This code was written hacked together and is absolutely not guaranteed to work and I cannot be held liable if it eats your cat or kills your computer. The purpose here is to really see if there would be any interest in furthering this. Eventually i would like to see these properties get added to the System.Management.Automation.FunctionInfo class, or create a new class that inherits from FunctionInfo.

First, we can use Add-Type to create new types with inline C#

   1: Add-Type @"
   2: namespace Getpowershell {
   3:     public class Scriptcmdlet {
   4:         public string noun;
   5:         public string verb;
   6:         public string processBlock;
   7:         public string beginBlock;
   8:         public string endBlock;
   9:         public Getpowershell.Parameter p1;
  10:         public Getpowershell.Parameter p2;
  11:  
  12:         public string Write() {
  13:  
  14:             System.Text.StringBuilder sb = new System.Text.StringBuilder();
  15:             sb.Append("Cmdlet " + this.verb + "-" + this.noun + " { \n");
  16:             sb.Append("param (\n");
  17:             sb.Append("[Parameter(\n");
  18:             if (this.p1.acceptsValueFromPipeline == true) { sb.Append("ValueFromPipeline=`$true," +"\n"); }
  19:             if (this.p1.Mandatory == true) { sb.Append("Mandatory,\n"); }
  20:             sb.Append("HelpMessage=\"" + this.p1.HelpMessage + "\"]\n");
  21:             sb.Append("$" + this.p1.Name);
  22:             sb.Append(")\n");
  23:             sb.Append("Begin { \n" + this.beginBlock + "\n}\n");
  24:             sb.Append("Process { \n" + this.processBlock + "\n}\n");
  25:             sb.Append("End { \n" + this.endBlock + "\n}\n}\n");
  26:             return sb.ToString();
  27:         }
  28:     }
  29:     public class Parameter {
  30:             public string Name;
  31:             public bool acceptsValueFromPipeline;
  32:             public string HelpMessage;
  33:             public bool Mandatory;
  34:         }
  35:     
  36: }
  37: "@

Now I can do this code over again but I need to set up some objects with my new types first.

   1: $cmdlet = New-Object Getpowershell.Scriptcmdlet
   2: $param = New-Object Getpowershell.Parameter
   3:  
   4: # Build Up A parameter object
   5: $param.Name = "File"
   6: $param.HelpMessage = "Please Enter a file name"
   7: $param.acceptsValueFromPipeline = $TRUE
   8:  
   9: # Build a block that will go into the processBlock of the ScriptCmdlet
  10: $scriptblock = "Get-Content `$File"
  11:  
  12: #Build The CmdLet
  13: $cmdlet.verb = "Get"
  14: $cmdlet.noun = "DemoTextFile"
  15: $cmdlet.p1 = $param # p1 is short for parameter1 in the scriptCmdlet
  16: $cmdlet.beginBlock = "write-host `"Beginning`""
  17: $cmdlet.processBlock = $scriptblock
  18: $cmdlet.endBlock = "write-host `"Ending`""
  19: $cmdlet.Write()

In theory, you can run $cmdlet.write() and it will echo out the text for a new Script Cmdlet..

Here is some output from get-member on $cmdlet and $param

   1: PS C:\Users\andys\Desktop> $cmdlet | fl *
   2:  
   3:  
   4: noun         : DemoTextFile
   5: verb         : Get
   6: processBlock : Get-Content $File
   7: beginBlock   : write-host "Beginning"
   8: endBlock     : write-host "Ending"
   9: p1           : Getpowershell.Parameter
  10: p2           :
  11:  
  12:  
  13:  
  14: PS C:\Users\andys\Desktop> $param | fl *
  15:  
  16:  
  17: Name                     : File
  18: acceptsValueFromPipeline : True
  19: HelpMessage              : Please Enter a file name
  20: Mandatory                : False
  21:  
  22:  
  23:  
  24: PS C:\Users\andys\Desktop> $cmdlet.Write()
  25: Cmdlet Get-DemoTextFile {
  26: param (
  27: [Parameter(
  28: ValueFromPipeline=$true,
  29: HelpMe
ssage="Please Enter a file name"]
  30: $File)
  31: Begin {
  32: write-host "Beginning"
  33: }
  34: Process {
  35: Get-Content $File
  36: }
  37: End {
  38: write-host "Ending"
  39: }
  40: }
  41:  
  42:  
  43: PS C:\Users\andys\Desktop>

Known issues:

I have no idea if the Mandatory option will produce proper code for the Cmdlet

Right now it will only support adding one parameter which is the p1 property of the $cmdlet object

Posted in:

Tags:

Managing Hyper-V with WMI

June 12, 2008 at 3:06 PMAndy Schneider

Dung K Hoang has an excellent series of blog posts on how to manage Hyper-V with WMI.

I have taken a couple of his scripts and built a function that takes input from the pipeline and gives me an object that has three properties, a HostServer, VirtualMachine, and a Switch.

   1: function Get-HyperVInfo {
   2:     begin {
   3:         $VmSwitchinfo = @();
   4:     }
   5:     process {
   6:         $computer = $_
   7:         $ListofVMs = gwmi -namespace root\virtualization Msvm_ComputerSystem -filter "ElementName <> Name" -computer $computer
   8:         $ListofSwitches = gwmi -namespace root\virtualization Msvm_VirtualSwitch -computer $computer
   9:         $ListofSwitchPorts = gwmi -namespace root\virtualization Msvm_SwitchPort  -computer $computer
  10:         foreach ($Switch in $ListofSwitches)
  11: {
  12:             $SwitchGUID = $Switch.Name
  13:             $SwitchDisplayName = $Switch.ElementName
  14:             $PortsOnSwitch = $ListofSwitchPorts | where {$_.SystemName -match $SwitchGUID} 
  15:  
  16:             foreach ($Port in $PortsOnSwitch)
  17: {
  18:                 $PortPath = $Port.__PATH
  19:                 $ListofConnections = gwmi -namespace root\virtualization Msvm_ActiveConnection -computer $computer
  20:                 $a = $ListofConnections | where {$_.Antecedent -like $PortPath}
  21:                 if ($a -ne $NULL)
  22: {
  23:                     $LANEndPoint = $a.Dependent 
  24:                     foreach ($VM in $ListofVMs)
  25: {
  26:                         $VMGUID = $VM.Name
  27:                         $VMDisplayName = $VM.ElementName
  28:                         if ($LanEndPoint -like "*$VMGUID*")
  29: {
  30:  
  31:                             $vminfo = "" |Select-Object VirtualMachine ,HostServer, switch
  32:                             $vminfo.Switch = $SwitchDisplayName
  33:                             $vminfo.VirtualMachine = $VMDisplayName
  34:                             $vminfo.HostServer = $_
  35:                             $vmswitchinfo += $vminfo
  36:  
  37:                         }
  38:                     }
  39:                 }
  40:             }
  41:         } 
  42:  
  43:  
  44:     }
  45:     end {
  46:     $vmswitchinfo
  47:     }
  48: }

To use this function you can just do something like this

"HVSERVER1","HVSERVER2","HVSERVER3" | Get-HypervInfo

I have used this script to get a bunch of information on all the VM's in my Hyper V Cluster. Note that this is just using WMI so I can query my Server Core machines that are running Hyper V.

Many Many thanks to Dung for this info. I would not have even been able to get started without his help.

Posted in:

Tags:

CTP2, Running Console Apps against multiple computers

June 8, 2008 at 5:06 AMAndy Schneider

 

With CTP2, one of the biggest features is remoting. This works really well when you are running native cmdlets and scriptblocks in runspaces with multiple computers.

For example

function q {$args}
$rs = New-Runspace (q powershell-dev1 powershell-dev2 powershell-dev3)
Invoke-Command -ScriptBlock {hostname} -Runspace $rs

# This will create the following output
PS C:\Users\andys> Invoke-Command -ScriptBlock {hostname} -Runspace $rs
powershell-dev2
powershell-dev3
powershell-dev1

Pretty cool, and you can use the computername property to find out which computer returned which object.

But with commands like ipconfig or netsh that are not native to Powershell, just tacking on the computername property is a little difficult. When you pipe everthing to select-object, format-table, or format-list, which properties do you select.

Under normal circumstances, with say something like Get-Process, you could pipe it to Format-Table Id, Name, WorkingSet.

But with native commands, there are no properties. So here's what you can do.

You can pipe it to select-object and select the whole object using $_ and then also select  $_.ComputerName. The only trick is you have to pass the properties in as scriptblocks,

PS C:\Users\andys> Invoke-Command -ScriptBlock {ipconfig} -Runspace $rs |

select {$_} ,{$_.ComputerName} $_ $_.ComputerName -- --------------- powershell-dev3 Windows IP Configuration powershell-dev3 powershell-dev3 powershell-dev3 Ethernet adapter Local Area Connection: powershell-dev3 powershell-dev3 Connection-specific DNS Suffix . : example.com.... powershell-dev3 Link-local IPv6 Address . . . . . : fe80::4c04:b... powershell-dev3 IPv4 Address. . . . . . . . . . . : 10.2.31.214 powershell-dev3 Subnet Mask . . . . . . . . . . . : 255.255.254.0 powershell-dev3 Default Gateway . . . . . . . . . : 10.2.30.1 powershell-dev3 powershell-dev3 Tunnel adapter Local Area Connection* 8: powershell-dev3 powershell-dev3 Media State . . . . . . . . . . . : Media discon... powershell-dev3 Connection-specific DNS Suffix . : corp.avanade... powershell-dev3 powershell-dev2 Windows IP Configuration powershell-dev2 powershell-dev2 powershell-dev2 Ethernet adapter Local Area Connection: powershell-dev2

The output is not incredibly wonderful but you can at least easily know which remote computer returned which line of text from the command you executed.

Posted in:

Tags:

Ping an Array of Computers

June 7, 2008 at 5:06 PMAndy Schneider

I have found on occasion the need to ping a list of computers, whether they be in a text file, an array in Powershell, or a CSV file. It turns out building commands as a string and then executing that string as a command in Powershell is not exactly intuitive.

You need to use invoke-expression -command $cmd where $cmd is the string you want to execute.

So, we can do the following, using my New-Array Function

   1: function new-array {$args}
   2: $servers = new-array powershell-dev1 powershell-dev2 powershell-dev3
   3: $servers | foreach-object {invoke-expression -command "ping -n 1 $_"} 

Using invoke-expression -command you can have an executable and arguments and everything just works. There are situations where you will need to use the back tick character " `" to escape characters.

You can use this technique to execute just about any string that you build using Powershell and variables.

Posted in:

Tags: