Showing posts with label CTP. Show all posts
Showing posts with label CTP. Show all posts

Saturday, May 3, 2008

Windows PowerShell V2 - Not a beta!

 

At last, It's out! The Windows PowerShell Team has released the second Community Technology Preview (CTP2) of Windows PowerShell V2. Read the announcement.

To see what's new in this release download the attached slides of this post MMS: What's Coming In PowerShell V2.

 

Download Windows PowerShell V2 CTP2

Monday, February 11, 2008

Tokenizing PowerShell scripts

 

In Windows PowerShell Virtual User Group Meeting #3, Lee Holmes presented a script that can do Syntax Highlighting in PowerShell in PowerShell v2 (CTP).

Reading through Lee's script, I remembered an older post where I was trying to take a script and resolve any aliases it contained to its full cmdlet name. Back then, Richard Siddaway commented that there is one major drawback. If you used the "%" sign as an alias for the Foreach-Object cmdlet then things may go wrong and behave unexpectedly, since "%" is also the symbol for modulo.

Well, not in the case of Tokenize. The Tokenize method takes the script content and breaks it apart into the script ingredients (PSTokenType). In the case of modulo, it knows when it's used as an alias and when it's used as an operator, amazing stuff!

 

To get a full list of all token types: 

PS > Get-EnumValues System.Management.Automation.PSTokenType

              Name Value
              ---- -----
           Unknown     0
           Command     1
  CommandParameter     2
   CommandArgument     3
            Number     4
            String     5
          Variable     6
            Member     7
         LoopLabel     8
         Attribute     9
              Type    10
          Operator    11
        GroupStart    12
          GroupEnd    13
           Keyword    14
           Comment    15
StatementSeparator    16
           NewLine    17
  LineContinuation    18
          Position    19

 

So, here we go... a sample script that contains aliases as well as the modulo sign:  

### demo.ps1 ###
dir | ? { ($_.length % 2) -eq 0 } | % { $_.name }

And here's a modified Convert-AliasToCmdlet script:
### Convert-AliasToCmdlet ###

param($file)

function Get-TokenType($token){
    switch($token.type){
        "Variable" {'${0}' -f $token.content}
        "Type" {"[{0}]" -f $token.content}
        "Command" { 
                $alias = (get-alias | where {$_.name -eq $token.content}).ResolvedCommandName
                if($alias) {$alias} else {$token.content}
        }
        default {$token.content}    
    }
}

$column=1
$content = [IO.File]::ReadAllText($file)
$tokens = [System.Management.Automation.PsParser]::Tokenize($content, [ref]$null)
$tokens | foreach {
    $padding=(" " * ($_.StartColumn - $column))
    $column=$_.EndColumn
    write-host ($padding + (Get-TokenType $_)) -NoNewline
}

write-host

 

The result shows that all aliases including the modulo sign were resolved as expected and each by its own usage context.

PS > Convert-AliasToCmdlet demo.ps1

Get-ChildItem | Where-Object {
 ($_.length % 2) -eq 0
} | ForEach-Object {
 $_.name
}

 

One thing I wasn't able to find is how to preserve TAB characters.

Thursday, January 3, 2008

New PowerShell TechNet event


The two sessions event will be delivered by Richard Siddaway.

Windows Powershell will ship as part all future major Microsoft products including Windows 2008. These two sessions will show you how to manage Windows Server 2008 and your Windows environment with Windows PowerShell as well as covering some of the features of PowerShell V2. Starting with an overview of Windows PowerShell and WMI we will show how local and remote systems can be managed by WMI and the new remoting features in Windows PowerShell V2. We will look at some of the new features in Windows Server 2008, including Server Core, Active Directory and IIS 7, and show how Windows PowerShell is the ideal management tool for automating your administrative experience.


REGISTER NOW

Sunday, December 9, 2007

Quotation Marks in Remote Commands

 

69178_scrptctrr_hero-left

This is a must read article (tipped by Jon, PowerShell NG) for PowerShell 2.0 CTP users. June Blender, Windows PowerShell Documentation Team, untangles the quotation marks spaghetti and provides very important rules of thumb for writing efficient remote commands with Invoke-Expression.

Read it on TechNet Script Center.

Wednesday, December 5, 2007

PowerShell remoting on XP and IIS

 

In order to use remoting in PowerShell CTP, all machines should have WinRM (KB936059) and PowerShell CTP installed. After the installation you should run the "Configure-Wsman.ps1" script (resides in the PowerShell home directory). The scripts configures WSMan to enable PowerShell remoting.

 

PS > & $pshome\Configure-Wsman.ps1

 

After the installation, the winrm service is not running (stopped state) so you should start it manually.

 

PS > get-service winrm

Status   Name               DisplayName
------   ----                     -----------
Stopped WinRM             Windows Remote Management (WS-Manag...

 

PS > get-service winrm | start-service

 

To make it load automatically when Windows starts:

PS > Set-Service winrm -StartupType automatic

 

Then I tried to run a command :

PS > Invoke-Expression -ComputerName  computer -Command Get-Process

 

It returned an error

Invoke-Expression : [computer] The WS-Management service does not support the
request.
At line:1 char:18
+ Invoke-Expression <<<< -ComputerName spare -command "get-process"

 

I posted a question in the PowerShell newsgroup and eventually the problem found with help from Marco Shaw.

If you have IIs installed on the machines your installed PowerShell CTP then there is a collision between WinRM port and IIs port.

To solve it temporary, stop the w3svc service and restart winrm service, or try to change the port setings for winrm.

There should be a public KB article from Microsoft on this issue somewhere soon.