Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

PowerTab for PowerShell V2 Update

I posted, 8 updates since PowerTab for PowerShell V2 Alpha testers wanted (link to download in former post) mostly working with Xaegr 

I did some hard work on the Member completion (3 rewrites) , but it is almost there,

For the other Alpha Testers, I added another eager missed feature FileSystem completion that I would like you to take a look at

relative filesystem, and UNC path completion (shares broken in latest build, but will be fixed ;-) )

image

Registry :

image

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 3 Comments
Filed under: , ,

The Show goes on

See: Translating Literature Into PowerShell ,

Hmm, would you only be able to translate books to PowerShell this way ? :

while {$Question.IsAnswered -eq $false) {if ($TheOne){switch $pill {'Red'{exit};"Blue {$memory.reset()}}}

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 1 Comments
Filed under: ,

PowerTab for PowerShell V2 Alpha testers wanted

As I did mention in last post ( PowerTab for PowerShell V2  ), I started working on PowerTab for PowerShell V2, and I posted the current version some day's ago and made 5 updates to it in that time.

I'm looking for some Alpha testers that have a bit experience with PowerTab and want to give detailed input.

This is only an alpha version and not as functional as the current PowerTab so no need to install this Alpha build looking for more functionality yet, so first some warnings.

  • CTP2 needed 
  • No Custom completion
  • No configuration
  • No powertab file completion
  • No install
  • Not everything works as expected yet
  • irregular updates

OK, if you still feel up to it after those warnings, You can find the latest version here :

http://thepowershellguy.com/blogs/posh/pages/powertab-v2-alpha-1.aspx

Status :

I did implement a lot of bugs and tips from Xaegr (See also his  Screencast about Powertab )

Current Focus :

Installation

Simple but run update-Tabexpansion and Export-Tabexpasion by hand after first install (to overwrite my database)

creating of new database still needs to be done.

for the rest as it is a Module now  the installation is very simple, follow Jaykul's excellent intro here : PowerShell Modules

Parameter completion

I'm implementing Parameter completion on Script Cmdlets, for example help is now a script cmdlet :

 image

Completion on Arguments that are enums :

image

Completion on scripts :

image

Member completion

I'm implementing Member completion on $_. ( Note, that members from File and directory objects are mixed in the completion)

image

Other Things to try :

image

Also try the examples in the NewTest.txt you will find in the PowerTab directory.

You can leave a comment with your bugs, tips, requests etc. or you can mail me at the mail address on the About page, (with pictures etc)

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 2 Comments
Filed under: , ,

PowerTab for PowerShell V2

I got a lot of questions about the future of PowerTab lately, so a teaser of what is coming in a new version of PowerTab for the CTP2 version of PowerShell:

- No Installer

PowerTab is now a Module, so installation will be as easy as copying the directory to the Packet location and running Add-Module PowerTab

- Using the Tokenizer

- $_ support

- ArgumentType support

and more to come ...

-

image

A first Alpha version can be expected later this week.  found here : http://thepowershellguy.com/blogs/posh/pages/powertab-v2-alpha-1.aspx

Note that this is an Alpha version, with still less functionality as current PowerTab, targeted at readers that want to help testing.

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 6 Comments
Filed under: , ,

PowerShell Get-Ipconfig function

 

In a comment on this post PowerShell on joeware.net , I translated a Perl example that used ipconfig information, I used the same regex techniques as in the Perl example, but this made me think of a more PoSHy  general wrapper that exposes the ipconfig information as an object in PowerShell.

for convenience I also added text output to the console by default (you can suppress this with the -Silent Switch) so you can use it as a quick lookup as Ipconfig, but it also returns a dynamically build Object that you can use to access all information hierarchal by digging into the properties.

You can use the properties (and tab completion) to access the property of interest from the output for interactive or script usage.

See examples below :

image

The Get-IpConfig function shown above looks like this :

Function get-Ipconfig ([switch]$silent){
  $ipc = ipconfig /all
  $ipcObject = New-Object object
  $ipc |% {

    if (.{$m = [regex]::Matches($_,'^(\w.*?):*$');$m[0]}) {
        $script:o = New-Object object
        Add-Member -InputObject $o -MemberType ScriptMethod -Name ToString -Value {$this.psobject.properties |% {$_.name + " = " + $_.value + "`n"}} -force
        Add-Member -InputObject $ipcObject -MemberType NoteProperty -Name $m[0].groups[1].value.replace(' ','') -Value $o
    } elseIf (.{$m = [regex]::Matches($_,'(.*?)[ \.]*: (.*)');$m[0]}){
        $script:p = $m[0].groups[1].value.replace(' ','')
        Add-Member -InputObject $script:o -MemberType NoteProperty -Name $m[0].groups[1].value.replace(' ','') -Value $m[0].groups[2].value
    } elseIf (.{$m = [regex]::Matches($_,'\w+.*');$m[0]}){
        $script:o."$p" += ";" + $m[0].groups[0] 
    }
  }
  if (!$silent) {$ipcobject | fl | out-string | write-host -fore 'Yellow'}
  $ipcobject
}

Note that this is only a simple example and as I build the property list dynamic I do keep all properties text, and not translate them but the use of nested Noteproperties might be interesting to look at, as it might come of use sometime, note also the overload of the ToString() Method for custom formatting with Format-List ;-)

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 10 Comments

Hey PowerShell Guy !, How Can I Copy Files Based on Multiple Criteria?

A Translation of the Scripting Guys post : How Can I Copy Files Based on Multiple Criteria? VbScript example,

As mostly in this series I point to the original Article for more information.

dir c:\Scripts |? {$_ -match "^FS|^CG|^GPS." -and $_.CreationTime -gt (get-date).AddDays(-1)} | copy -dest C:\Test

And on the Scripting Guy's remark in the article :

2) with Windows PowerShell 1.0 we wouldn’t be able to run this script against a remote computer. 

not really as we can just as easy use UNC paths in the command ;-p 

dir \\server\c$\scripts .....

or of course by using WMI  as in the Vbscript example

* Update * OK, I could not resist here is the WMI oneliner, doing exactly the same as the Scripting Guy's VbSCript version :

([WmiSearcher]"ASSOCIATORS OF {\\.\root\cimv2:Win32_Directory.Name='C:\Scripts'} Where ResultClass = CIM_DataFile").Get() |? {$_.ConvertToDateTime($_.CreationDate) -gt (get-date).AddDays(-1) -and $_.FileName -match "^FS|^CG|^GPS." } |% {$_.copy(("c:\test\{0}.{1}" -f $_.filename,$_.Extension))}

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 1 Comments
Filed under: ,

Little PoSH prank

I came up with this little prank while on IRC,

give this command on a colleague's workstation (he did not lock) in a PowerShell console and minimize it  , and you might have some fun watching him when he gets back ;-)

do {[Windows.Forms.Cursor]::Position = "1,1";sleep 5}until (0)

enjoy,

Greetings /\/\o\/\/

Posted by MoW | 3 Comments
Filed under: ,

PowerShell projects on Codeplex, 37 and Growing

I often recommend peaple to keep an eye on , these articles might tell you why :

PowerTools Script that Generates a Table in an Open XML Document

Hyper-V PowerShell library - now on Codeplex

Windows Mobile PowerShell Provider released (MS Mobiles)

This brings the count of current  PowerShell Projects on CodePlex to 37 !

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 0 Comments
Filed under: ,

Scripting "Sysadmin" Meme

Being Called out by Andy Schneider :

I didn't have an real administrator role for over 14 years now, but often messing up there environments in that time, hence there still is a close relation so here we go : 

How old were you when you started using computers?

10

What was your first machine?

After a Pong and an Atari 2600 Gameconsole, an ZX81 with 1K memory digged up somewhere got me started, then as santa saw this really was the thing for me, he brought me a brand new Atari 600 XL.

What was the first real script you wrote?

Hangman on the ZX81 I think.

What languages have you used?

    • Powershell
    • VBScript
    • kix, rexx  , VB.NET, C# 
    • Batch / Resourcekit tools and all else I could find that got the job done.
    • Basic
    • Turbo Pascal,  DB3+ ,  Rbase, Informix

What was your first professional Sysadmin gig?

After doing some Unix as interim, I started out as a sysadmin in an Apple environment at a designer studio.

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

Yes,  I can't do anything else ;-)

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

Explore and get to know your environment interactively, preferable from the console whenever you can spare some time, try everything (changing settings and any other things you're not comfortable with, first  in a test environment of course !), if you find something useful, and think it might come handy later use ADM (Admin Development Model)  to Script It, Share it, and get involved in the community as there also is a lot done there already . and you will get inspiration to try and discover new things.

Playing with this kind of stuff (commandline and scripting ) and knowing your environment well (not only the parts you realy need too) really will pay off in the long time. 

What is the most fun you have had scripting?

Since hacking on my Atari, using PowerShell by far.

Who Am I calling out ?

* Yes I know they are more Dev oriented,  Klumsy I think is the only one doing some real admin work at the moment , but hey that kinda goes for me ! I'm sure they all script ;-)

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 1 Comments

MSDN Virtual Lab Express: What’s New in Windows PowerShell V2

A PowerShell V2 virtual lab is available on MSDN, from the Overview .

    After completing this lab you will be better able to:

  • Create and use runspaces, both synchronously and asynchronously
  • Alter the behavior of existing cmdlets using generated proxies
  • Constrain a runspace
  • Use the debugging features to debug a script
  • Use the data language to internationalize a script
  • Use the PowerShell v2 enhancements to get-member, select-string and the ADSI Adapter
  • Use the new split and join string operations
  • Execute basic commands in Graphical PowerShell

You can do this virtual lab here :  MSDN Virtual Lab Express: What’s New in Windows PowerShell V2

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 1 Comments
Filed under: ,

PowerShell Performance Series Part 4 (Version 1.0 wins this round ?)

I finally installed two simular VM's, one with PowerShell Version 1.0 and the other one with PowerShell CTP2, to do performance testing between the 2 versions.

Both VM's run a bare Windows XP SP 2 installation with .NET framework 2.0 on both (to exclude the framework from testing)

While I was translating the code in the last post PowerShell Performance Series Part 3 (.NET method call not always faster as PowerShell shortcut) to PowerShell Version 1.0, (as PowerShell 1.0 has no add-type Cmdlet I did the same thing using the .NET framework directly) so that I could use the same code on as well PowerShell version 1.0 as the CTP2 build.

When I started testing I got some "Strange" results PowerShell 1.0 was up till 10 times faster !

image

For reference I also did the Tests from the First post PowerShell Performance Series Part 1 (Warming Up) on both the VM's (still a win for CTP2 as expected):

Seems something happened to the $null , out-null and variable assignment, that did cost performance, I need to look into it further but hope this is a CTP thingie and that it will be faster in the final V2 version.

The Test code :

$provider = new-object Microsoft.CSharp.CSharpCodeProvider
$params = new-object System.CodeDom.Compiler.CompilerParameters 

$refs = "System.dll","C:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0\System.Management.Automation.dll"
$params.ReferencedAssemblies.AddRange($refs) 
$params.GenerateExecutable = $false 
$params.GenerateInMemory = $True

$class = @"
public static class ObjGen
{
  public static System.Management.Automation.PSObject GetPSObject( System.Collections.Hashtable noteproperties )
  {
    System.Management.Automation.PSObject obj = new System.Management.Automation.PSObject();
    if (noteproperties != null) 
    {
      foreach(System.Collections.DictionaryEntry item in noteproperties)
      {
        obj.Properties.Add(new System.Management.Automation.PSNoteProperty((string)item.Key,item.Value)); 
      }
    }
    return obj;
  }
}
"@


$results = $provider.CompileAssemblyFromSource($params, $Class) 
$results 

1..50000 |% {
  $o = New-Object PSObject
  Add-Member -in $o -Name 'Name' -MemberType 'NoteProperty' -Value 'Karl'
  Add-Member -in $o -Name 'age' -MemberType 'NoteProperty' -Value 31
  Add-Member -in $o -Name 'now' -MemberType 'NoteProperty' -Value [datetime]::Now
}

for($i = 0; $i -lt 50000;$i++ ) {
  $o = New-Object PSObject
  Add-Member -in $o -Name 'Name' -MemberType 'NoteProperty' -Value 'Karl'
  Add-Member -in $o -Name 'age' -MemberType 'NoteProperty' -Value 31
  Add-Member -in $o -Name 'now' -MemberType 'NoteProperty' -Value [datetime]::Now
}

[void](1..50000 |% {[ObjGen]::GetPSObject(@{name="karl";age=31;now = [datetime]::Now})})
#1..50000 |% {[void][ObjGen]::GetPSObject(@{name="karl";age=31;now = [datetime]::Now})}
1..50000 |% {$null = [ObjGen]::GetPSObject(@{name="karl";age=31;now = [datetime]::Now})}
1..50000 |% {[ObjGen]::GetPSObject(@{name="karl";age=31;now = [datetime]::Now}) | out-null}
1..50000 |% {[ObjGen]::GetPSObject(@{name="karl";age=31;now = [datetime]::Now}) } | out-null

1..50000 |% {
  $p = @{}
  $p.Add("name","karl")
  $p.add("age",31)
  $p.add("now",[datetime]::Now)
  [ObjGen]::GetPSObject($p) | out-null
}

1..50000 |% {
  $p = @{}
  $p.name = "karl"
  $p.age = 31
  $p.now = [datetime]::Now
  [ObjGen]::GetPSObject($p) | out-null
}

1..50000 |% {
  $p = @{}
  $p.name = "karl"
  $p.age = 31
  $p.now = [datetime]::Now
  [ObjGen]::GetPSObject($p) 
} | out-null


foreach ($i in 1..50000) {
  $p = @{}
  $p.name = "karl"
  $p.age = 31
  $p.now = [datetime]::Now
  [ObjGen]::GetPSObject($p) | out-null
} 

for($i = 0; $i -lt 50000;$i++ ) {
  $p = @{}
  $p.name = "karl"
  $p.age = 31
  $p.now = [datetime]::Now
  [ObjGen]::GetPSObject($p) | out-null
}

foreach ($i in 1..50000) {
  $p = @{}
  $p.name = "karl"
  $p.age = 31
  $p.now = [datetime]::Now
  $null = [ObjGen]::GetPSObject($p)
}

foreach ($i in 1..50000) {
  $p = @{}
  $p.name = "karl"
  $p.age = 31
  $p.now = [datetime]::Now
  [ObjGen]::GetPSObject($p) > $null
}

.{foreach ($i in 1..50000) {
  $p = @{}
  $p.name = "karl"
  $p.age = 31
  $p.now = [datetime]::Now
  [ObjGen]::GetPSObject($p) 
} } | out-null

1..100000 |% {$null = $_}
for($i = 0; $i -lt 100000;$i++ ) { $Null = [String]::Copy("Now is men to come the the aid of their country" )}
for($i = 0; $i -lt 100000;$i++ ) { $a = [String]::Copy("Now is men to come the the aid of their country" )}

h | select com*,{[datetime]$_.EndExecutionTime - [datetime]$_.StartExecutionTime}

*Disclaimer* your results may vary, I don't completely believe mine yet, hence I'm scarce on conclusions in this post, and am open to any ideas why the CTP2 build does lose from Version 1.0 here ;-)

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 1 Comments
Filed under: , ,

PowerShell Performance Series Part 3 (.NET method call not always faster as PowerShell shortcut)

Continuing  on from Karl's post's :

  • Fast New PSCustomObject.
  • generating a "PropertyBag" aka PScustomObject in C#
  • Getting serious about performance in PowerShell.

    and mine

  • PowerShell Performance Series Part 2 (Get-PerformanceHistory.ps1)
  • PowerShell Performance Series Part 1 (Warming Up)

    I did go on from Karl's last post Fast New PSCustomObject. where he used a HashTable to add the Properties and questioned the performance, so I converted his sample to a CTP2 Add-Type example and tested generating the Hashtable with an added method to ObjGen to create a HashTable, so to get a nice way in code to create the HashTable that is still performant.

    I got some nice results again :

    1. Creating HashTables with @{} does not preserve order (know issue) see output below
    2. creating a HashTable from objGen and adding properties to it slows down considerable
    3. Using the PowerShell Shortcut, just assigning the values to non existing properties (very convenient ) , is actually faster as using the Add method  and brings back speed to comparable speed with Karl's method :

    Especialy 3 item did surprise and pleasure me

    image

    As you can see from the output above just assigning the not existing properties in the HashTable is faster as using the Add method, so this method looks better in Code AND is faster so a win-win situation

    Lesson learned, convenient shortcuts are not always slower  

    My testing code :

    
    $class = @"
    public static class ObjGen
    {
    
      public static System.Collections.Hashtable GetHashTable (){return new System.Collections.Hashtable(); }
    
      public static System.Management.Automation.PSObject GetPSObject( System.Collections.Hashtable noteproperties )
      {
        System.Management.Automation.PSObject obj = new System.Management.Automation.PSObject();
        if (noteproperties != null) 
        {
          foreach(System.Collections.DictionaryEntry item in noteproperties)
          {
            obj.Properties.Add(new System.Management.Automation.PSNoteProperty((string)item.Key,item.Value)); 
          }
        }
        return obj;
      }
    }
    "@
    
    add-type $class
    
    . c:\powershell\Get-PerformanceHistory.ps1
    
    1..50000 |% {[ObjGen]::GetPSObject($null)}
    1..50000 |% {new-object PSObject}
    
    .{
      [ObjGen]::GetPSObject(@{name="karl";age=31;now = [datetime]::Now}) | out-host
    
      $p = [ObjGen]::GetHashTable()
      $p.Add("name","karl")
      $p.add("age",31)
      $p.add("now",[datetime]::Now)
      [ObjGen]::GetPSObject($p) | out-host
    
      $p = [ObjGen]::GetHashTable()
      $p.name = "karl"
      $p.age = 31
      $p.now = [datetime]::Now
      [ObjGen]::GetPSObject($p) | out-host
    }
    
    1..50000 |% {[void][ObjGen]::GetPSObject(@{name="karl";age=31;now = [datetime]::Now})}
    
    1..50000 |% {
      $p = [ObjGen]::GetHashTable()
      $p.Add("name","karl")
      $p.add("age",31)
      $p.add("now",[datetime]::Now)
      [void][ObjGen]::GetPSObject($p)
    }
    
    1..50000 |% {
      $p = [ObjGen]::GetHashTable()
      $p.name = "karl"
      $p.age = 31
      $p.now = [datetime]::Now
      [void][ObjGen]::GetPSObject($p)
    }
    
    Get-PerformanceHistory 6
    
    # h | select com*,{[datetime]$_.EndExecutionTime - [datetime]$_.StartExecutionTime}

    Also testing showed the HashTable method of Objgen is realy not needed as the @{} construct for hashtables PowerShell  is also faster as this method.

  • 4.45300 0.00009 1..50000 |% {$p = [ObjGen]::GetHashTable()}
    4.11800 0.00008 1..50000 |% {$p = @{}}
     

    but using New-Object for this is a bad plan :

    16.32000 0.00033 1..50000 |% {$p = new-object hashtable} 

    * Update * PowerShell V1 users can test the "hashTable adding difference" by running these 2 lines and comparing them

    1..50000 |% {$p = @{};$p.Add("name","karl"); $p.add("age",31); $p.add("now",[datetime]::Now)}

    1..50000 |% {$p = @{};$p.name = "karl";$p.age = 31;$p.now = [datetime]::Now} 

    Enjoy,

  • Greetings /\/\o\/\/  

  • Posted by MoW | 2 Comments
    Filed under: ,

    PowerShell Performance Series Part 2 (Get-PerformanceHistory.ps1)

    Jaykul, made a Get-PerformanceHistory function we also used during the Scripting games  but I could find only the CTP2 version in the repository , and to compare V1 to CTP2 we need to be able to run it on both versions, hence I used a quick one-liner to query the history, but Jaykul made an updated version that also works on V1    http://powershellcentral.com/scripts/424 and still has naming, the Average column, formatting linecount -etc

    PoSH> Get-PerformanceHistory 7

    Duration Average Commmand
    -------- ------- --------
    5.29700 0.00011 1..50000 | % { dummy }
    4.03800 0.00008 1..50000 | % { $obj = 1 }
    1.16000 0.00002 for($i = 0; $i -lt 50000;$i++ ) { dummy }
    0.12400 0.00000 for($i = 0; $i -lt 50000;$i++ ) { $obj = 1 }
    1.52900 0.00003 1..50000 |  &{ process {dummy} }
    0.00000 0.00000 function k% ([scriptblock]$sb){process {&$sb }}
    9.32100 0.00019 1..50000 | K% { dummy }

    For the CTP2 version see :  http://powershellcentral.com/scripts/155 

    This function is very handy, it also uses get-history we do not need to rerun a command  to time it, and the processing has no effect on the testing as processing is afterwards, using data available anyway, so we are able to use it anytime without preparements , so for future tests I'm going  to use this function.

    enjoy,

    Greetings /\/\o\/\/

    Posted by MoW | 4 Comments
    Filed under: ,

    PowerShell in SQL Server 2008

    From PowerShell in SQL Server 2008

    The SQL Server team have approached PowerShell support in a different manner to other Microsoft products. They have chosen to create a SQL Server specific version of PowerShell in which the SQL Server functionality is pre-installed. This means that there will be two versions of PowerShell on any machine with SQL Server 2008 installed:

    • The original PowerShell
    • The SQL Server specific version including the SQL Server provider

    There are a number of points that must be remembered regarding the SQL Server PowerShell console:

    The SQL Server PowerShell console is closed. It is not possible to use Add-PSSnapin to add additional functionality.

    Join the rant here : SQL Server 2008 + PowerShell = NO NO NO NO NO!

    Protest,

    Greetings /\/\o\/\/

    Posted by MoW | 0 Comments
    More Posts Next page »