cEPRSAzureFeatures.psm1
enum Ensure { Absent Present } <# This resource manages a Database in a specific Subscription(Creation and deletion). [DscResource()] indicates the class is a DSC resource #> [DscResource()] class cEPRSCreateAzureDatabase { [DscProperty(Mandatory)] [string]$AdministratorLogin #Administrator Login [DscProperty(Mandatory)] [string]$AdministratorPassword #Administrator Password [DscProperty(Key)] [string]$SubscriptionName #Name of the subscription [DscProperty(Mandatory)] [string]$ServerName #Name of the azure SQL Database Server [DscProperty(Mandatory)] [string]$DatabaseName #Name of the azure SQL Database Server [DscProperty(Mandatory)] [string]$PublishSettingsFile #Path to publish Settings File [DscProperty(Mandatory=$false)] [ValidateSet("Basic","Business","None","Premium","Standard", "Web")] [string]$DatabaseEdition #Database Edition [DscProperty(Mandatory)] [ValidateSet("Absent","Present")][Ensure]$Ensure #Ensure value(Give Present for install and Absent for uninstall) [cEPRSCreateAzureDatabase] Get() { Import-AzurePublishSettingsFile "$($this.PublishSettingsFile)" Select-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" Set-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" $Database = Get-AzureSqlDatabase -ServerName $this.ServerName -DatabaseName $this.DatabaseName -ErrorAction Ignore if($Database -ne $null) { $this.DatabaseName = $Database.Name $this.Ensure = [Ensure]::Present } else { $this.Ensure = [Ensure]::Absent } return $this } [void] Set() { Import-AzurePublishSettingsFile "$($this.PublishSettingsFile)" Select-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" Set-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" if($this.Ensure -eq [Ensure]::Present) { $servercredential = new-object System.Management.Automation.PSCredential("$($this.AdministratorLogin)", ("$($this.AdministratorPassword)" | ConvertTo-SecureString -asPlainText -Force)) $ctx = New-AzureSqlDatabaseServerContext -ServerName “$($this.ServerName)” –Credential $servercredential if($this.DatabaseEdition -ne $null) { $NewDatabase = New-AzureSqlDatabase -ConnectionContext $ctx -DatabaseName $this.DatabaseName -Edition $this.DatabaseEdition } else { $NewDatabase = New-AzureSqlDatabase -ConnectionContext $ctx -DatabaseName $this.DatabaseName } Write-Verbose "Creating new Azure Sql Database $($this.DatabaseName) in server $($this.ServerName)..." Write-Verbose "Database Name: $($NewDatabase.Name) Collation Name: $($NewDatabase.CollationName) Edition: $($NewDatabase.Edition) MaxSizeGB: $($NewDatabase.MaxSizeGB) MaxSizeBytes: $($NewDatabase.MaxSizeBytes) ServiceObjectiveName: $($NewDatabase.ServiceObjectiveName)" } else { Write-Verbose "Ensure Set to Absent... Deleting the Azure SQL Database $($this.DatabaseName) from the server $($this.ServerName) from subscription $($this.SubscriptionName)" $Remove = Remove-AzureSqlDatabase -ServerName $this.ServerName -DatabaseName $this.DatabaseName -Force } } [bool] Test() { $present=$true Import-AzurePublishSettingsFile "$($this.PublishSettingsFile)" Select-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" Set-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" $Database = Get-AzureSqlDatabase -ServerName $this.ServerName -DatabaseName $this.DatabaseName -ErrorAction Ignore if($this.Ensure -eq [Ensure]::Present) { if($Database -ne $null) { $present = $true } else { $present = $false } } else { if($Database -ne $null) { $present = $false } else { $present = $true } } return $present } } <# This resource manages a AzureDatabase Server in a specific Subscription(Creation and deletion). [DscResource()] indicates the class is a DSC resource #> [DscResource()] class cEPRSCreateAzureDBServer { [DscProperty(Mandatory=$false)] [string]$AdministratorLogin #Administrator Login [DscProperty(Mandatory=$false)] [string]$AdministratorPassword #Administrator Password [DscProperty(Mandatory=$false)] [ValidateSet("East US","West US","Central US","South Central US","East US 2","North Europe","West Europe","Southeast Asia","East Asia")] [string]$ServerLocation #Location of SQL Server [DscProperty(Key)] [string]$SubscriptionName #Name of the subscription [DscProperty(Mandatory=$false)] [ValidateSet("2.0","12.0")] [string]$Version #Azure SQL Database server version [DscProperty(Mandatory=$false)] [string]$ServerName #Name of the azure SQL Database Server [DscProperty(Mandatory)] [string]$PublishSettingsFile #Path to publish Settings File [DscProperty(Mandatory)] [ValidateSet("Absent","Present")][Ensure]$Ensure #Ensure value(Give Present for install and Absent for uninstall) [cEPRSCreateAzureDBServer] Get() { Import-AzurePublishSettingsFile "$($this.PublishSettingsFile)" Select-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" Set-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" $server = Get-AzureSqlDatabaseServer -ServerName $this.ServerName -ErrorAction Ignore if($server -ne $null) { $this.Ensure = [Ensure]::Present $this.ServerName = $server.ServerName $this.Version = $server.Version $this.ServerLocation = $server.Location } else { $this.Ensure = [Ensure]::Absent } return $this } [void] Set() { Import-AzurePublishSettingsFile "$($this.PublishSettingsFile)" Select-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" Set-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" if($this.Ensure -eq [Ensure]::Present) { Write-Verbose "Creating new AzureSqlServer..." $NewServer = New-AzureSqlDatabaseServer -AdministratorLogin $this.AdministratorLogin -AdministratorLoginPassword $this.AdministratorPassword -Location $this.ServerLocation -Version $this.Version Write-Verbose "Operation Description: $($NewServer.OperationDescription)" Write-Verbose "Operation Id: $($NewServer.OperationId)" Write-Verbose "Operation Status: $($NewServer.OperationStatus)" Write-Verbose "New Azure SQL Database Server Created." Write-Verbose "Server name: $($NewServer.ServerName)" Write-Verbose "Server State: $($NewServer.State)" Write-Verbose "Server Version: $($NewServer.Version)" Write-Verbose "Server Location: $($NewServer.Location)" Write-Verbose "Administrator Login: $($NewServer.AdministratorLogin)" Write-Verbose "Server Location: $($NewServer.Location)" } else { Write-Verbose "Ensure Set to Absent... Deleting the Azure SQL Database Server $($this.ServerName) from subscription $($this.SubscriptionName)" $Remove = Remove-AzureSqlDatabaseServer -ServerName $this.ServerName -Force if( ! $? ) { Write-Error "Failed to remove Cloud service..." Write-Error $_.exception.message } else { Write-Verbose "Removed Server Successfully..." } } } [bool] Test() { $present=$true Import-AzurePublishSettingsFile "$($this.PublishSettingsFile)" Select-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" Set-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" if($this.ServerName -ne $null) { $server = Get-AzureSqlDatabaseServer -ServerName $this.ServerName -ErrorAction Ignore if($this.Ensure -eq [Ensure]::Present) { if($server -ne $null) { $present = $true } else { $present = $false } } else { if($server -ne $null) { $present = $false } else { $present = $true } } } else { if($this.Ensure -eq [Ensure]::Present) { $present = $false } else { Write-Verbose "Ensure Set to Absent... Please pass the Server Name to be deleted" $present = $true } } return $present } } <# This resource manages a Cloud service in a specific Subscription(Creation and deletion). [DscResource()] indicates the class is a DSC resource #> [DscResource()] class cEprsCreateAzureServiceBus { [DscProperty(Mandatory=$false)] [string]$ServiceBusName #Specifies a name for the new namespace [DscProperty(Mandatory=$false)] [ValidateSet("East US","West US","Central US","South Central US","East US 2","North Europe","West Europe","Southeast Asia","East Asia")] [string]$Location #Specifies a region for the new namespace [DscProperty(Mandatory)] [Boolean]$CreateACSNamespace #Specifies whether to create an associated ACS namespace in addition to the service namespace. [DscProperty(Mandatory)] [ValidateSet("Messaging","NotificationHub")] [string]$NamespaceType #Specify type of namespace to use... Eg: Messaging or NotificationHub [DscProperty(Key)] [string]$SubscriptionName #Name of the subscription [DscProperty(Key)] [string]$PublishSettingsFilePath #Path to publish Settings File [DscProperty(Mandatory)] [ValidateSet("Absent","Present")][Ensure]$Ensure #Ensure value(Give Present for install and Absent for uninstall) [cEprsCreateAzureServiceBus] Get() { Import-AzurePublishSettingsFile "$($this.PublishSettingsFilePath)" Select-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" Set-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" $server = Get-AzureSBNamespace -Name $this.ServiceBusName -ErrorAction Ignore if($server -ne $null) { $this.Ensure = [Ensure]::Present $this.ServiceBusName = $server.Name $this.NamespaceType = $server.NameSpaceType $this.Location = $server.Region } else { $this.Ensure = [Ensure]::Absent } return $this } [void] Set() { Import-AzurePublishSettingsFile "$($this.PublishSettingsFilePath)" Select-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" Set-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" if($this.Ensure -eq [Ensure]::Present) { Write-Verbose "Creating new Azure ServiceBus Namespace $($this.ServiceBusName)..." $NewServiceBus = New-AzureSBNamespace -Name $this.ServiceBusName -Location "$($this.Location)" -CreateACSNamespace $this.CreateACSNamespace -NamespaceType $this.NamespaceType Write-Verbose "New Service Bus Created with Namespace $($this.CreateACSNamespace)" Write-Verbose "Region: $($this.Location)" } else { Write-Verbose "Ensure Set to Absent... Deleting the Azure Service Bus $($this.ServiceBusName) from subscription $($this.SubscriptionName)" $Remove = Remove-AzureSBNamespace -Name $this.ServiceBusName -Force } } [bool] Test() { $present=$true Import-AzurePublishSettingsFile "$($this.PublishSettingsFilePath)" Select-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" Set-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" $SBNameSpace = Get-AzureSBNamespace -Name $this.ServiceBusName -ErrorAction Ignore if($this.Ensure -eq [Ensure]::Present) { if($SBNameSpace -ne $null) { $present = $true } else { $present = $false } } else { if($SBNameSpace -ne $null) { $present = $false } else { $present = $true } } return $present } } <# This resource manages a Cloud service in a specific Subscription(Creation and deletion). [DscResource()] indicates the class is a DSC resource #> [DscResource()] class cEprsCreateCloudService { [DscProperty(Key)] [string]$ServiceName #Service Name [DscProperty(Mandatory)] [ValidateSet("East US","West US","Central US","South Central US","East US 2","North Europe","West Europe","Southeast Asia","East Asia")] [string]$Location #Location of cloud service [DscProperty(Key)] [string]$SubscriptionName #Name of the subscription [DscProperty(Mandatory)] [string]$PublishSettingsFile #Path to publish Settings File [DscProperty(Mandatory)] [ValidateSet("Absent","Present")] [Ensure]$Ensure #Ensure value(Give Present for install and Absent for uninstall) [cEprsCreateCloudService] Get() { Import-AzurePublishSettingsFile "$($this.PublishSettingsFile)" Select-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" Set-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" $Service = Get-AzureService -ServiceName $this.ServiceName -ErrorAction Ignore if($Service -ne $null) { $this.Ensure = [Ensure]::Present $this.ServiceName = $Service.ServiceName $this.Location = $Service.Location } else { $this.Ensure = [Ensure]::Absent } return $this } [void] Set() { Import-AzurePublishSettingsFile "$($this.PublishSettingsFile)" Select-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" Set-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" if($this.Ensure -eq [Ensure]::Present) { Write-Verbose "Creating new Cloud service $($this.ServiceName)" New-AzureService -ServiceName "$($this.ServiceName)" -Location "$($this.Location)" if( ! $? ) { Write-Error "Failed to create Cloud service..." Write-Error $_.exception.message } } else { Write-Verbose "Ensure Set to Absent... Deleting the service $($this.ServiceName) from subscription $($this.SubscriptionName)" Remove-AzureService -ServiceName $this.ServiceName -Force if( ! $? ) { Write-Error "Failed to remove Cloud service..." Write-Error $_.exception.message } } } [bool] Test() { $present=$true Import-AzurePublishSettingsFile "$($this.PublishSettingsFile)" Select-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" Set-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" $Service = Get-AzureService -ServiceName $this.ServiceName -ErrorAction Ignore if($this.Ensure -eq [Ensure]::Present) { if($Service -ne $null) { $present = $true } else { $present = $false } } else { if($Service -ne $null) { $present = $false } else { $present = $true } } return $present } } <# This resource manages deployment in particular slot(Production\staging) of a Cloud service in a specific Subscription(Creation and deletion). [DscResource()] indicates the class is a DSC resource #> [DscResource()] class cEprsDeployPackage { [DscProperty(Key)] [string]$ServiceName #Service Name [DscProperty(Mandatory)] [ValidateSet("Production","Staging")] [string]$Slot #Deployment Slot(Stage:Staging\Production) [DscProperty(Mandatory)] [string]$Package #Package file path(eg: D:\Deployment\CloudApplication.cspkg) [DscProperty(Key)] [string]$SubscriptionName #Name of the subscription [DscProperty(Mandatory)] [string]$StorageAccountName #Name of the Storage Account [DscProperty(Mandatory)] [string]$Configuration #Configuration file path(eg: D:\Deployment\Cloudconfiguration.cscfg) [DscProperty(Mandatory)] [string]$PublishSettingsFile #Path to publish Settings File [DscProperty(Mandatory)] [string]$DeploymentLabel #Deployment Label [DscProperty(Mandatory)] [bool]$Upgrade #Check whether upgrade or new deployment(true or false) [Dscproperty(Mandatory)] [string]$RoleName #Role to deploy package for(Provide Exact Role Name to deploy for or all to deploy to all Roles). [DscProperty(Mandatory)] [ValidateSet("Absent","Present")][Ensure]$Ensure #Ensure value(Give Present for install and Absent for uninstall) [cEprsDeployPackage] Get() { Import-AzurePublishSettingsFile "$($this.PublishSettingsFile)" Select-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" Set-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" $Deployment = Get-AzureDeployment -ServiceName $this.ServiceName -Slot $this.Slot -ErrorAction Ignore if($Deployment -ne $null) { $this.Ensure = [Ensure]::Present $this.ServiceName = $Deployment.ServiceName $this.Slot = $Deployment.Slot } else { $this.Ensure = [Ensure]::Absent } return $this } [void] Set() { Import-AzurePublishSettingsFile "$($this.PublishSettingsFile)" Select-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" Set-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" -CurrentStorageAccountName $this.StorageAccountName $CertExtension = Get-ChildItem "$($this.CertToUpload)" if($this.Ensure -eq [Ensure]::Present) { if($this.Upgrade -ne "$true") { Write-Verbose "Creating a new deployment in slot $($this.Slot) of service $($this.ServiceName)" $NewDeployment = New-AzureDeployment -ServiceName $this.ServiceName -Package $($this.Package) -Configuration $($this.Configuration) -Slot $this.Slot -Label $this.DeploymentLabel if(! $?) { Write-Error "Failed to create deployment in slot $($this.Slot) for service $($this.ServiceName)" Write-Error $_.exception.message } else { Write-Verbose "Operation Description: $($NewDeployment.OperationDescription)" Write-Verbose "Operation Status: $($NewDeployment.OperationStatus)" Write-Verbose "Operation Id: $($NewDeployment.OperationId)" } } else { Write-Verbose "Deployment already exists in $($this.Slot) slot of $($this.ServiceName) for ROLE $($this.RoleName) , upgrading the deployment..." if($this.RoleName -eq "All") { $Upgradedeployment = Set-AzureDeployment -Upgrade -ServiceName $this.ServiceName -Package "$($this.Package)" -Configuration "$($this.Configuration)" -Slot $this.Slot -Label "$($this.DeploymentLabel)" -Force if(! $?) { Write-Error $_.exception.message Write-Error "Failed to update the deployment for $($this.Slot) of service $($this.ServiceName)" } else { Write-Verbose "Operation Description: $($Upgradedeployment.OperationDescription)" Write-Verbose "Operation Status: $($Upgradedeployment.OperationStatus)" Write-Verbose "Operation Id: $($Upgradedeployment.OperationId)" } } else { $Upgradedeployment = Set-AzureDeployment -Upgrade -ServiceName $this.ServiceName -Package "$($this.Package)" -Configuration "$($this.Configuration)" -Slot $this.Slot -Label "$($this.DeploymentLabel)" -Force -RoleName $this.RoleName if(! $?) { Write-Error $_.exception.message Write-Error "Failed to upgrade the deployment for $($this.Slot) of service $($this.ServiceName)" } else { Write-Verbose "Operation Description: $($Upgradedeployment.OperationDescription)" Write-Verbose "Operation Status: $($Upgradedeployment.OperationStatus)" Write-Verbose "Operation Id: $($Upgradedeployment.OperationId)" } } } } else { Write-Verbose "Ensure Set to Absent... Deleting the Deployment from slot $($this.Slot) of cloud service $($this.ServiceName)" Remove-AzureDeployment -ServiceName $this.ServiceName -Slot $this.Slot -Force if( ! $? ) { Write-Verbose $_.exception.message Write-Error "Failed to Delete deployment from service $($this.ServiceName)..." } } } [bool] Test() { $present=$true Import-AzurePublishSettingsFile "$($this.PublishSettingsFile)" Select-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" Set-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" $CheckDeployment = Get-AzureDeployment -ServiceName $this.ServiceName -Slot $this.Slot -ErrorAction Ignore if($this.Ensure -eq [Ensure]::Present) { if($CheckDeployment -ne $null -and $this.Upgrade -ne "$true") { $present = $true } else { $present = $false } } else { if($CheckDeployment -ne $null) { $present = $false } else { $present = $true } } return $present } } <# This resource manages a Cloud service in a specific Subscription(Creation and deletion). [DscResource()] indicates the class is a DSC resource #> [DscResource()] class cEprsUploadCertToService { [DscProperty(Key)] [string]$ServiceName #Service Name [DscProperty(Mandatory)] [string]$CertToUpload #Certificate Path [DscProperty(Mandatory=$false)] [string]$CertPassword #Certificate Password [DscProperty(Key)] [string]$SubscriptionName #Name of the subscription [DscProperty(Mandatory)] [string]$PublishSettingsFile #Path to publish Settings File [DscProperty(Mandatory)] [ValidateSet("Absent","Present")][Ensure]$Ensure #Ensure value(Give Present for install and Absent for uninstall) [cEprsUploadCertToService] Get() { Import-AzurePublishSettingsFile "$($this.PublishSettingsFile)" Select-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" Set-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" $CertificateDetails = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("$($this.CertToUpload)", "$($this.CertPassword)", "MachineKeySet,PersistKeySet") $CertThumbPrint = $CertificateDetails.Thumbprint $Service = Get-AzureCertificate -ServiceName $this.ServiceName -ThumbprintAlgorithm sha1 -Thumbprint "$CertThumbPrint" -ErrorAction Ignore if($Service -ne $null) { $this.Ensure = [Ensure]::Present $this.ServiceName = $Service.ServiceName $this.Location = $Service.Location } else { $this.Ensure = [Ensure]::Absent } return $this } [void] Set() { Import-AzurePublishSettingsFile "$($this.PublishSettingsFile)" Select-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" Set-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" $CertExtension = Get-ChildItem "$($this.CertToUpload)" if($this.Ensure -eq [Ensure]::Present) { Write-Verbose "Uploading Certifiacte to Cloud service $($this.ServiceName)" if($CertExtension.Name.Count -gt 1) { Write-Error "Please provide the full path of certificate along with certificate name" } else { if($CertExtension.Extension -eq ".pfx") { Add-AzureCertificate -ServiceName $this.ServiceName -CertToDeploy $this.CertToUpload -Password $this.CertPassword } elseif($CertExtension.Extension -eq ".cer") { Add-AzureCertificate -ServiceName $this.ServiceName -CertToDeploy $this.CertToUpload } else { Write-Error "Provide a valid certificate(.pfx or .cer) file" } } } else { Write-Verbose "Ensure Set to Absent... Deleting the Certificate $($CertExtension.Name) from cloud service $($this.ServiceName)" $CertificateDetails = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("$($this.CertToUpload)", "$($this.CertPassword)", "MachineKeySet,PersistKeySet") $CertThumbPrint = $CertificateDetails.Thumbprint Remove-AzureCertificate -ServiceName $this.ServiceName -ThumbprintAlgorithm "sha1" -Thumbprint $CertThumbPrint if( ! $? ) { Write-Verbose $_.exception.message Write-Error "Failed to remove Certificate from service..." } } } [bool] Test() { $present=$true Import-AzurePublishSettingsFile "$($this.PublishSettingsFile)" Select-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" Set-AzureSubscription -SubscriptionName "$($this.SubscriptionName)" $CertificateDetails = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("$($this.CertToUpload)", "$($this.CertPassword)", "MachineKeySet,PersistKeySet") $CertThumbPrint = $CertificateDetails.Thumbprint $CertCheck = Get-AzureCertificate -ServiceName "$($this.ServiceName)" -Thumbprint "$CertThumbPrint" -ThumbprintAlgorithm "sha1" -ErrorAction Ignore if($this.Ensure -eq [Ensure]::Present) { if($CertCheck -ne $null) { $present = $true } else { $present = $false } } else { if($CertCheck -ne $null) { $present = $false } else { $present = $true } } return $present } } |