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)
}
}

Advertisements