Powershell and SourceSafe

I posted yesterday on using Powershell to email a TFS user if they had files checked out. Well, we still run a legacy set of SourceSafe databases for old projects that are under maintenance, but not major redevelopment. (Our usual practice is to migrate projects to TFS at major release points).

Anyway, these SourceSafe repositories are just as likely, if not more so, to have files left checked out as TFS. The following script email a list of all checked out files in a SourceSafe DB

# to run this script without signing need to first run
#  Set-ExecutionPolicy  Unrestricted
# (the other option is to sign the script)
#  then run it using
#  .VSSstatus.ps1

function CheckOutVSSFileForUser(
    [string]$ssdir,
    [string]$to,
    [string]$from,
    [string]$server    )
{
    # get the open file list
    [char]10 + "Checking checked out file in " + $ssdir

    # set the environment variable without this you cannot access the DB
    $env:ssdir=$ssdir

    # we assume the logged in user has rights, as the -Yuid,pwd ss.exe
    # parameter does not work due to the ,
    # could used a named user that takes no password as another option
    # can use the -U option to limit the user listed
    $filelist = &"C:Program FilesMicrosoft Visual StudioVSSwin32ss.exe" status $/ -R

    # we have the results as an array of rows, so insert some line feeds
    foreach ($s in $filelist) { $emailbody = $emailbody + [char]10 + $s }
    # not the strange concatenation for the email to field, not a + as I suspected!
    $title = "File currently have checked out in " + $ssdir
    SendEmail $to  $from $server  $title  $emailbody
}

function SendEmail(
    [string]$to,
    [string]$from,
    [string]$server,
    [string]$title,
    [string]$body    )
{

    # send the email
    $SmtpClient = new-object system.net.mail.smtpClient
    $SmtpClient.host = $server
    $SMTPClient.Send($from,$to,$title,$body)
    "Email sent to " + $to
}

# the main body
$emailServer = "mail.domain.com"
$from = "vss@domain.com "
$to = "admin@domain.com "
$ssdir = ("\serversourcesafe1",
          "\serversourcesafe2",
          "\serversourcesafe3")

foreach ($s in $ssdir) {
    CheckOutVSSFileForUser $s $to $from $emailServer
}

Update 10-0ct-2008: Corey Furman has extended script this on his blog http://codeslabs.wordpress.com/2008/10/09/who-has-what-checked-out