Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

Tuesday, July 1, 2008

2008 Microsoft MVP Award

 mvp
I got an email from Microsoft regarding the 2008 Microsoft MVP Award. I was chosen for the award in the Admin Frameworks category (Windows PowerShell).

What a day! What can I say. It feels like walking on the moon. It is an honor to be among the great guys. I would like to thank the people who supported me along the way and to Microsoft for the award.

PowerShell ROCKS!

Monday, June 30, 2008

Do you know your aliases?

 

This little PowerShell script you can check how familiar you are with all the built-in aliases in PowerShell V2 CTP2.
For each alias there are 7 cmdlet names to choose from. There are 10 questions, and at the end of the quiz you can review your results and optionally run the quiz again.

You need to have PowerShell V2 CTP2 installed for the script to run.


Good luck :)

 

 

Thursday, June 26, 2008

ActiveXPoSH - PowerShell for VBScript/JScript

 

SAPIEN Technologies has released a new COM component named ActiveXPoSH (included in PrimalScript 2007 script editor). It is a special PowerShell host that allow developers/scripters to run single cmdlets or entire scripts from VBScript/JScript code. The component is free for personal and commercial use.

You can find more details in this document and download it from here.

Saturday, June 21, 2008

Generating TinyURLs from PowerShell

 

TinyURL is a web service that provides short aliases for long URLs. Here's a function to generate URL aliases using TinyURL's API. The returned URL is short and never expires.

 
function New-TinyUrl($url){
    (new-object net.webclient).downloadString("http://tinyurl.com/api-create.php?url=$url") 
}
 
PS > $vug = New-TinyUrl -url http://marcoshaw.blogspot.com/2008/06/windows-powershell-virtual-user-group.html
PS > $vug
PS > http://tinyurl.com/4k9ons

 

Test it in your default browser:

PS > (new-object -com shell.application).open($vug) 

 

-Shay

Friday, June 20, 2008

Windows PowerShell Virtual User Group #6

 

Marco's Windows PowerShell Virtual User Group #6 is going to take place next week:
Tuesday, Jun 24 7:30pm – 10:30pm (Timezone: Eastern Time)

 

Agenda

8:00PM-8:10PM: Introduction
8:10PM-8:40PM: Bruce Payette (Microsoft)
8:40PM-9:10PM: Wassim Fayed (Microsoft)
9:10PM-9:40PM: Hal Rottenberg
9.40PM-9:50PM: Closing

 

Speaker topics

Bruce Payette : PowerShell v2 modules.
Wassim Fayed : PowerShell v2 remoting.
Hal Rottenberg: Managing VMware Infrastructure using PowerShell.

 

To join for video:
https://www.livemeeting.com/cc/winserver_usergroup/join?id=5H2F88&role=attend&pw=8%5E-%5CzqX

To join for audio:
Toll free: +1 (866) 553-8146
Participant code: 5592528585

 

Required login information (via LiveMeeting)

Meeting ID: 5H2F88
Entry Code: 8^-\zqX

 

Mark your calendar :)

Wednesday, June 18, 2008

Portable PowerShell

 

Hallelujah! Get all the details on Karl Prosser [MVP] blog.

Tuesday, June 17, 2008

Webcast: Managing Hyper-V Virtual Machines with WMI and Windows PowerShell

 

Start Date: Wednesday, July 02, 2008 8:00 AM Pacific Time (US & Canada)

 

Event Overview

Before the beta release of Microsoft System Center Virtual Machine Manager 2008, there was only one option for automating management of Hyper-V virtual machines—Windows Management Instrumentation (WMI). In this webcast, you learn how to automate the most common management tasks, how to create a delegation model using Windows Authorization Manager, and where the power of WMI can pick up where Virtual Machine Manager leaves off. In addition, the Hewlett-Packard Windows Server 2008 Academy team shares their experience automating the deployment of 600 virtual machines in less than three hours using WMI and Windows PowerShell.

 
Presenters: Dung Hoang Khac, Principal Consultant, HP, and Aric Bernard, Senior Technology Consultant, HP

 

Register for the event.

Listing enumeration type values (updated)

 

The old function return duplicate names when the enumeration has two names with the same value, for example: 

 

PS >  Get-EnumValues Microsoft.PowerShell.ExecutionPolicy

        Name Value
        ---- -----
Unrestricted     0
RemoteSigned     1
   AllSigned     2
  Restricted     3
  Restricted     3

 

Here is the modified version of the function:

function Get-EnumValues([type]$type){ 
   [enum]::getNames($type) | select @{n="Name";e={$_}},@{n="Value";e={$type::$_.value__}} | ft -auto
}

 

And the results:

PS >  Get-EnumValues Microsoft.PowerShell.ExecutionPolicy

Name         Value
----         -----
Unrestricted     0
RemoteSigned     1
AllSigned        2
Restricted       3
Default          3
 
-Shay

Monday, June 16, 2008

Scripting Sysadmin meme

 

In response to Steve's Scripting/Sysadmin Meme

 

How old were you when you started programming?

I was 27 when I started to write HTML/ASP pages.

 

What was your first machine?

My first real computer was Windows 95 PC. When I was 12 (I think), my uncle gave me a Texas Instruments (I can't remember the model name) but at that time I wasn't a computer geek.

 

What was the first real script you wrote?

Looping through ADO recordset and populating Active Server Pages (ASP).

 

What scripting languages have you used?

JavaScript/DHTML, VBScript/JScript.
Now that PowerShell is out all other scripting languages are set aside.

 

What was your first professional sysadmin gig?

I started as a sysadmin in a government institution back in 1998 and I'm still holding that position (aside from one year in which I left and returned).

 

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

Yes. Ever since I have been in the field I have found that being in IT brings out the best in  me.

 

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

Learn to script! Knowing to script is your sysadmin added value, sorts of a 'super sysadmin' if you will. You'll be able to do magic when your boss wants something that the GUI can't provide.

 

What’s the most fun you’ve ever had scripting?

Every day that passes with Windows PowerShell scripting is a great deal of fun. I learn something new every day.

 

Who am I calling out?

Alexandair Nicolic, Rolf Masuch and Andy Schneider

Sunday, June 15, 2008

PowerShell Popcorn

 

My son heard all the alien beeps and asked me for this one, the Popcorn tune. I couldn't say no :)

 

1..2  | % {
    
    1..2 | % {900,800,900,700,550,700,450 | % { [console]::beep($_,50); sleep -m 150}; sleep -m 250}
    
    900,1000,1050,1000,1050,1000,1050,900,1000,900,1000,800,900,800,900 | % { [console]::beep($_,50); sleep -m 150}
    sleep -m 250
    
    1..2 | % {900,800,900,700,550,700,450 | % { [console]::beep($_,50); sleep -m 150}; sleep -m 250}
    
    900,1000,1050,1000,1050,1000,1050,900,1000,900,1000,800,900,1000,1050 | % { [console]::beep($_,50); sleep -m 150}
    sleep -m 250    

    1..2 | % {
        1..2 | % {1350,1250,1350,1050,800,1050,680 | % { [console]::beep($_,50); sleep -m 150}; sleep -m 250}
        1350,1500,1600,1500,1600,1500,1600,1350,1500,1350,1500,1200,1350,1200,1350 | % { [console]::beep($_,50); sleep -m 150}
        sleep -m 250
    }
}

Saturday, June 14, 2008

Webcast - Using Windows PowerShell with Visual Studio 2008

 

Event Overview

Windows PowerShell is arguably one of the coolest things to happen to developers and administrators alike. Harness the power of the Microsoft .NET Framework from the command line and learn how to write scripts in no time at all. With Windows PowerShell, we leverage your current .NET Framework experience to turn you into one heck of a good scripter.

Presenter: Zain Naboulsi, Developer Evangelist, Microsoft Corporation

The webcast is available for offline viewing.

Friday, June 13, 2008

Enhanced CSV file handling in CTP2

 

In PowerShell v1 you could import comma separated CSV files only. That's not a problem anymore in CTP2. The Import/Export CSV cmdlets got enhanced and now they support a new -delimiter parameter:

 

PS > get-help import-csv -Parameter delimiter

-Delimiter [<char>]
    Specifies the delimiter that separates the property values in the file that is being imported. The default is a com
    ma (,). Enter a character, such as a semicolon (;).

    Required?                    false
    Position?                    2
    Default value                ,
    Accept pipeline input?       false
    Accept wildcard characters?  false

 

Note that the delimiter is limited to a single character.

# Export processes to a space separated csv file
PS > get-process | export-csv -delimiter " " -path processes.csv
# Convert csv files: comma separated to tab separated

PS > import-csv test.csv | export-csv -del "`t" -p test1.csv

Wednesday, June 11, 2008

Close Encounters of the PowerShell Kind

 


Five tones:

$notes = 900,1000,800,400,600
$duration = 400,400,400,400,1600
0..($notes.length-1) | % {[console]::beep($notes[$_],$duration[$_]) }

 

Spaceship Siren:

1..10 | % { 370..470 | % { [console]::beep($_,5) }}

 

Talking alien :)

900,500,1000,800,400,600,1200,300,200,500,1100,300 | %{[console]::beep($_,100)}

 

CTP2 Alien:

1..20 | foreach { 
    
    $rnd = (Get-Random -min 0 -max 20)*100
    
    if($rnd -eq 0) {
        sleep -m 10
    } else {
        [console]::beep($rnd,100)
    }
}

CTP2 - Get Process/Service from remote computers

 

One major change to the Get-Process/Get-Service cmdlets in CTP2 is the ability to get objects from remote computers via a new parameter, computerName (or -c for short). You can map process/service objects to their computer by the machineName member.

 

PS > Get-Service s* -c computer1,computer2 | select name,machineName

Name                  MachineName
----                  -----------
sacsvr                computer2
SamSs                 computer1
SCardSvr              computer1
Schedule              computer1
seclogon              computer1
SENS                  computer1
ServeRAIDManagerAgent computer2
(...)

PS > Get-Process s* -c computer1,computer2 | select name,machineName

Name          MachineName
----          -----------
searchindexer computer1
services      computer2
services      computer1
slp_srvreg    computer2
smax4pnp      computer1
smss          computer2
smss          computer1
snmp          computer2
spoolsv       computer2
spoolsv       computer1
(...)

Thursday, May 29, 2008

Select and then Sort

 

When you use both select-object and sort-object in a pipeline, what's the proper order? Let's check how fast they execute.
Each speed test is built from two similar commands with a different sort/select piping order and each test is executed 10 times. The total execution time is measured in Milliseconds.

 

- Updated: 06/11/2008 (see comment below by Lee Holmes) -

 

Test #1

PS > (measure-Command { 1..10 | foreach { gsv | sort name | select name,status }}).TotalMilliseconds

444.5451


PS > (measure-Command { 1..10 | foreach { gsv | select name,status | sort name}}).TotalMilliseconds

384.7565


Result: Second command is 15% faster.

 

Test #2  

# this command is the third example of select-object command from the help files.

PS > (measure-command { 1..10 | foreach { gps | sort ws | select -last 5 }}).TotalMilliseconds

346.0652


PS > (measure-command { 1..10 | foreach { gps | select -last 5 | sort WS }}).TotalMilliseconds

100.4444
    
Result
: Second command is 3.44 times faster.

# this command is the sixth example of sort-object command from the CTP help files. I changed the extension to ps1.

PS > (measure-Command { 1..10 | foreach { dir *.ps1 | sort @{Expression={$_.LastWriteTime-$_.CreationTime}; Ascending=$
false} | select LastWriteTime, CreationTime}}).TotalMilliseconds
640.9769
PS > (measure-Command { 1..10 | foreach { dir *.ps1 | select LastWriteTime, CreationTime | sort @{Expression={$_.LastWr
iteTime-$_.CreationTime}; Ascending=$false}}}).TotalMilliseconds



592.3512

 Result: Second command is 8.2% faster

 

Test #3  

PS > (measure-Command { 1..10 | foreach { dir | sort -unique | select name}}).TotalMilliseconds

6405.653


PS > (measure-Command { 1..10 | foreach { dir | select name | sort -unique}}).TotalMilliseconds

750.8251


Result: Second command is 753% faster!

 

The reason why 'select then sort' is faster to execute is because there are much less properties for sort-object to work on. When you select certain properties from a collection, select-object creates a new object with just the specified properties of the incoming object thus resulting in a smaller object to process.

One thing is for sure: In most cases, select objects before sorting them, and ALWAYS make sure they produce the SAME output!.

Wednesday, May 28, 2008

Generating Random Dates

 

One of the new cmdlets in PowerShell CTP2 is Get-Random. Get-Random gets a random number or selects objects randomly from a collection. 

I wanted to generate random DateTime objects with it but there is no built-in capability for that, yet it doesn't mean you can't use Get-Random it to generate random dates :)

At its base, a DateTime object is measured in 100-nanosecond units called Ticks. So I tried to use Get-Date to initialize a DateTime using Ticks. Again, Get-Date doesn't have any parameter for that so I checked the underlying object, System.DateTime.

One of the System.DateTime constructors allows you to initialize a new instance of the DateTime structure to a specified number of ticks. The maximum value for ticks is determined by:

PS > [datetime]::maxValue.ticks
3155378975999999999

 

Now, Can I use Get-Random to generate a number in the range of 0-3155378975999999999 (exclusive)? 
The help for Get-Random says: Without parameters or input, "Get-Random" returns a randomly selected 32-bit unsigned integer between 0 and Int32.MaxValue (0x7FFFFFFF, 2,147,483,647), so it shouldn't be a problem with the ticks input to generate a higher value then the max value of Int32.MaxValue:

PS > Get-Random ([datetime]::maxValue.ticks)
1.34540044929813E+18

PS > Get-Random ([datetime]::maxValue.ticks)
8.71609536968019E+17

Yes I can! So I tried to generate a new DateTime using that method:

 

PS > new-object dateTime (Get-Random ([datetime]::maxValue.ticks))
Friday, March 09, 3951 11:00:06 PM

PS > new-object dateTime (Get-Random ([datetime]::maxValue.ticks))
Wednesday, October 07, 2476 1:36:05 AM

 

Cool. But this can give a date between the Year 0 and 9999:

PS > new-object datetime ([datetime]::maxValue.ticks)

Friday, December 31, 9999 11:59:59 PM

 

If you want to have random DateTime between a specific period of time then you can use the Get-Random -min and -max parameters:

PS > $dateMin = get-date -year 1980 -month 1 -day 1
PS > $dateMax = get-date -year 2007 -month 1 -day 1

PS > new-object datetime (Get-Random -min $dateMin.ticks -max $dateMax.ticks) Thursday, November 14, 2002 9:45:23 AM

PS > new-object datetime (Get-Random -min $dateMin.ticks -max $dateMax.ticks)
Friday, December 07, 2001 6:17:54 PM

PS > new-object datetime (Get-Random -min $dateMin.ticks -max $dateMax.ticks)
Monday, April 01, 2002 4:50:52 PM

 

I didn't find any use for random dates yet, but it is good to know you can create them.

Friday, May 23, 2008

Fine Grain Password Policy Tool (cmdlet)


In Microsoft Windows 2000 and Windows Server 2003 Active Directory domains, only one password policy and account lockout policy could be applied to all users in the domain.
The Windows Server 2008 operating system provides a way to define different (fine-grained)password and account lockout policies and apply different restrictions to different sets of users in a domain.

Christoffer Andersson, Directory Services MVP, has released the second beta of FGPP - Fine Grain Password Policy Tool. The tool is available as MMC snap-in and a PowerShell cmdlet.

A quick start guide, download link and usage examples are available on Christoffer's blog.

Thursday, May 22, 2008

PowerShell Community Toolbar

 

The PowerShell Community Toolbar has passed 200 installations. Don't know what I'm talking about? Find out here.

Tuesday, May 20, 2008

Explore your mobile device with Windows PowerShell

 

Oisin Grehan [MVP] has released a beta version of his PowerShell provider for mobile devices: SmartPhone, PocketPC and Windows Mobile. Now you can start browsing your mobile like any other PowerShell drive:


ps c:\> add-pssnapin psmobile
ps c:\> cd mobile:
ps mobile:\> cd \temp
ps mobile:\temp> copy \windows\*.gif .
ps mobile:\temp> ii banner.gif
ps mobile:\temp> ii -local banner.gif
ps mobile:\temp> ii \windows\tasks.exe
ps mobile:\temp> convertfrom-wmfile *.gif c:\temp\

 

This is COOL!

Get all the details and download it at CodePlex.

Monday, May 19, 2008

PowerScripting Podcast - Episode 26

 

PowerScripting Podcast - Episode 26 is out, this time with the legendary Keith Hill, Windows PowerShell MVP. You can also listen to the podacst online via PowerShell Community Toolbar.

 


And for a limited time, Quest, the sponsor of the PowerScripting Podcast is offering Powerscripting Podcast listeners a free copy of Jeffery Hicks [MVP] upcoming book, 'Managing Active Directory with Windows PowerShell: TFM' from Sapien Press. Get the full details at the PowerScripting Podcast website.