Feeds:
Posts
Comments

Archive for May, 2010

Blog has moved! You should check this post out on kevinpelgrims.com

A while ago I created a script to manage services on a remote server (see Remote Service Manager). Included in that script was a way to display some information with the use of a switch (-help). A few days later I was wondering if there wasn’t a better way to include help in own PowerShell scripts. And of course there is! It’s even in the help files.

So, here’s what to do. First, check the information in PowerShell help by running following command:

Get-Help about_Comment_Based_Help

There we can see this help thing will be pretty straightforward to implement. Here’s what it says:

The syntax for comment-based Help is as follows:

    # .< help keyword>
    # <help content>

-or -

    <#
        .< help keyword>
        < help content>
    #>

So, this means we just have to make a comment block, with some specific keywords. Cool. So, what are the right keywords?  They’re also in the help. Here’s a small list of the most important stuff:

  • .SYNOPSIS
  • .DESCRIPTION
  • .PARAMETER <Parameter-Name>
  • .EXAMPLE
  • .NOTES
  • .LINK

For more details and a complete list of keywords, I suggest you look at the help in PowerShell. Here is a little example of how we’re going to put this to work:

<#

.SYNOPSIS
This is a simple Powershell script to explain how to create help

.DESCRIPTION
The script itself will only print 'Hello World'. But that's cool. It's main objective is to show off the cool help thingy anyway.

.EXAMPLE
./HelloWorld.ps1

.NOTES
Put some notes here.

.LINK
https://kevinpelgrims.wordpress.com

#>

Write-Host ‘Hello World!’

There, that’s all there is to it. I wish I looked at that before I made the RSM script 🙂 It adds some kind of professionalism to a script, don’t you think? And also, it’s intuitive for users to use the Get-Help command. Everyone is happy! Below is a screenshot of our own help.

Read Full Post »

Blog is moving! You should check this post out on kevinpelgrims.com

This is the last part (of four) in a series of articles on the new features in PowerShell 2.0. Last time we discussed modules, now it’s time for remoting!

Remoting

One of the coolest new features is remoting. It is kind of like running remote desktop in command line. Altough it’s an important piece of PowerShell 2.0, there’s not a lot to explain about this. It’s all pretty easy to use.

What’s fun is that we have the option of executing one command on different remote computers at the same time. This will save system administrators a lot of time. To demonstrate this consider following command:

Invoke-Command -ComputerName Server1 -scriptblock {Get-Service}

This gets all services from Server1. To run this on multiple servers we just adjust the command:

Invoke-Command -ComputerName Server1, Server2, Server3 -scriptblock {Get-Service}

Now we get all services from Server1, Server2 and Server3 in a really easy way.

Another way to use remoting is by connecting to a remote session with Enter-PSSession. You type commands in your local console, but everything is executed on the remote machine. Until you leave the remote session, by using Exit-PSSession.

PS D:\ProjectsPS> Enter-PSSession -ComputerName Server1

PS Server1> Get-Process powershell

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
547       8    95660       8852   199     6,84   5136 powershell

PS Server1> Exit-PSSession
PS D:\ProjectsPS>

Of course, there are a lot of other commands available, so for the last time I will tell you to use the PowerShell 2.0 help: Get-Help about_PSSessions. Much can be learned from the official documentation (yes, it’s really good!).

And another good resource is of course Technet, where all the available cmdlets are explained in a more readable manner (see resources).

So that’s all for today folks! I hope you learned some things that will be useful in future projects.

Recources

A list of resources on the discussed topics:

Read Full Post »

Blog is moving! You should check this post out on kevinpelgrims.com

This is part three of four in a series of articles on the new features in PowerShell 2.0. Last time we discussed transactions, now it’s time for modules!

Modules

A module is a package that contains a bunch of PowerShell commands, like cmdlets, functions, variables, ..

This can be very useful when sharing your code with others, or deploying it to different servers. Think of a module as kind of an assembly for PowerShell.

Consider the next piece of code:

function Add {
  if(!(Test-Path variable:script:count)) { $script:count = 0 }
  $script:count++
  "We add one and the total becomes {0}!" -f $script:count
}

function Substract {
  if(!(Test-Path variable:script:count)) { $script:count = 0 }
  $script:count--
  "We substract one and the total becomes {0}!" -f $script:count
}

function SetCount($value) {
 $script:count = $value
}

# Export only the Add and Substract functions, not SetCount.
Export-ModuleMember Add, Substract
# End of Script

This is our module. Now what do we do with it? To deploy this module we just need to create a new folder in C:\Users\username\Documents\WindowsPowerShell\Modules\ and put our files in there. I’ll call this file Calculatron.psm1 and create the folder Calculatron at the Modules root.

All we need to do now is import the module by calling Import-Module and then we can use the new functions we created! They even become available in the tab command completion (you know what I mean, type “subs” and press the tab button).

Now you can use this awesome power to do some fun stuff and make your scripts easily reusable.

Recources

A list of resources on the discussed topics:

Read Full Post »