Showing posts with label CTP2. Show all posts
Showing posts with label CTP2. Show all posts

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
(...)

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.

Sunday, May 11, 2008

PowerShell CTP2 on Server 2008

Updated: 05/14/2008 ( See original post below)

 

It won't work! (adding the command-line backup-tools when CTP(2) is installed) At the end of the process I checked the $host variable (thanks Marco) and it  showed that v1 is the current version. I jumped into conclusions too early and got fooled by PowerShell console title :(

Installing the backup-tools via serverManagerCmd (like the MMC) installs ALL backup dependencies (PowerShell v1 and all backup-features), it removes CTP without any confirmation. However, PowerShell CTP2 still appears under Control Panel > 'Programs and features' and the CTP2 program shortcuts are still available in the Programs menu but it points to v1 (installed by serverManager).

The confusion stems from the console title which shows: 'Windows PowerShell V2 (CTP2)' as it appears in the shortcut. I should have check the $host output or make sure that the CTP text logo appears in the console, CTP versions logo says:

Windows PowerShell V2 (Community Technology Preview - Features Subject to Change)
Copyright (C) 2007 Microsoft Corporation. All rights reserved.

So, my apologies for misleading you! I'm still researching the issue and hope to find a solution (if possible).

 

-Shay

 

 

 

Original Post:

There was a discussion at the PowerShell newsgroup regarding the inability to add the Backup-Tools feature when PowerShell CTP is installed on Server 2008. I was already installing Server 2008 on VM, so I decided to give it a try. I had no problems whatsoever and the installation was successfull. Here is a video capture of the process. The capture includes the following steps:

Server 2008 is already installed (default install)

1. Install PowerShell CTP2 on Server 2008.
2. Adding the Backup-Tools server component (registers the Windows.ServerBackup PowerShell snap-in)
3. Reboot the Server.
4. Loading the ServerBackup snap-in and listing its cmdlets.

 

 PowerShell CTP2 on Server 2008

PowerShell CTP2 on Server 2008