Archive

Posts Tagged ‘PowerShell’

Bulk Creating Active Directory groups

One of my colleagues gave me a list of AD groups for the sharepoint environment he’s confguring. Instead of creating them manually I used the Quest ActiveRoles management shell.

import-csv ‘SecurityGroups.csv’ | foreach {
New-QADGroup -Parent $_.ParentContainer -Name $_.Name -sAM $_.Name -GroupScope $_.GroupScope -GroupType $_.GroupType
}

This script imports a .csv file to create the security groups. The .csv file contains the OU distinguished name, the group Name, the group type (security or distribution) and finally the scope (Domain Local, Global or Universal).

It looks like this:

Parentcontainer,Name,Grouptype,GroupScope
“OU=Sharepoint,DC=domain,DC=local”,SecurityGroup01,Security,DomainLocal

Automating a powershell demo using AutoHotKey

I gave a custom Exchange 2007 course to one of our customers yesterday. Not unsurprisingly, it also included a demo of Exchange Management Shell (EMS). These demos tend to end up in a quest for the exact command, which is quite annoying if you’re in front of an audience (even a small one). Think about:

  • about_regular_expressions or about_regular_expression?
  • $_. or $._?
  • Why is -Confirm $False not working?

This time I decided to try and automate that demo by using the autoreplace feature (Hotstring) in AutoHotKey. The main goal is avoiding typo’s by using tried and true commands in a working order. A poor man’s Start-Demo script if you wish ;-)

The script starts of with a bunch of commands controlling the overall functionallity (the first 3 come with the default template):

#NoEnv

Recommended for performance and compatibility with future AutoHotkey releases.

SendMode Input

Recommended for new scripts due to its superior speed and reliability.

SetWorkingDir %A_ScriptDir%

Ensures a consistent starting directory.

#Hotstring EndChars `n

Limit the hotstring end characters to ENTER only

#Hotstring O

Don’t actually type the end character. (I want to launch the command myself after/during my explanation.)

#Hotstring R

Type replacement text in raw mode. (To prevent AHK from interpreting e.g. “{“)

The remainder of the script is actually a long list of hotstring definitions like these ones

::ems01::Get-Help
::ems02::Get-Help About
::ems03::Get-Help About_Regular_Expression
::ems04::Get-Command *mailbox
::ems05::Get-Help New-Mailbox
::ems06::Get-Help New-Mailbox -Detailed
::ems07::Get-Help New-Mailbox -Full

and so on.

Seems stupid with these simple examples, but the advantage becomes clear for more complex commands. Lets take “ems26″ for example;

::ems26::1..10 | Foreach-Object { new-mailbox -Database “Mailbox Database 2″ -Name (“User” + $_) -UserPrincipalName (“User” + $_ + “@exchange2007.lab”) -Password $Pwd }

When I type “ems26″+ENTER in EMS AutoHotKey executable automatically replaces this string with “1..10 | Foreach-Object { new-mailbox -Database “Mailbox Database 2″ -Name (“User” + $_) -UserPrincipalName (“User” + $_ + “@exchange2007.lab”) -Password $Pwd }” and waits. I do my talking and then hit ENTER again to execute the command.

It worked nicely actually. People were impressed with my typing skills ;-) I only encountered one error because I didn’t delete some manually after a test the day before. It even works with shell running inside a VM while the AutoHotKey script is running on my physical computer.

Categories: Scripting Tags: ,

Hooking up SQLite and PowerShell

I’ve been playing around with SQLite on the command line (cmd that is) a bit. I immediately started wondering if it would be possible to combine this portable SQL engine with PowerShell, but I couldn’t find any working samples on the internet.

After some more research I discovered the SQLite ADO.net 2.0 Provider but I couldn’t get it to work immediately.

It took me a couple of days to find the blog post Database Queries with Windows Powershell. (This blog seems to have moved recently as most of the hyperlinks/search results are still referring to Typepad). The post is nicely commented, so I could rather easily translate the MySQL to SQLite statements with some assistance from the included help file.

[void][System.Reflection.Assembly]::LoadFrom("D:\DATA\Tools\System.Data.SQLite.dll")

$cn = New-Object -TypeName System.Data.SQLite.SQLiteConnection
$cn.ConnectionString = "Data Source=D:\DATA\Tools\koen.sqlite"
$cn.Open()

$cm = New-Object -TypeName System.Data.SQLite.SQLiteCommand
$sql = "SELECT FirstName, LastName FROM tblKoen"
$cm.Connection = $cn
$cm.CommandText = $sql
$dr = $cm.ExecuteReader()

while ($dr.Read())
{
    write-host $dr.GetString(0) " " $dr.GetString(1)
}

$cn.Close()

Some more interesting links I came across:

Categories: Scripting Tags: ,

VI Toolkit scripting contest winners announced.

Carter Shanklin announced the winners of the contest.

1st prize: The guest provisioning system of LucD. This application lets you provision your VMs in an easy way. You can set the options from one screen no clicking around to configure everything manually.

2nd prize: VI Power Documenter by tzamora. This script generates reports about your virtual infrastructure.

3rd prize: PowerVDI script by Dan Baskette. It let you deploy multiple VDI instances on an EMC celera using snapshots and AD integration.

I’m sure there are other contributions that are very useful.

Congrats to the winners. Nice job guys.

Categories: Scripting, Virtualization Tags:

PowerGui integrates with VI Client

Dmitri over at Dmitry’s PowerBlog just posted about the integration between PowerGui and the VI client. I had already installed the PowerGUI 1.5.1 version but hadn’t noticed the plugin for the VI Client.

This makes managing your VMware environment using  Powershell even more easier. PowerGUI lets you write your own Powershell scripts and the VMware PowerPack is also compatible with the VI client plugin. The plugin uses your current connection to the VC. You don’t need to make a separate connection for every script.

VI Toolkit 1.0 Released

Last week VMware officially released the VI Toolkit for Windows.

  • They replaced get-viserver with connect-viserver. get-viserver is now an alias for connect-viserver.
  • disconnect-viserver was added which allows you to do a clean disconnect. Hence the rename of the get-viserver cmdlet.
  • VMware also added cmdlets for managing the VMware Update Manager.
Categories: Scripting Tags: ,

Pimp My PowerShell

Jeffrey Snover posted a solution for speeding up the PowerShell start-up long time ago. I only noticed it lasted week :-(

Check it out: http://blogs.msdn.com/powershell/archive/2007/11/08/update-gac-ps1.aspx

Categories: Scripting Tags:

Powershell auto-completion for Notepad++

I’ve been working on a PowerShell auto-completion file for my favorite lightweight text-editor (Notepad++) as I couldn’t find one online: powershell.xml

Procedure:

  1. Install the PowerShell Language Definitions for Notepad++ (for syntax highlighting) and make sure it’s working (this is a prerequisite).
  2. Save the the file above in “C:\Program Files\Notepad++\plugins\APIs”
  3. Make sure the language is set to powershell in your document.

npp language selection

Note: You might want to have a look at the Notepad++ Auto-completion HOWTO for more details about this feature.

The auto-complete features of npp are not the most powerful though. It seems to work perfectly if you hit CTRL+SPACE before the dash. If you try after the dash it seems to forget about the verb in a PS-cmdlet. Even the builtin languages like LISP sport the same behaviour. The underscore characters in the WMI-class names and the “about” pages do not pose a problem.

*Update 2009-01-29 Npp has switched to the xml format for the autocompletion files. I’ve uploaded an xml version of the previous file to some other webspace (= no renaming anymore) and update the post accordingly.

Categories: Software Tags: ,

ListTopProcesses.ps1

After having received a splendid presentation on powershell from a couple of my colleagues (Rastix, Virtix and others…) it was time to write my first script myselves. After fiddling around a couple of hours I came up with a script to list a top 10 of memory consuming processes on a remote computer:

function PrintMegs
{
[Math]::Truncate($args[0]/1MB)
}

get-wmiobject -class win32_process -computername $args[0] | Sort-Object vm -desc | Select-Object -first 10 | Format-Table ProcessName,@{Label=”VM (MB)”;Expression={PrintMegs($_.VM)}},@{Label=”WS (MB)”;Expression={PrintMegs($_.WS)}},Handles,ProcessID,Path -autosize

The script takes one input parameter; the name of the computer, it does work with “localhost” also though.

The hardest part was figuring out how to print the memroy details in MB, with no decimal places and in table format ;-)

Categories: Scripting Tags: