public/http/urlacl/Get-UrlAcl.ps1
function Get-UrlAcl { <# .SYNOPSIS Gets one or many urlacl entries using netsh http show urlacl .DESCRIPTION Gets one or many urlacl entries using netsh http show urlacl and parses them into a collection of [UrlAcl] objects for easier access to properties and their values. .EXAMPLE PS C:\> Get-UrlAcl Gets a collection of all entries returned by netsh http show urlacl .EXAMPLE PS C:\> Get-UrlAcl -Url http://+:443/ Gets a single urlacl entry matching the given URL or results in an error if the urlacl is not found .EXAMPLE PS C:\> $user = whoami; Get-UrlAcl | Where-Object { $_.Users.ContainsKey($user) } Gets all URL reservations where the current user is included in the list of users under that urlacl reservation .OUTPUTS Results are provided as [UrlAcl] objects with a Url [string] property and a Users [hashtable] property #> [CmdletBinding(DefaultParameterSetName='NoFilter')] [OutputType([UrlAcl])] param ( # Specifies the url to filter on. Example: http://+:80/MyUri, http://www.contoso.com:80/MyUri # Note: Wildcards are not supported [Parameter(ParameterSetName='UrlFilter')] [string] $Url ) process { $command = "netsh.exe http show urlacl" switch ($PSCmdlet.ParameterSetName) { 'UrlFilter' { $command += " url=$Url" } Default {} } Write-Verbose "Executing the command '$command'" $output = Invoke-Expression -Command $command $success = $LASTEXITCODE -eq 0 if ($success) { $reservationCount = 0 foreach ($row in $output) { if ([string]::IsNullOrWhiteSpace($row)) { continue } elseif ($row.StartsWith('URL Reservations:')) { continue } elseif ($row.StartsWith('-')) { continue } elseif ($row -like '*Error:*') { Write-Error "Netsh returned error '$row' on url '$($urlacl.Url)'" continue } $line = $row.Trim() if ($line.StartsWith('Reserved URL')) { if ($null -ne $urlacl) { Write-Verbose "Completed urlacl entry property collection for url $($urlacl.Url)" Write-Output $urlacl $reservationCount += 1 } Write-Verbose "Starting new urlacl entry property collection" $url = $line -split '\s+:\s+' | Select-Object -Skip 1 -First 1 $urlacl = [UrlAcl]::new($url) continue } $key, $value = $line -split '(?!\w+):\s+' if ($null -eq $value) { $value = [string]::Empty } if ($key -eq 'User') { Write-Verbose "Adding user '$value' to urlacl $($urlacl.Url)" $user = $value $urlacl.Users.$user = @{} continue } $urlacl.Users.$user.$key = $value } Write-Verbose "Completed urlacl entry property collection for url $($urlacl.Url)" if ($null -ne $urlacl) { Write-Output $urlacl $reservationCount += 1 } if (![string]::IsNullOrWhiteSpace($Url) -and $reservationCount -eq 0) { Write-Error "No urlacl entry found matching url '$Url'" } } else { $output = [string]::Join("`r`n", $output).Trim() Write-Error "Error: $output" } } } |