Monday, June 04, 2018

Export IIS bindings

I find the cmdlet for exporting cmdlets a bit lacking so I built my own export function for fun and profit.
The function in the toolsfile collects all info and return a populated object that can be used to export list to file or do something else with.
$scriptPath = $(split-path -Parent $MyInvocation.MyCommand.Definition)
import-module "$($scriptPath)\xssWebTools.psm1" -Force
$Site1Headers = get-SiteBindings -siteName "site1"
$Site2Headers = get-SiteBindings -siteName "site2"
if ($Site1Headers -ne $null) {
$Site1Headers |export-csv -Path "$($scriptPath)\site1Addresses.csv" -Encoding UTF8 -NoTypeInformation
write-output "Printed Site1Headers to $($scriptPath)"
} else {write-output "No Site1Headers found"}
if ($Site2Headers -ne $null) {
$Site2Headers |export-csv -Path "$($scriptPath)\site2Addresses.csv" -Encoding UTF8 -NoTypeInformation
write-output "Printed Site2Headers to $($scriptPath)"
} else { write-output "No Site2Headers found"}
function get-SiteBindings {
param(
[parameter(mandatory=$true)]
[ValidateNotNullOrEmpty()]
$siteName
)
if (!(Get-Module -Name webadministration)) {import-module webadministration}
$selectedSite = Get-ChildItem -Path IIS:\Sites |where-object {$_.name -like $siteName}
if ($selectedSite.count -eq 1) {
$currentHostHeaders = Get-ItemProperty -Path $selectedSite.pspath -Name bindings.collection
$object = @()
foreach ($currentHostHeader in $currentHostHeaders) {
#$TrimmedHeader = $currentHostHeader.bindingInformation.TrimStart("*").trimstart(":80")
$TrimmedHeader = $currentHostHeader.bindingInformation.split(":")[-1]
$portHeader = $currentHostHeader.bindingInformation.split(":")[-2]
$tempObject = new-object psobject -Property @{hostheader=$TrimmedHeader;protocol=$currentHostHeader.protocol;port=$portHeader}
$object += $tempObject
}
return $object
}
elseif ($selectedSite.count -gt 1) {
write-warning "Too many hits. Skipping..."
return $null
}
elseif ($selectedSite.count -eq 0) {
write-warning "Couldn't find any sites matching '$siteName'!"
return $null
}
}

No comments:

Powershell and Uptimerobot

Uptimerobot can be quite tedious when you need to update many monitors at once. For example say you bought the license for Uptimerobot and n...