Monday, March 22, 2010

Authentication for Coded VSTS Web Tests

To set credentials for a Visual Studio WebTest programatically, simply add the following to the class that extends WebTest:

this.UserName = "domain\\user"
this.Password = "pass"

Sunday, March 21, 2010

Easily run commands on many machines with powershell

Need to run a series of commands on a bunch of machines in a row? Try something like this in PowerShell:

01,02,03,04,05,08,09,10,19,20,21 |
%{"base10name12{0:D2}" -f $_} | %{
Enable-WSManCredSSP client $_
invoke-command -comp $_ { cp D:\logs\* \\server\share\logs }
restart-computer $_
}

Deleting SharePoint lists with PowerShell

This web-scrapes the "All Site Content" page of a SharePoint site, picking out the Document Libraries and Lists that are not part of the default setup. It then deletes them (actually sends them to the recycle bin).

$cred = New-Object System.Management.Automation.PSCredential "domain\user",("pass" | ConvertTo-SecureString -AsPlainText -Force)

$client = new-object system.net.webclient
$client.Credentials = $cred

$page = $client.DownloadString("http://server/_layouts/viewlsts.aspx")
$matches = [regex]::Matches($page, "AllItems.aspx`">([^<]+?)<")

$toDel = $Matches | %{$_.groups[1].value} | ?{
$_ -ne "Customized Reports" -and
$_ -ne "Shared Documents" -and
$_ -ne "Site Assets" -and
$_ -ne "Style Library" -and
$_ -ne "Announcements" -and
$_ -ne "Links" -and
$_ -ne "Tasks" -and
$_ -ne "Team Discussion"}

$toDel | %{
$req = [System.Net.HttpWebRequest]::Create("http://server/$_")
#$req.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$req.Credentials = $cred
$req.Method = "DELETE"
$res = $req.GetResponse()

sleep 5 # give it a chance to catch its breath. otherwise, you may have to run the script several times
}


Emptying the recycle bin is left as an exercise to the reader (or me, when I have more time)

Saturday, March 20, 2010

If SharePoint Service Instances are "Provisioning" forever...

Note to self: Make sure the SPTimer service is running on all machines in your SharePoint farm. If things seem "paused" (ie: service instances are "provisioning" forever), this may be the culprit.

Saturday, March 6, 2010

Faster get-content with ReadCount

By default, get-content reads one line at a time, presumably so you can see file content before PowerShell has finished reading the file. When you want to process large files (ie: logs) and don't want to see the raw contents, use the ReadCount parameter to speed things up. To read x lines at a time, set -ReadCount x. To read the whole file at once, set -ReadCount 0.

cat -ReadCount 0 $file

Note that this returns an array instead of a single line. The easiest way around this is to feed the resulting array down the pipeline one line at a time (still faster):

cat -ReadCount 0 $file | %{$_}

More detail here.

Control lala website from powershell

I recently discovered the lala web music service. I like the idea of having a music collection in the cloud, but I also like having shortcut keys to play/pause, skip, etc, while I'm in another app. PowerShell can control Internet Explorer, so I can do this by calling the following script from AutoHotKey (or the like):

# usage: lala.ps1 -PlayPause -Status
param([switch]$PlayPause, [switch]$Previous, [switch]$Next, [switch]$Show, [switch]$Hide, [switch]$Status)
$app = New-Object -ComObject shell.application
$ie = $app.Windows() | ?{ $_.LocationURL -match "www.lala.com" }
$doc = $ie.Document

if ($Show) { $ie.Visible = $true }
if ($Hide) { $ie.Visible = $false }

if ($Previous) { $doc.getElementById("headerPrevButton").Click() }
if ($PlayPause) { $doc.getElementById("headerPauseButton").Click() }
if ($Next) { $doc.getElementById("headerNextButton").Click() }

if ($Status) {
$doc.getElementById("headerTrackTitle").InnerText
$doc.getElementById("headerTrackArtist").InnerText
}


This version expects a lala window to already exist and have music queued up. Future versions will handle this more robustly.