Thursday, January 25, 2007

Is my file fragmented?

Small function to determine if a file is fragmented - for instance to analyzed an .ldf/.mdf file.
The function below also demonstrates one way to invoke an external command/tool (in this case "contig.exe" from www.sysinternals.com).
The function requires contig.exe to be in the local path.

function getFileFragments {
    $filename = $args[0]
    $contigOutput = contig -a $filename
    foreach ($line in $contigOutput) {
        if ($line -match "Average fragmentation") {
            $splitline = $line.split(' ')
            $x = $splitline.count
            Write-Host $splitline[$x -2]
        }
    }
}

Usage:

getFileFragments "c:\boot.ini"

Tuesday, January 02, 2007

Test connection to server

Small function to test connectivity to a server.

function test-connection {
    $pingtest = ping $args[0] -n 1
        if ($pingtest -match 'TTL=') {
        Write-Output $true
    } else {
        Write-host $false
    }
}

Usage:

test-connection MyServer01.domain.com