Testing website status

Like any applications, websites require a number of other components, such as the network and the operating system, in order to function. In the case of a problem you need to isolate the component causing the problem. One quick test is to see if the website is running. If you can confirm that it shows that the network and other components
are functioning.
PROBLEM
The phone rings. “The website isn’t available,” shouts the user. You need to check whether the website is up before performing other diagnostics.
SOLUTION
The following listing enables you to retrieve the website status,

$ws = DATA {
ConvertFrom-StringData -StringData @'
1 = Started
2 = Starting
3 = Stopped
4 = Stopping
5 = Unknown
'@
}
function get-websitestatus {
[CmdletBinding()]
param (
[parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[string]$computername="$env:COMPUTERNAME",
[string]$site

)
PROCESS{
if ($site) {
$sites = Get-WmiObject -Namespace 'root\webadministration' -Class Site -ComputerName $computername -Authentication 6 -Filter "Name='$site'"
}
else {
$sites = Get-WmiObject -Namespace 'root\webadministration' -Class Site -ComputerName $computername -Authentication 6
}
$data = @()

foreach ($site in $sites){
$wsdata = New-Object -TypeName PSobject
Add-Member -InputObject $wsdata -MemberType NoteProperty -Name "Name" -Value $site.Name

$state = $site.GetState()
Add-Member -InputObject $wsdata -MemberType NoteProperty -Name "Status" -Value $ws["$($state.ReturnValue)"]

$data += $wsdata
}
$data
}
}

Advertisements

Creating a website

You can create websites using the WMI provider, but there are some issues with performing this action due to Packet Privacy being enforced by the provider. This means you need to be a little bit sneaky in the way you approach this problem. I don’t recommend this approach for all use of WMI, but it’s a useful fallback technique if you can’t work directly with the WMI cmdlets.

 

function new-website {
[CmdletBinding()]
param (
[parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[string]$computer="$env:COMPUTERNAME",
[parameter(Mandatory=$true)]
[string]$site,
[parameter(Mandatory=$true)]
[string]$domain,
[parameter(Mandatory=$true)]
[string]$dirpath
)
PROCESS{
$conopt = New-Object System.Management.ConnectionOptions
$conopt.Authentication = [System.Management.AuthenticationLevel]::PacketPrivacy

$scope = New-Object System.Management.ManagementScope
$scope.Path = "\\$computer\root\WebAdministration"
$scope.Options = $conopt

$path = New-Object System.Management.ManagementPath
$path.ClassName = "Site"

$website = New-Object System.Management.ManagementClass($scope, $path, $null)

$path2 = New-Object System.Management.ManagementPath
$path2.ClassName = "BindingElement"

$bind = New-Object System.Management.ManagementClass($scope, $path2, $null)
$BInstance = $bind.CreateInstance()

$Binstance.BindingInformation = "*:80:$site.$domain"
$BInstance.Protocol = "http"

$website.Create($site, $Binstance, $dirpath, $true)
}
}