public/http/urlacl/Add-UrlAcl.ps1
#Requires -RunAsAdministrator function Add-UrlAcl { <# .SYNOPSIS Adds a new URL ACL entry using netsh http add urlacl .DESCRIPTION Adds a new URL ACL entry using netsh http add urlacl. .EXAMPLE PS C:\> Add-UrlAcl -Url http://+:80/MyUri -User DOMAIN\user Creates a URL reservation on all interfaces for DOMAIN\user .OUTPUTS Outputs a [UrlAcl] object when the -PassThru parameter switch is present #> [CmdletBinding(SupportsShouldProcess)] [OutputType([UrlAcl])] param( # Specifies the URL to reserve [Parameter(Mandatory, ValueFromPipelineByPropertyName)] [ValidateNotNullOrEmpty()] [string] $Url, # Specifies the Windows user for which the reservation applies [Parameter(Mandatory, ParameterSetName = 'AddByUserName')] [ValidateNotNullOrEmpty()] [string] $User, # Specifies whether to allow the user to register URLs [Parameter(ParameterSetName = 'AddByUserName')] [ValidateSet('yes', 'no')] [string] $Listen, # Specifies whether to allow the user to delegate URLs [Parameter(ParameterSetName = 'AddByUserName')] [ValidateSet('yes', 'no')] [string] $Delegate, # Specifies the SSDL string that describes the DACL [Parameter(Mandatory, ParameterSetName = 'AddBySddl')] [string] $Sddl, # Pass the newly added UrlAcl object into the pipeline [Parameter()] [switch] $PassThru ) process { $command = "netsh.exe http add urlacl url=$Url" switch ($PSCmdlet.ParameterSetName) { 'AddByUserName' { $command += " user=$User" if (![string]::IsNullOrWhiteSpace($Listen)) { $command += " listen=$Listen" } if (![string]::IsNullOrWhiteSpace($Delegate)) { $command += " delegate=$Delegate" } } 'AddBySddl' { $command += " sddl=$Sddl" } Default {} } Write-Verbose "Executing the command '$command'" if ($PSCmdlet.ShouldProcess((hostname), $command)) { $output = Invoke-Expression -Command $command $success = $LASTEXITCODE -eq 0 $output = [string]::Join("`r`n", $output).Trim() if ($success) { Write-Information $output if ($PassThru) { Get-UrlAcl -Url $Url } } else { Write-Error "Error: $output" } } } } |