|
| 1 | +# ---------------------------------------------------------------------------------- |
| 2 | +# |
| 3 | +# Copyright Microsoft Corporation |
| 4 | +# Licensed under the Apache License, Version 2.0 (the \"License\"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an \"AS IS\" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | +# ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +<# |
| 16 | +.Synopsis |
| 17 | +Create a in-memory object for Diagnostics Extension |
| 18 | +.Description |
| 19 | +Create a in-memory object for Diagnostics Extension |
| 20 | +
|
| 21 | +.Outputs |
| 22 | +Microsoft.Azure.PowerShell.Cmdlets.CloudService.Models.Api20201001Preview.Extension |
| 23 | +.Link |
| 24 | +https://docs.microsoft.com/en-us/powershell/module/az.CloudService/new-AzCloudServiceExtensionObject |
| 25 | +#> |
| 26 | + |
| 27 | +function New-AzCloudServiceDiagnosticsExtension { |
| 28 | + [OutputType('Microsoft.Azure.PowerShell.Cmdlets.CloudService.Models.Api20201001Preview.Extension')] |
| 29 | + param( |
| 30 | + [Parameter(HelpMessage="Name of Diagnostics Extension.", Mandatory)] |
| 31 | + [string] $Name, |
| 32 | + |
| 33 | + [Parameter(HelpMessage="Subscription.")] |
| 34 | + [Microsoft.Azure.PowerShell.Cmdlets.CloudService.Category('Path')] |
| 35 | + [Microsoft.Azure.PowerShell.Cmdlets.CloudService.Runtime.DefaultInfo(Script='(Get-AzContext).Subscription.Id')] |
| 36 | + [System.String] |
| 37 | + # Subscription credentials which uniquely identify Microsoft Azure subscription. |
| 38 | + # The subscription ID forms part of the URI for every service call. |
| 39 | + ${Subscription}, |
| 40 | + |
| 41 | + [Parameter(HelpMessage="Resource Group name of Cloud Service.", Mandatory)] |
| 42 | + [string] $ResourceGroupName, |
| 43 | + |
| 44 | + [Parameter(HelpMessage="Name of Cloud Service.", Mandatory)] |
| 45 | + [string] $CloudServiceName, |
| 46 | + |
| 47 | + [Parameter(HelpMessage="Specifies the configuration for Azure Diagnostics. You can download the schema by using the following command: (Get-AzureServiceAvailableExtension -ExtensionName 'PaaSDiagnostics' -ProviderNamespace 'Microsoft.Azure.Diagnostics').PublicConfigurationSchema | Out-File -Encoding utf8 -FilePath 'WadConfig.xsd'", Mandatory)] |
| 48 | + [string] $DiagnosticsConfigurationPath, |
| 49 | + |
| 50 | + [Parameter(HelpMessage="Name of the Storage Account.", Mandatory)] |
| 51 | + [string] $StorageAccountName, |
| 52 | + |
| 53 | + [Parameter(HelpMessage="Storage Account Key.", Mandatory)] |
| 54 | + [string] $StorageAccountKey, |
| 55 | + |
| 56 | + [Parameter(HelpMessage="Specifies the version of the extension.")] |
| 57 | + [string] $TypeHandlerVersion, |
| 58 | + |
| 59 | + [Parameter(HelpMessage="Roles applied to.")] |
| 60 | + [string[]] $RolesAppliedTo, |
| 61 | + |
| 62 | + [Parameter(HelpMessage="Auto upgrade minor version.")] |
| 63 | + [Boolean] $AutoUpgradeMinorVersion |
| 64 | + ) |
| 65 | + |
| 66 | + process { |
| 67 | + $publisher = "Microsoft.Azure.Diagnostics" |
| 68 | + $extensionType = "PaaSDiagnostics" |
| 69 | + |
| 70 | + if (!(Test-Path $DiagnosticsConfigurationPath -PathType Leaf)) |
| 71 | + { |
| 72 | + throw ("DiagnosticsConfigurationPath does not exits: " + $DiagnosticsConfigurationPath) |
| 73 | + } |
| 74 | + |
| 75 | + [xml]$diagnosticsConfigurationXml = Get-Content $DiagnosticsConfigurationPath |
| 76 | + |
| 77 | + $storageAccount = $diagnosticsConfigurationXml.PublicConfig.ChildNodes | Where-Object { $_.Name -eq 'StorageAccount' } |
| 78 | + if ($storageAccount) |
| 79 | + { |
| 80 | + Write-Host "Using StorageAccount information defined in diagnostics configuration file." |
| 81 | + } |
| 82 | + else |
| 83 | + { |
| 84 | + $storageAccount = $diagnosticsConfigurationXml.CreateElement('StorageAccount', $diagnosticsConfigurationXml.PublicConfig.NamespaceURI) |
| 85 | + $storageAccount.InnerText = $StorageAccountName |
| 86 | + $storageAccount = $diagnosticsConfigurationXml.PublicConfig.AppendChild($storageAccount) |
| 87 | + } |
| 88 | + |
| 89 | + $metrics = $diagnosticsConfigurationXml.PublicConfig.WadCfg.DiagnosticMonitorConfiguration.ChildNodes | Where-Object { $_.Name -eq 'Metrics' } |
| 90 | + if ($metrics) |
| 91 | + { |
| 92 | + Write-Host "Using Metrics information defined in diagnostics configuration file." |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + $metrics = $diagnosticsConfigurationXml.CreateElement('Metrics', $diagnosticsConfigurationXml.PublicConfig.NamespaceURI) |
| 97 | + $resourceId = "/subscriptions/$Subscription/resourceGroups/$ResourceGroupName/providers/Microsoft.Compute/cloudservices/$CloudServiceName" |
| 98 | + $metrics.SetAttribute('resourceId', $resourceId) |
| 99 | + $metrics = $diagnosticsConfigurationXml.PublicConfig.WadCfg.DiagnosticMonitorConfiguration.AppendChild($metrics) |
| 100 | + } |
| 101 | + |
| 102 | + $setting = $diagnosticsConfigurationXml.PublicConfig.OuterXml |
| 103 | + |
| 104 | + $privateConfig = $diagnosticsConfigurationXml.ChildNodes | Where-Object { $_.Name -eq 'PrivateConfig' } |
| 105 | + if ($privateConfig) |
| 106 | + { |
| 107 | + $protectedSetting = $privateConfig.OuterXml |
| 108 | + Write-Host "Using PrivateConfig information defined in diagnostics configuration file." |
| 109 | + } |
| 110 | + else |
| 111 | + { |
| 112 | + $storageEndpoint = "https://core.windows.net" |
| 113 | + $protectedSetting = '<?xml version="1.0" encoding="UTF-8"?><PrivateConfig xmlns="http://schemas.microsoft.com/ServiceHosting/2010/10/DiagnosticsConfiguration"><StorageAccount endpoint="' + $storageEndpoint + '" key="' + $StorageAccountKey + '" name="' + $storageAccountName + '"/></PrivateConfig>' |
| 114 | + } |
| 115 | + |
| 116 | + return New-AzCloudServiceExtensionObject -Name $Name -Publisher $publisher -Type $extensionType -TypeHandlerVersion $TypeHandlerVersion -Setting $setting -ProtectedSetting $protectedSetting -RolesAppliedTo $RolesAppliedTo -AutoUpgradeMinorVersion $AutoUpgradeMinorVersion |
| 117 | + } |
| 118 | +} |
0 commit comments