For the PowerShell calendar :
TechNet Webcast: An Overview of Windows PowerShell (Level 200)
Presenter: Don Jones, Scripting Guru, Author, SAPIEN Technology
Event Name: TechNet Webcast: An Overview of Windows PowerShell (Level 200)
Start Date: 12/19/2006
Start Time: 1:00 PM (GMT-08:00) Pacific Time (US & Canada)
End Date: 12/19/2006
End Time: 2:00 PM (GMT-08:00) Pacific Time (US & Canada)
Enjoy,
Greetings,
/\/\o\/\/
Tags : PowerShell
One question/remark I did hear very often at the IT Forum is about available books for PowerShell and about Editors for PowerShell .
Don Jones, from Sapiens, well know from the script Editor PrimalScript . (PrimalScript 4.1 introduces support for Microsoft's Windows PowerShell! and did go RTM at the same time as PowerShell, at the IT Forum.!!)
But they do much more, as giving Scripting Training (also PowerShell) and Publishing (SAPIEN Press)
Also they cover a lot of PowerShell material online : - A blog http://sapien.eponym.com/blog, - Scripting / PowerShell Forums :ScriptingAnswers.com and a search engine with PowerShell support: SearchScripting.com
Today Don Jones also did announce that the book he was writing Windows PowerShell: TFM has gone Gold,
See the following message I got from him :
Jeff Hicks and I are very pleased to announce that Windows PowerShell: TFM is now “gold,” meaning the manuscript is complete and should be off to the printer’s sometime this week. We’re just waiting on the cover design to be completed. The manuscript checked in at 470 pages, including the index, meeting our target for a highly on-topic book that administrators will find approachable and comfortable to read.
Pre-orders are available on Amazon.com, and our first official sample chapters are posted, along with a complete table of contents (all in one PDF) on www.SAPIENPress.com/powershell.asp. The book’s sample scripts are also available now as a free download for one and all, whether they’ve purchased the book or not.
We hope to announce shipping and general availability dates in December.
Some quick facts:
- Almost 500 pages of Windows PowerShell-flavored goodness
- Walks a Windows admin through the PoSH learning process from the ground up
- Focuses on task completion, helping admins get the job done faster
- Chosen by more than 50% of Amazon.com customers shopping for a PoSH title
- Currently in the top 100,000 books on Amazon.com
- Book is supported personally by the authors through a forum on www.ScriptingAnswers.com.
I did not read the book yet,only the preview chapter I did post about before, but when I do, I will do another post about my impression of the book but I'm very sure it will be a great read and this will be another "Must have" Book about PowerShell, from the sample chapters, seeing all the PowerShell coverage and support they provide online, and of course the writer Don Jones,who did write very good scripting books before.
I had the pleasure to meet Don Jones 2 times, at TechEd Boston and on the IT forum, on both events he also did give some impressive Demos of the PowerShell support in PrimalScript.He is a great guy, a real Scripting and PowerShell Guru, a good presenter and very good in explaining and teaching.
Hence, I'm sure I can recommend this book for learning PowerShell.
I also got the opportunity to, and did review the book from Bruce Payette that is available in the :Windows PowerShell in Action - EARLY ACCESS EDITION program, I finished reading it in my vacation, and I really loved this book, it is well written, good examples, and lots of inside and background information, the book does focus on the language itself and how PowerShell is structured and why ,starting at the core, as there are some advanced concepts that lay at the base of how PowerShell works,.
The PowerShell in Action book is a "must Read" , I think it is a great book, one of the best technical books I did read ever , More in a later post
the RTM book is a different format and from another viewpoint, so it could be good combination, a more in a later post when I also did read TFM.
So next to the excellent documentation (getting started and UserGuide) that does come with the PowerShell installation (if you did not do that yet, be sure to check it out !, it is really great !!! )
There are, next to the books about Monad and MSH beta s :
Monad (PowerShell Beta) see :/\/\o\/\/ PowerShelled: Series about Monad on Computerworld ,
and Lee holmes (who is Member of the PowerShell team and also has a great powerShell blog ) wrote O'Reilly PowerShell Quick Reference (see also his Blog O'Reilly PowerShell Quick Reference Now Available )
and also we have the Free E-book Don Jones did before See /\/\o\/\/ PowerShelled: Powershell links and free E-book :
Now allreay two very books, to learn more about PowerShell (release), that you can get your hands on, and there are much more in the pipeline ,amongs others : Professional Windows from Andrew Watt ,
Andrew Watt is a MVP and also is very active on the Newsgroup : microsoft.public.windows.powershell NewsGroup and also has a PowerShell blog : PowerShell Ponderings
Enjoy,
Greetings,
/\/\o\/\/
Tags : PowerShell
On the NewsGroup microsoft.public.windows.powershell ,
there was a Question about : Advanced renaming directly in Powershell?
The Question was about doing advanced file renaming, to rename a set of files like this :
file.ex2
file.ext
file_1.ex2
file_1.ext
file_2.ext
to the following format, Giving a base number and then rename the files and use the _3 as an offset to the number
file12.ex2
file12.ext
file13.ex2
file13.ext
file14.ext
Now the rename-object commandlet in PowerShell is very powerfull for this, some basic renaming might be a bit hard at first see also /\/\o\/\/ PowerShelled: PowerShell : How Can I Rename Files Using ... (a CSV file) and commments on this post Upgrading MSH, My first Windows PowerShell Commands about renaming *.msh to *.ps1,
But as the -NewName parameter takes a ScriptBlock as a parameter it is very handy to use some more advanced logic to rename file as you also can use the Power of the -Match and -Replce Operators RegEx support.
I Came up with this oneliner for this task :
PoSH>$num = 12
PoSH>ls file*.ex* | rename-item -newname {if($_.name -match '_(\d)'){$num = $num + $matches[1]};$_.name -replace "file.*\.", "file$num."} -WhatIfWhat if: Performing operation "Rename File" on Target "Item: C:\PowerShell\file.ex2 Destination: C:\PowerShell\file12.ex2".
What if: Performing operation "Rename File" on Target "Item: C:\PowerShell\file.ext Destination: C:\PowerShell\file12.ext".
What if: Performing operation "Rename File" on Target "Item: C:\PowerShell\file_1.ex2 Destination: C:\PowerShell\file13.ex2".
What if: Performing operation "Rename File" on Target "Item: C:\PowerShell\file_1.ext Destination: C:\PowerShell\file13.ext".
What if: Performing operation "Rename File" on Target "Item: C:\PowerShell\file_2.ext Destination: C:\PowerShell\file14.ext".
As there is happening a lot in this one line of code I'm going to work it out a bit more in this post.
lets first make the files and list them :
PoSH>sc file.ext 'a'
PoSH>sc file.ex2 'a'
PoSH>sc file_1.ex2 'a'
PoSH>sc file_1.ext 'a'
PoSH>sc file_2.ext 'a'PoSH>ls file*.ex*
Directory: Microsoft.PowerShell.Core\FileSystem::C:\PowerShell
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 11/25/2006 1:55 PM 3 file.ex2
-a--- 11/25/2006 1:54 PM 3 file.ext
-a--- 11/25/2006 1:55 PM 3 file_1.ex2
-a--- 11/25/2006 1:55 PM 3 file_1.ext
-a--- 11/25/2006 2:15 PM 3 file_2.ext
Now that we have listed the files we want to act upon ( al files starting with file and ending with .ex* )
we can pipe them to rename object :
PoSH>ls file*.ex* | rename-item -newname {$_.name} -WhatIf
What if: Performing operation "Rename File" on Target "Item: C:\PowerShell\file.ex2 Destination: C:\PowerShell\file.ex2".
What if: Performing operation "Rename File" on Target "Item: C:\PowerShell\file.ext Destination: C:\PowerShell\file.ext".
What if: Performing operation "Rename File" on Target "Item: C:\PowerShell\file_1.ex2 Destination: C:\PowerShell\file_1.ex2".
What if: Performing operation "Rename File" on Target "Item: C:\PowerShell\file_1.ext Destination: C:\PowerShell\file_1.ext".
What if: Performing operation "Rename File" on Target "Item: C:\PowerShell\file_2.ext Destination: C:\PowerShell\file_2.ext".
Note that in the scriptblock I provided to the -newname parameter I have access to the complete File Object in the $_ variable, I just use it to get the name property and do not do any processing yet, also note that I added the -WhatIf parameter so no changes are made and I we can test freely till we got it right without messing up your testfiles.
Now lets start by adding the Base Number for it, in this case 12 that I put in the variable $num
PoSH>$num = 12
PoSH>ls file*.ex* | rename-item -newname {$_.name -replace "file.*\.", "file$num." } -WhatIfWhat if: Performing operation "Rename File" on Target "Item: C:\PowerShell\file.ex2 Destination: C:\PowerShell\file12.ex2".
What if: Performing operation "Rename File" on Target "Item: C:\PowerShell\file.ext Destination: C:\PowerShell\file12.ext".
What if: Performing operation "Rename File" on Target "Item: C:\PowerShell\file_1.ex2 Destination: C:\PowerShell\file12.ex2".
What if: Performing operation "Rename File" on Target "Item: C:\PowerShell\file_1.ext Destination: C:\PowerShell\file12.ext".
What if: Performing operation "Rename File" on Target "Item: C:\PowerShell\file_2.ext Destination: C:\PowerShell\file12.ext".
In this first step we use the -replace operator to insert the basenumber given into the filename by replacing everything between the word file and the extension.
note that -replace uses RegEx expressions not wildcards so the expression looks like this : "file.*\." first the word file, then we need to use a dot . before the * that means any character and we need to escape the last (literal) dot by escaping it \.
but as you can see in the following steps this makes it much more powerfull as also the -mach operator uses the regular expression engine, so we can use the full RegEx power.
Next we need to raise the number for filesnames that do contain an _ with the number that is behind it.
we can use the mach operator to do this (note again that we use -WhatIf so are free just to return anything we want) :
PoSH>ls file*.ex* | rename-item -newname {$_.name -match '_' } -WhatIf
What if: Performing operation "Rename File" on Target "Item: C:\PowerShell\file.ex2 Destination: C:\PowerShell\False".
What if: Performing operation "Rename File" on Target "Item: C:\PowerShell\file.ext Destination: C:\PowerShell\False".
What if: Performing operation "Rename File" on Target "Item: C:\PowerShell\file_1.ex2 Destination: C:\PowerShell\True".
What if: Performing operation "Rename File" on Target "Item: C:\PowerShell\file_1.ext Destination: C:\PowerShell\True".
What if: Performing operation "Rename File" on Target "Item: C:\PowerShell\file_2.ext Destination: C:\PowerShell\True".
The -Mach operator returns $True or $False , but does more it also fills the $maches variable, lets look at this with a single filename to look how this works :
PoSH>"file_1.ex2" -match '_'
TruePoSH>$matches
Name Value
---- -----
0 _PoSH>"file_1.ex2" -match '_\d'
TruePoSH>$matches[0]
_1
You can see here that the $matches variable does contain the Capture found, in the second example I did add \d that means any digit.
but as with a fullBlown [regex] object also the -match operator supports subCaptures, so we can get the Number :
PoSH>$matches
Name Value
---- -----
1 1
0 _1PoSH>$matches[1]
1
So now we can combine the $true returned by -match and the capture made in $matches with an If statement to do the Math :
PoSH>if ("file_1.ex2" -match '_(\d)'){$matches[1]}
1
PoSH>if ("file_2.ex2" -match '_(\d)'){$matches[1]}
2
PoSH>if ("file_2.ex2" -match '_(\d)'){$num + $matches[1]}
14
and all can be combined the the one-liner I did post on the NewGroup, what also could be written like this in PowerShell to make it a bit more readable and you can still just past it into the PowerShell console
$num = 12
Dir file*.ex* |
rename-item -newname {
if($_.name -match '_(\d)'){
$num = $num + $matches[1]
}
$_.name -replace "file.*\.", "file$num."
} -WhatIf
I hope this post makes it clear how easy it is to make and test advanced rename jobs interactivly on the console by using -WhatIf and command history, and then while ready format as a script, and how powerfull the RegEx support of -match and -Replace is.
Enjoy,
Greetings,
/\/\o\/\/
Tags : PowerShell
On Rob van der Woude's Scripting Pages, there is an excelent PowerShell section,
with some good links and some examples.
one of the examples is how to test a year if it is a leap year.
this script did look a bit for such a trivial task in powerShell, as I did Know that the .NET [dateTime] class did have some handy methods, I started out like this :
PoSH>[datetime]::IsLeapYear(2006)
False
PoSH>[datetime]::IsLeapYear(2008)
True
but ofcourse this was not the only thing the script did do if you look at the original script we just replaced one line
# Check if the specified year is a leap year
[boolean]$leapyear = ( [boolean]!( $year % 4 ) -and [boolean]( $year % 100 ) ) -or [boolean]!( $year % 400 )
Read some info from the host
PoSH>[datetime]::IsLeapYear((read-host "Year:"))
Year:: 2006
FalsePoSH>[datetime]::IsLeapYear((read-host "Year"))
Year: 8
True
Add some formatting
PoSH>function get-leapYear ($year = (read-host "Year")){"$year = Leapyear : {0}" -f [datetime]::IsLeapYear($Year)}
PoSH>get-leapyear 2008
2008 = Leapyear : True
Combine asking the year and providing default value
PoSH>function get-leapYear ($year = (read-host "Year")) {if(! $year) {$year = (get-date).year} ; "$year = Leapyear : {0}
" -f [datetime]::IsLeapYear($Year)}
PoSH>get-leapYear
Year: 2008
2008 = Leapyear : True
PoSH>get-leapYear
Year:
2006 = Leapyear : False
PoSH>get-leapYear 12
12 = Leapyear : True
Gotha with 2 digit years
PoSH>(get-date -year 12).year -lt (get-date).year
True
PoSH>(get-date -year 2012).year -lt (get-date).year
False
below 30 use 2000 rangle
PoSH>[datetime]::ParseExact("12","yy",$null).year
2012
so to solve that :
PoSH>function get-leapYear ($year = (read-host "Year")) {if(! $year) {$year = (get-date).year} ; if ($year -lt 100){$yea
r = [datetime]::ParseExact($year,"yy",$null).year};"$year = Leapyear : {0}" -f [datetime]::IsLeapYear($Year)}
PoSH>get-leapYear 12
2012 = Leapyear : True
Formatting of the past, present or future
PoSH>switch ((get-date -year 2007).year - (get-date).year){0 {"is"} default {if ($_ -gt 0){"will be"} else {"was"}}}
will be
PoSH>switch ((get-date -year 2005).year - (get-date).year){0 {"is"} default {if ($_ -gt 0){"will be"} else {"was"}}}
was
PoSH>switch ((get-date -year 2006).year - (get-date).year){0 {"is"} default {if ($_ -gt 0){"will be"} else {"was"}}}
is
So complete :
PoSH>function get-leapYear ($year = (read-host "Year")) {if(! $year) {$year = (get-date).year} ; if ($year -lt 100){$yea
r = [datetime]::ParseExact($year,"yy",$null).year};"$year {0} a Leap Year" -f $(switch ((get-date -year $year).year - (g
et-date).year){0 {"is {0}"} default {if ($_ -gt 0){"will {0} be"} else {"was {0}"}}}) -f $(if ( ![datetime]::IsLeapYear(
$Year)){"NOT"})}
PoSH>get-leapYear 2005
2005 was NOT a Leap Year
PoSH>get-leapYear 2006
2006 is NOT a Leap Year
PoSH>>get-leapYear 2007
2007 will NOT be a Leap Year
PoSH>get-leapYear 2008
2008 will be a Leap Year
PoSH>get-leapYear 12
2012 will be a Leap Year
So you can see the sample was not that bad except for not using the [dateTime]::IsLeapYear() function .
but I hope you see from above examples how flexible you can work in PoSH, bu handling the input and formatting in different ways as I tested this all as onliners interactively.
now ofcourse some whitespace an variables would be handy.
Enjoy,
Greetings,
/\/\o\/\/
Tags : Monad PowerShell
October 2005 November 2005 December 2005 January 2006 February 2006 March 2006 April 2006 May 2006 June 2006 July 2006 August 2006 September 2006 October 2006 November 2006 December 2006