New-NotificationHub.ps1
function New-NotifcationHub { <# .SYNOPSIS This script can be used to provision a namespace and an notification hub. .DESCRIPTION This script can be used to provision a namespace and an notification hub. In particular, the script allows to specify the following parameters: -- Notification Hub Path -- Notification Hub RegistrationTtl -- Notification Hub UserMetadata -- Service Bus Namespace .PARAMETER Path Specifies the full path of the notification hub. .PARAMETER RegistrationTtl Specifies the time span of expiration time of all registrations in this notification hub. .PARAMETER UserMetadata Specifies the user metadata for the notification hub. .PARAMETER Namespace Specifies the name of the Service Bus namespace. #> [CmdletBinding(PositionalBinding=$True)] Param( [Parameter(Mandatory = $true)] [String]$Path, # required needs to be alphanumeric [ValidateNotNullorEmpty()] [TimeSpan]$RegistrationTtl = (new-timespan -days 90), # optional default to 90 [String]$UserMetadata = $null, # optional default to $null [ValidatePattern("^[a-z0-9]*$")] [alias("NamespaceName")] [String]$Namespace = (Get-Variable -Name $MyInvocation.MyCommand.Module.PrivateData.MODULEVAR -ValueOnly).DefaultNameSpace ) BEGIN { $datBegin = [datetime]::Now; [string] $fn = $MyInvocation.MyCommand.Name; $PSDefaultParameterValues.'Log-Debug:fn' = $fn; $PSDefaultParameterValues.'Log-Debug:fac' = 0; $PSDefaultParameterValues.'Log-Info:fn' = $fn; $PSDefaultParameterValues.'Log-Error:fn' = $fn; } # BEGIN PROCESS { # Default test variable for checking function response codes. [Boolean] $fReturn = $false; # Return values are always and only returned via OutputParameter. $OutputParameter = $null; try { # Parameter validation if(!$PSCmdlet.ShouldProcess(($PSBoundParameters | Out-String))) { throw($gotoSuccess); } # Set the output level to verbose and make the script stop on error $VerbosePreference = "Continue"; $ErrorActionPreference = "Stop"; # Create Service Bus namespace $CurrentNamespace = Get-SBNamespace -Name $Namespace; # Check if the namespace already exists or needs to be created if ($CurrentNamespace) { Log-Debug -msg "The namespace [$Namespace] already exists."; } else { Log-Debug -msg "The [$Namespace] namespace does not exist."; Log-Debug -msg "Creating the [$Namespace] namespace..."; New-SBNamespace -Name $Namespace; $CurrentNamespace = Get-SBNamespace -Name $Namespace; Log-Debug -msg "The [$Namespace] namespace has been successfully created."; } # Get namespace connections string $ConnectionString = Get-SBClientConfiguration -Namespaces $Namespace; Log-Debug -msg "ConnectionString of [$Namespace] namespace: [$ConnectionString]"; # Create the NamespaceManager object to create the notification hub Log-Debug -msg "Creating a NamespaceManager object for the [$Namespace] namespace..." $NamespaceManager = [Microsoft.ServiceBus.NamespaceManager]::CreateFromConnectionString($ConnectionString); Log-Debug -msg "NamespaceManager object for the [$Namespace] namespace has been successfully created."; # Check if the notification hub already exists if ($NamespaceManager.NotificationHubExists($Path)) { $msg = "The [$Path] notification hub already exists in the [$Namespace] namespace."; $e = New-CustomErrorRecord -m $msg -cat InvalidData -o $NamespaceManager; Log-Error -msg $msg; $PSCmdlet.ThrowTerminatingError($e); } else { Log-Debug -msg "Creating the [$Path] notification hub in the [$Namespace] namespace..."; $NotificationHubDescription = New-Object -TypeName Microsoft.ServiceBus.Notifications.NotificationHubDescription -ArgumentList $Path; $NotificationHubDescription.RegistrationTtl = $RegistrationTtl; $NotificationHubDescription.UserMetadata = $UserMetadata; $OutputParameter = $NamespaceManager.CreateNotificationHub($NotificationHubDescription); Log-Info -msg "The [$Path] notification hub in the [$Namespace] namespace has been successfully created."; } $fReturn = $true; } catch { if($gotoSuccess -eq $_.Exception.Message) { $fReturn = $true; } else { [string] $ErrorText = "catch [$($_.FullyQualifiedErrorId)]"; $ErrorText += (($_ | fl * -Force) | Out-String); $ErrorText += (($_.Exception | fl * -Force) | Out-String); $ErrorText += (Get-PSCallStack | Out-String); if($_.Exception -is [System.Net.WebException]) { Log-Critical $fn ("[WebException] Request FAILED with Status '{0}'. [{1}]." -f $_.Exception.Status, $_); Log-Debug $fn $ErrorText -fac 3; } else { Log-Error $fn $ErrorText -fac 3; if($gotoError -eq $_.Exception.Message) { Log-Error $fn $e.Exception.Message; $PSCmdlet.ThrowTerminatingError($e); } elseif($gotoFailure -ne $_.Exception.Message) { Write-Verbose ("$fn`n$ErrorText"); } else { # N/A } } $fReturn = $false; $OutputParameter = $null; } } finally { # Clean up # N/A } } # PROCESS END { $datEnd = [datetime]::Now; Log-Debug -fn $fn -msg ("RET. fReturn: [{0}]. Execution time: [{1}]ms. Started: [{2}]." -f $fReturn, ($datEnd - $datBegin).TotalMilliseconds, $datBegin.ToString('yyyy-MM-dd HH:mm:ss.fffzzz')) -fac 2; # Return values are always and only returned via OutputParameter. return $OutputParameter; } # END } # function if($MyInvocation.ScriptName) { Export-ModuleMember -function New-NotifcationHub; } # SIG # Begin signature block # MIIXDwYJKoZIhvcNAQcCoIIXADCCFvwCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB # gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR # AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUCHG74E9yckGDGi+Yan7QAS9z # /3SgghHCMIIEFDCCAvygAwIBAgILBAAAAAABL07hUtcwDQYJKoZIhvcNAQEFBQAw # VzELMAkGA1UEBhMCQkUxGTAXBgNVBAoTEEdsb2JhbFNpZ24gbnYtc2ExEDAOBgNV # BAsTB1Jvb3QgQ0ExGzAZBgNVBAMTEkdsb2JhbFNpZ24gUm9vdCBDQTAeFw0xMTA0 # MTMxMDAwMDBaFw0yODAxMjgxMjAwMDBaMFIxCzAJBgNVBAYTAkJFMRkwFwYDVQQK # ExBHbG9iYWxTaWduIG52LXNhMSgwJgYDVQQDEx9HbG9iYWxTaWduIFRpbWVzdGFt # cGluZyBDQSAtIEcyMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlO9l # +LVXn6BTDTQG6wkft0cYasvwW+T/J6U00feJGr+esc0SQW5m1IGghYtkWkYvmaCN # d7HivFzdItdqZ9C76Mp03otPDbBS5ZBb60cO8eefnAuQZT4XljBFcm05oRc2yrmg # jBtPCBn2gTGtYRakYua0QJ7D/PuV9vu1LpWBmODvxevYAll4d/eq41JrUJEpxfz3 #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