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

Happy New Year !

I’m very happy and proud that I received a e-mail from Microsoft that I got my 4th MVP Award, and may call myself a PowerShell MVP for another year, an excellent start for me ;-).

Dear Marc van Orsouw,
Congratulations! We are pleased to present you with the 2009 Microsoft® MVP Award! This award is given to exceptional technical community leaders who actively share their high quality, real world expertise with others.

Best Wishes for the year 2009  and may the Power of the Shell be with you !

Greetings /\/\o\/\/

Posted by MoW | 3 Comments
Filed under: ,

Regular Expressions and PowerShell Part 2

A helpful tool on the way in this series is Robby Foust’s  RegEx Quick Reference in PoSHcode.org.

The function returns an Object, so you can easy format and filter it to your liking.

I like this view the most:

get-regex | Format-Table Sequence,meaning -GroupBy table –AutoSize

As having a quick regex reference around this one is

POSH> get-regex |? {$_.table -match 'Character classes'}| Format-Table Sequence,meaning -GroupBy table -AutoSize

   Table: Character classes and class-like constructs

Sequence Meaning
-------- -------
[...]    A single character listed or contained within a listed range.
[^...]   A single character not listed and not contained within a listed range.
.        Any character, except a line terminator (unless single-line mode, s).
\w       Word character.
\W       Non-word character.
\d       Digit.
\D       Non-digit.
\s       Whitespace character.
\S       Non-whitespace character.
\p{prop} Character contained by given Unicode block or property.
\P{prop} Character not contained by given Unicode block or property.

Note that I use the –Match operator in this example to filter only the Character Classes in a where (?) clause, a place using a RegEx can be handy.

let’s go for part 3 of the series, there is Some PowerShell mentioned but I wanted to add this,

you can use static methods from the .NET framework (system.regularBLOCKED EXPRESSION as in the VB.NET/C# examples using the [regex] typeacceloprator.

POSH> [regex]::IsMatch('12345','^\d{5}$')
True
POSH> [regex]::IsMatch('123456','^\d{5}$')
False

there was also was a question about linenumbers in the webcast, you can use select-string also for this that gives linenumbers (and can do context etc. and also can work with regexes

POSH> type computers.txt
localhost
foo
localhost
POSH> cat .\computers.txt | select-string 'foo'

foo

POSH> cat .\computers.txt | select-string 'foo' | format-list

IgnoreCase : True
LineNumber : 2
Line       : foo
Filename   : InputStream
Path       : InputStream
Pattern    : foo
Context    :
Matches    : {foo}

POSH> cat .\computers.txt | select-string 'foo' | ft line* -a

LineNumber Line
---------- ----
         2 foo

You can see that select string seems to return text but that is just the default formatter, you get back more info.

and then pick you just the info you want

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 2 Comments
Filed under: ,

Regular Expressions and PowerShell Part 1

Shay pointed me to this excellent series about Regular Expressions Regular Expression Webcast Series , While following this series to fresh up my Regex skill’s, I will post about PowerShell Specifics in this series here.

There are more ways to work with regular expressions with PowerShell, for following the exercises in Part 2 of the series, we can limit us to the –Match Operator.

I this Post I show how to do the presented exercises in PowerShell (Note follow the Second webcast to see complete examples and background I just picked some examples to show the translation to using the –Match operator in PowerShell.

Regular expressions can be used in a lot of places in Powerhell so adding regex skills to your PowerShell toolset will be very profitable in the long time !

So let’s get started with watching the Webcasts and following allong in PowerShell, picking up PowerShell regex specifics on the way ..  

First a little caveat you might encounter with the  –Match  Operator and might confuse you, if you do not know about this behavior, the  –Match operator will react different on scalar values and Collections :

PoSH> '7' -match '7'
True
PoSH> '7','77' -match '7'
7
77
PoSH> '7' -match '7'
True
PoSH> $matches

Name                           Value
----                           -----
0                              7

In the case of a Scalar a Boolean value is return if matched and in case of a collection the matched items are returned.

Note that you can check up on the actual Matchs(and or submatches but more about that later) by inspecting the $matches variable

another thing you have to take note of is that the –Match operator is Case Insensitive by default, as we can see in the Word examples later in the second webcast.

PoSH>  $names = 'jack','jim','betty','sue','barry','Henry','harry'
PoSH>> $names  -match '^h'
Henry
harry
PoSH> $names  -cmatch '^h'
harry
 

you can see to do a Case sensitive comparison, we can use the –cMatch Operator in PowerShell.

This is enough I think to get you trough the samples in the Webcast first the first 2 chapters, looking at the code samples below.

In the Second part so examples are show to mach ZIP codes here are some of of those examples translated to PowerShell to make it more easy to follow the examples in the webcast on PowerShell

'75603','78654','80765','90120','08765','76543','23456','56732' -match '7'

To make it more easy to follow the exercises we store this list in a variable, to make the line a bit shorter (although we can get the list back using the Up arrow, this makes it more readable)  :

$zip = '75603','78654','80765','90120','08765','76543','23456','56732'

$zip -match '^(75|78)'

Some more examples :

$zip -match '^[275]'
$zip -match '^[3-6]'
$zip -match '^[^78]'

$names = 'jack','jim','betty','sue','barry','Henry','harry'
$names  -match '^h'
$names  -cmatch '^h'
$zip -match '^[0-9]{5}'
$zip -match '^[0-9]{2,5}'
$zip -match '^\d{5,}'
$zip -match 'a*
'

I only watched the first to parts of the series but will follow up on how to do more advanced regex stuff in PowerShell when needed in following the Webcast series in PowerShell.

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 2 Comments
Filed under: ,

Jingle Shell, Jingle Shell, PowerShell V2 CTP3 is RTW.

Download Windows PowerShell V2 CTP3

Download WinRM 2.0 CTP3 (required for PowerShell remoting)

Enjoy,

Greetings /\/\o\/\/

 

 

Posted by MoW | 1 Comments
Filed under: ,

New Spaghetti Code Podcast: Neil Iversen Discusses Microsoft PowerShell

New Spaghetti Code Podcast: Neil Iversen Discusses Microsoft PowerShell

Spaghetti Code discusses Microsoft Powershell with Neil Iversen. Neil describes how Powershell is the combination of command line simplicity with the power of .NET.  He also talks about how Powershell can make life easier for developers and how Powershell exposes new capabilities to the applications they create.

A very nice Podcast about what PowerShell and the comunity can do for Developers. 

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 0 Comments
Filed under: ,

PowerShell V2 CTP3 teaser : list PST files on a list of computers using background jobs

At work I had to list al PST files on a list big of computers, as searching for PST files is a timeconsuming job it should be possible to run the job concurently, so I thought that it was a good opportunity to try in PowerShell V2.   

 as I have "The PDC build" of WIndows 7 running, that has a Post-CTP2 version of PowerShell included I prototyped this example., As I noted that he needed CTP2 for it to run, as seemed after he dowloaded the CTP2 build of PowerShell V2, that it was not possible in yet CTP2, As CPT3 is not yet released to general public  (but will be sometime this Month) , he could not use it, but  I still decided to post it on my blog as a teaser (sorry, but it was so Cool I could not resist) :

 

# start job, with max of 10 Job running at the same time 

 

$job = get-wmiobject -query "Select * from CIM_DataFile Where Extension = 'pst'" -ComputerName (Get-Content C:\powershell\computers.txt) -asJob -ThrottleLimit 10

 

# check status

 

$job

 

# Display results on screen, preserving the data

 

Receive-Job -Keep $job | select CSName,FileName,Description,FileSize

 

# when all jobs are finished, we can export the results to a  CSV file

 

Receive-Job $job | select CSName,FileName,Description,FileSize | Export-Csv PstFiles.csv –NoTypeInformation

You can see it is, when you have the needed WQL query sting, simple enough, so simple even that we do not even need a "Stinking Script" anymore, we can just do it it interactive in a console session and check the results inbetween before exporting it to a CSV file in another simple oneliner :

  

The result looks like this :

 

PS C:\PowerShell>  get-wmiobject -query "Select * from CIM_DataFile Where Extension = 'pst'" -ComputerName (Get-Content
C:\powershell\computers.txt) -asJob -ThrottleLimit 10
PS C:\PowerShell>
PS C:\PowerShell> $job = get-wmiobject -query "Select * from CIM_DataFile Where Extension = 'pst'" -ComputerName (Get-C
ntent C:\powershell\computers.txt) -asJob -ThrottleLimit 10
PS C:\PowerShell>
PS C:\PowerShell>
PS C:\PowerShell>
PS C:\PowerShell> # check status
PS C:\PowerShell>
PS C:\PowerShell>
PS C:\PowerShell>
PS C:\PowerShell> $job

Id              Name            State      HasMoreData     Location             Command
--              ----            -----      -----------     --------             -------
5               Job5            Running    False           localhost,foo,loc... Get-WMIObject


PS C:\PowerShell>
PS C:\PowerShell>
PS C:\PowerShell>
PS C:\PowerShell> # Display results on screen, preserving the data
PS C:\PowerShell>
PS C:\PowerShell>
PS C:\PowerShell>
PS C:\PowerShell> Receive-Job -Keep $job | select CSName,FileName,Description,FileSize
PS C:\PowerShell> Receive-Job -Keep $job | select CSName,FileName,Description,FileSize
Receive-Job : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:1 char:12
+ Receive-Job <<<<  -Keep $job | select CSName,FileName,Description,FileSize
    + CategoryInfo          : InvalidResult: (:) [Receive-Job], COMException
    + FullyQualifiedErrorId : JobStateFailed,Microsoft.PowerShell.Commands.ReceiveJobCommand

PS C:\PowerShell> $job.ChildJobs

Id              Name            State      HasMoreData     Location             Command
--              ----            -----      -----------     --------             -------
6               Job6            Completed  True            localhost
7               Job7            Failed     False           foo
8               Job8            Completed  True            localhost


PS C:\PowerShell> Receive-Job -Keep $job | select CSName,FileName,Description,FileSize

CSName                        FileName                      Description                                        FileSize
------                        --------                      -----------                                        --------
MOW7                          mow                           c:\users\mow\appdata\local...                        271360
Receive-Job : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:1 char:12
+ Receive-Job <<<<  -Keep $job | select CSName,FileName,Description,FileSize
    + CategoryInfo          : InvalidResult: (:) [Receive-Job], COMException
    + FullyQualifiedErrorId : JobStateFailed,Microsoft.PowerShell.Commands.ReceiveJobCommand

 

As we needed this in production we in the end used another solution, but you can see how easy this will become in PowerShell V2

 

Enjoy soon,

 

Greetings /\/\o\/\/

Posted by MoW | 2 Comments
Filed under: ,

YMCA

Read the full story here :

http://powershellug.com/blogs/richardsiddaway/archive/2008/11/08/ymca.aspx

* Update *

See the Pictures here : http://jonathanmedd.blogspot.com/2008/11/ymca.html 

* Update 2 *

read the rest of the full story here :Gesticulating Ben Pearce

 

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 2 Comments
Filed under: ,

TechNet Webcasts about PowerShell

TechNet Webcast: Introduction to Windows PowerShell (Level 200)

 
Language(s): English.
Product(s): Windows Server 2008.
Audience(s): IT Professional.
Duration: 90 Minutes
Start Date:
Monday, December 08, 2008 11:30 AM Pacific Time (US & Canada)
 

Event Overview

In this webcast, we provide an introduction to Windows PowerShell for IT professionals and show how to automate client administrative activities in the Windows Server 2008 and Windows Vista operating systems. We explore the features and capabilities of Windows PowerShell, describe customer scenarios to manage day-to-day server and client administration activities, and discuss command-line syntax usage.

Presenters: John Baker, IT Pro Evangelist, Microsoft Corporation, and Yung Chou, IT Pro Evangelist, Microsoft Corporation

TechNet Webcast: Advanced Windows Powershell Scripting (Level 400)

Language(s): English.
Product(s): Windows Server 2008.
Audience(s): IT Professional.
Duration: 90 Minutes
Start Date:
Monday, December 15, 2008 11:30 AM Pacific Time (US & Canada)
 

Event Overview

In this webcast, we look at some of the powerful scripting capabilities of Windows PowerShell. First, we introduce some of the basics of Windows PowerShell.  We examine how the help system works and review the Windows PowerShell command structure. We also discuss how to format output and some basic Windows PowerShell commands. Next, we explore variable declaration and scripting constructs in Windows PowerShell to perform basic programmatic functions, like loops and branching. Then we look at advanced scripting techniques involving functions, filters, script blocks, and error handling. We also look briefly at how to use Windows PowerShell with Windows Management Instrumentation (WMI) to gain access to a wide variety of system objects and settings. Finally, we look at how to use the Microsoft .NET Framework to build a simple form using Windows PowerShell.

Presenters: John Baker, IT Pro Evangelist, Microsoft Corporation, and Yung Chou, IT Pro Evangelist, Microsoft Corporation

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 3 Comments
Filed under: ,

Azure Services Training Kit and PowerShell

Following a note on this blogpage about Powershell for needed for the Azure Services Training Kit, I decided to check it out :

And yes the dowload page states you need PowerShell for  this, so I downloaded this Kit and did a DIR -REC on the directory for .PS1 files :

    Directory: C:\AzureServicesKit\Assets\DependencyChecker\Scripts\Dependencies\Check


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        24-10-2008     20:01        501 CheckASPNETFutures.ps1
-a---        24-10-2008     20:01        484 CheckCCT.ps1
-a---        24-10-2008     20:01         98 CheckIis6Comp.ps1
-a---        24-10-2008     20:01        162 CheckIis7AspNet.ps1
-a---        24-10-2008     20:01        517 CheckLiveFrameworkTools.ps1
-a---        24-10-2008     20:01        269 CheckLiveServicesSDK.ps1
-a---        24-10-2008     20:01        493 CheckNET35.ps1
-a---        24-10-2008     20:01        497 CheckNET35SP1.ps1
-a---        24-10-2008     20:01        758 CheckNetServicesSDK.ps1
-a---        24-10-2008     20:01        112 CheckServer2008.ps1
-a---        24-10-2008     20:01        468 CheckSHSDK.ps1
-a---        24-10-2008     20:01        486 CheckSilverlight.ps1
-a---        24-10-2008     20:01        492 CheckSilverlightDevTools.ps1
-a---        24-10-2008     20:01        490 CheckSQL2008.ps1
-a---        24-10-2008     20:01        497 CheckVS2008.ps1
-a---        24-10-2008     20:01        895 CheckVSTSSP1.ps1
-a---        24-10-2008     20:01        162 CheckWCFHTTPAct.ps1
-a---        24-10-2008     20:01        162 CheckWinAuth.ps1
-a---        24-10-2008     20:01         85 CheckWmiIis7.ps1
-a---        24-10-2008     20:01        498 CheckZermattSDK.ps1
-a---        24-10-2008     20:01         59 QueryWmiIis7.ps1


    Directory: C:\AzureServicesKit\Labs\AdvancedSQLDataServices\Assets


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         6-11-2008     18:25       2828 SDSCreateStorage.ps1
-a---         6-11-2008     18:25        176 SDSFinalize.ps1
-a---         6-11-2008     18:25        595 SDSSetup.ps1


    Directory: C:\AzureServicesKit\Labs\ConcurrencySQLDataServices\Assets


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         6-11-2008     18:25        459 SDSCreateStorage.ps1
-a---         6-11-2008     18:25        176 SDSFinalize.ps1
-a---         6-11-2008     18:25        595 SDSSetup.ps1

 

you can see there are some basic prereq tests written in powerShell

I decided to do a quick check on the contents of the scripts

 PS C:\AzureServicesKit> ls . -rec *.ps1 | gc 

In the first directory we can find some scripts where PowerShell is just used to do some Dependency checks, all of them very simple and some are even oneliners (e.g. registry checks) and make for excelent example scripts to get started with PowerShell

I nice helper function that came back in a lot of the Dependency check scripts :

function SearchUninstall($SearchFor)
{
$uninstall = "HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
$uninstallObjects = ls -path $uninstall ;
$found = $FALSE;

foreach($uninstallEntry in  $uninstallObjects)
{
   $entryProperty = Get-ItemProperty -path registry::$uninstallEntry
   if($entryProperty.DisplayName -like $searchFor)
    {
       $found = $TRUE;
       break;
    }
}

$found;
}

SearchUninstall -SearchFor 'Microsoft SQL Server 2008*';

and in the SDS (SQL Data Services) examples a custom snapin is used :

#Register the commandlets
$psSnapInDll = resolve-path Microsoft.Samples.AzureServices.PowerShell.dll
&$env:windir\Microsoft.net\Framework\v2.0.50727\installutil.exe -i $psSnapInDll >> $null

#Add the commandlet
Add-pssnapin AzureServicesManagement
. .\SDSSetup.ps1

 You can see that PowerShell is mostly just used a GLUE here, and  to provide access to a Custom DLL (SnapIn) as you mostly find in an SDK also.

This may not look much added value to make this Training Kit depending on PowerShell, but it (the Snapin Provided)  does make Programatic (Script) access much easier for administrators as the Former SDK managed DLL only solution, with the sample scripts to get started with.

Also the other provided sample scripts are a good starting point for learning PowerShell a bit like the PowerShell support in SBS (Small Business  Server), an not intrusive way to get into PowerShell as Susan Bradley, an SBS MVP, did a very good job in explaining in the latest PowerScripting Podcast : 

Episode 49 - PowerScripting Podcast - Susan Bradley

So I think this is an excelent evolution ;-)

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 2 Comments
Filed under: ,

TechEd IT Pro EMEA PowerShell sessions online

The High quality video recordings of the PowerShell Panel discussion are now online here  :

http://microsofttech.fr.edgesuite.net/TechEdOnline/Videos/406_high.wmv

 The following PowerShell Breakout sessions from Teched are online now :

This session focuses on how to remotely administer both clients and servers throughout your enterprise, using PowerShell V2. Firstly, this session will cover the infrastructure requirements necessary to successfully use PowerShell V2. The session will then introduce and explain PowerShell scripts that solve real world administrative challenges on an enterprise scale.
 
PowerShell V1 delivered a radically simplified approach to the traditional problem of interactive Shell, Scripting and Admin GUIs. Come and learn how V2 makes it easier to create and debug your scripts with a graphical environment, write production quality scripts using script cmdlets, modules and transactions, manage large distributed environments using WSMAN, improved WMI support and the new Unified Code Execution Model, and write smaller simpler scripts through improvements to the language and new/improved cmdlets.
 
Dimitry's Speaker Idol session can also be found there (can't find a direct link but search for PowerShell) 
 
and also there is a new PowerScripting Podcast :
 
 
Enjoy,
Greetings /\/\o\/\/
Posted by MoW | 0 Comments
Filed under: ,

TechEd EMEA Powershell Panel Discussion

 Yesterday we had a great Powershell Panel Discussion and you can already find the audio version online.

http://get-scripting.blogspot.com/2008/11/get-scripting-podcast-episode-4-teched.html 

This episode was recorded live on Tuesday 4th November at TechEd EMEA, Barcelona and features only the recorded Powershell Panel Discussion.

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 0 Comments
Filed under: ,

TechEd EMEA 2008 IT Professionals Barcelona

Just one week to go for TechEd EMEA 2008 IT Professionals Barcelona.

I'm really looking forward to it again as there will be much exciting PowerShell content, and other interesting sessions

Some people on my "finally get a chance to meet, or meet again " list will be there, and of course a lot new people to meet.

I also will be at the  PowerShell  Booth in the ATE (Ask the Experts ) area again this year along with the following PowerShell MVP's  :

Dmitry Sotnikov

Richard Siddaway

Tobias Weltner

So good chances you can find me there in between sessions and at the following times I'm scheduled in so yo can be sure to find me at the  Booth:

Tuesday 4 Nov - Day 2 13:00 17:00

Wednesday 5 Nov - Day 3 13:00 15:45

Friday 7 Nov - Day 5 10:00 14:00

So if you are at TechEd next week, be sure to drop by at the PowerShell ATE Booth, to ask a question or just for a chat,

Hope to see you there !

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 2 Comments
Filed under: ,

Hey PowerShell Guy !, how can I use get-content to analyze sql server logfile ?

I got a nice question the the comments, how can I use get-content to analyze sql server logfile ?

Ran has a SQL logfile like this : 

2008-10-21 09:51:51.40 spid3 "ERROR:" 
2008-10-22 09:51:52.04 spid3 "OK" 
2008-10-22 01:51:52.04 spid5 "ERROR:" 
2008-10-22 09:51:52.04 spid3 "OK" 
2008-10-23 01:51:52.04 spid5 "ERROR:"
2008-10-23 09:51:52.04 spid5 "ERROR:"  
2008-10-23 09:51:52.04 spid3 "OK" 
2008-10-24 09:51:52.04 spid5 "ERROR:" 
2008-10-24 09:51:52.04 spid5 "ERROR:" 
2008-10-24 09:51:52.04 spid3 "OK" 
2008-10-25 09:51:52.04 spid5 "ERROR:" 
2008-10-25 09:51:52.04 spid5 "OK" 
2008-10-25 09:51:52.04 spid5 "ERROR:"

and wanted want to be be able to search for errors ( "ERROR:" )only when the date is yesterday or today

the Quick and Dirty answer (as it is late early ) I made interactive in the PowerShell console  :

(Get-Content C:\powershell\sample_ERRORLOG.txt) |? {[void]($_ -match '((.*)\d\d spid)');(get-date -date $matches[2]) -gt (get-date).AddDays(-2)} |? {$_ -match 'ERROR'}

the Result :

image

You can see it takes a literal 2 day's back filter up till second level, hence you can see 1 error record of 2009-10-13

As I do filtering on converted datetime objects al in this single one-liner it is a bit  cryptic and the target calls for a bit more clear and robust solution for repeated usage.

So in a next post, I will follow up on this one and take this one-liner into it's parts to do some more explaining and  build a small script to do this task.

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 4 Comments
Filed under: ,

I *heart* PowerShell

When I saw the example in I *heart* python , and the remark about PowerShell in it, I could not resist doing the same exercise in PowerShell :

$codes = @{}

# some existing coded for testing and make sure we have at least one duplicate

$existingCodes = 'poipoi','blinky'
$codes['poipoi']= 0

# Create new codes

$r = new-object random
(
1..500)|% {
 
$tmp = ''
 
1..6 |% {$tmp += [char]$r.Next(97,123)}
 
$codes[$tmp]= 0
}

# Duplicates

$duplicates = $codes.keys|? {$existingCodes -contains $_}

# valid ones

$valid = $codes.keys|? {$existingCodes -notContains $_}

Remarks

  • I use the same dictionary trick, only in PowerShell it is called a HashTable and is declared like this @{}  
  • As I was to lazy to type the Alphabet I created the letter list first :
    • $letters = [char[]](97..122)
  • but later as I needed a random character anyway, I included this logic directly in the loop
  • As I suspect that we need the valid ones more as the Duplicates I store them in $valid

* Update * to filter out the letters (I missed in example above) the script again using $letters Without l or q

$Codes = @{}
$letters = [char[]](97..122) -notmatch 'l|q'
$r = new-object random
(1..500) |% {
  $tmp = ''
  1..6 |% {$tmp += $letters[($r.Next(0,24))]}
  $codes[$tmp] = 0
}

So if you do not have (Iron)Python handy, you can use PowerShell as well to create a on-the-fly list of password-style codes for e-xamit.

Enjoy,

Greetings /\/\o\/\/

Posted by MoW | 1 Comments
Filed under:
More Posts Next page »