Skip to content

Commit 095af85

Browse files
Using EnvironmentName Parameter
1 parent 1bff11f commit 095af85

12 files changed

Lines changed: 288 additions & 229 deletions

File tree

ComputeAdmin/AzureStack.ComputeAdmin.psm1

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Function Add-VMImage{
7878

7979
[Parameter(Mandatory=$true, ParameterSetName='VMImageFromLocal')]
8080
[Parameter(Mandatory=$true, ParameterSetName='VMImageFromAzure')]
81-
[string] $ArmEndpoint,
81+
[string] $EnvironmentName,
8282

8383
[Parameter(ParameterSetName='VMImageFromLocal')]
8484
[Parameter(ParameterSetName='VMImageFromAzure')]
@@ -93,15 +93,7 @@ Function Add-VMImage{
9393
[bool] $CreateGalleryItem = $true
9494
)
9595

96-
if(!$ARMEndpoint.Contains('https://')){
97-
if($ARMEndpoint.Contains('http://')){
98-
$ARMEndpoint = $ARMEndpoint.Substring(7)
99-
$ARMEndpoint = 'https://' + $ARMEndpoint
100-
101-
}else{
102-
$ARMEndpoint = 'https://' + $ARMEndpoint
103-
}
104-
}
96+
$ARMEndpoint = GetARMEndpoint -EnvironmentName $EnvironmentName -ErrorAction Stop
10597

10698
if($CreateGalleryItem -eq $false -and $PSBoundParameters.ContainsKey('title')) {
10799
Write-Error -Message "The title parameter only applies to creating a gallery item." -ErrorAction Stop
@@ -115,11 +107,11 @@ Function Add-VMImage{
115107
$storageAccountName = "addvmimagestorageaccount"
116108
$containerName = "addvmimagecontainer"
117109

118-
$subscription, $headers = (Get-AzureStackAdminSubTokenHeader -TenantId $tenantId -AzureStackCredentials $azureStackCredentials -ArmEndpoint $ArmEndpoint)
110+
$subscription, $headers = (Get-AzureStackAdminSubTokenHeader -TenantId $tenantId -AzureStackCredentials $azureStackCredentials -EnvironmentName $EnvironmentName)
119111

120112
#pre validate if image is not already deployed
121113
$VMImageAlreadyAvailable = $false
122-
if ($(Get-VMImage -publisher $publisher -offer $offer -sku $sku -version $version -ArmEndpoint $ArmEndpoint -tenantID $tenantID -azureStackCredentials $azureStackCredentials -location $location -ErrorAction SilentlyContinue).Properties.ProvisioningState -eq 'Succeeded') {
114+
if ($(Get-VMImage -publisher $publisher -offer $offer -sku $sku -version $version -EnvironmentName $EnvironmentName -tenantID $tenantID -azureStackCredentials $azureStackCredentials -location $location -ErrorAction SilentlyContinue).Properties.ProvisioningState -eq 'Succeeded') {
123115
$VMImageAlreadyAvailable = $true
124116
Write-Verbose -Message ('VM Image with publisher "{0}", offer "{1}", sku "{2}", version "{3}" already is present.' -f $publisher,$offer,$sku,$version) -Verbose -ErrorAction Stop
125117
}
@@ -237,7 +229,7 @@ Function Add-VMImage{
237229
Invoke-RestMethod -Method PUT -Uri $uri -Body $RequestBody -ContentType 'application/json' -Headers $Headers
238230
}
239231

240-
$platformImage = Get-VMImage -publisher $publisher -offer $offer -sku $sku -version $version -ArmEndpoint $ArmEndpoint -tenantID $tenantID -azureStackCredentials $azureStackCredentials -location $location
232+
$platformImage = Get-VMImage -publisher $publisher -offer $offer -sku $sku -version $version -EnvironmentName $EnvironmentName -tenantID $tenantID -azureStackCredentials $azureStackCredentials -location $location
241233

242234
$downloadingStatusCheckCount = 0
243235
while($platformImage.Properties.ProvisioningState -ne 'Succeeded') {
@@ -254,7 +246,7 @@ Function Add-VMImage{
254246
$downloadingStatusCheckCount++
255247
if($downloadingStatusCheckCount % 30 -eq 0){
256248
Write-Verbose -Message "Obtaining refreshed token..."
257-
$subscription, $Headers = (Get-AzureStackAdminSubTokenHeader -TenantId $tenantId -AzureStackCredentials $azureStackCredentials -ArmEndpoint $ArmEndpoint)
249+
$subscription, $Headers = (Get-AzureStackAdminSubTokenHeader -TenantId $tenantId -AzureStackCredentials $azureStackCredentials -EnvironmentName $EnvironmentName)
258250
}
259251
$platformImage = Invoke-RestMethod -Method GET -Uri $uri -ContentType 'application/json' -Headers $Headers
260252
}
@@ -313,23 +305,16 @@ Function Remove-VMImage{
313305
[switch] $KeepMarketplaceItem,
314306

315307
[Parameter(Mandatory=$true)]
316-
[string] $ArmEndpoint
308+
[string] $EnvironmentName
317309

318310
)
319311

320-
if(!$ARMEndpoint.Contains('https://')){
321-
if($ARMEndpoint.Contains('http://')){
322-
$ARMEndpoint = $ARMEndpoint.Substring(7)
323-
$ARMEndpoint = 'https://' + $ARMEndpoint
324-
}else{
325-
$ARMEndpoint = 'https://' + $ARMEndpoint
326-
}
327-
}
312+
$ARMEndpoint = GetARMEndpoint -EnvironmentName $EnvironmentName -ErrorAction Stop
328313

329-
$subscription, $headers = (Get-AzureStackAdminSubTokenHeader -TenantId $tenantId -AzureStackCredentials $azureStackCredentials -ArmEndpoint $ArmEndpoint)
314+
$subscription, $headers = (Get-AzureStackAdminSubTokenHeader -TenantId $tenantId -AzureStackCredentials $azureStackCredentials -EnvironmentName $EnvironmentName)
330315

331316
$VMImageExists = $false
332-
if (Get-VMImage -publisher $publisher -offer $offer -sku $sku -version $version -ArmEndpoint $ArmEndpoint -tenantID $tenantID -azureStackCredentials $azureStackCredentials -location $location -ErrorAction SilentlyContinue) {
317+
if (Get-VMImage -publisher $publisher -offer $offer -sku $sku -version $version -EnvironmentName $EnvironmentName -tenantID $tenantID -azureStackCredentials $azureStackCredentials -location $location -ErrorAction SilentlyContinue) {
333318
Write-Verbose "VM Image is present in Azure Stack - continuing to remove" -Verbose
334319
$VMImageExists = $true
335320
}
@@ -389,7 +374,7 @@ function New-Server2016VMImage {
389374
[string] $CUPath,
390375

391376
[Parameter(Mandatory)]
392-
[string] $ArmEndpoint,
377+
[string] $EnvironmentName,
393378

394379
[Parameter()]
395380
[string] $VHDSizeInMB = 40960,
@@ -502,7 +487,7 @@ function New-Server2016VMImage {
502487

503488
Write-Verbose -Message "Checking authorization against your Azure Stack environment" -Verbose
504489

505-
$subscription, $headers = (Get-AzureStackAdminSubTokenHeader -TenantId $tenantId -AzureStackCredentials $azureStackCredentials -ArmEndpoint $ArmEndpoint -ErrorAction Stop)
490+
$subscription, $headers = (Get-AzureStackAdminSubTokenHeader -TenantId $tenantId -AzureStackCredentials $azureStackCredentials -EnvironmentName $EnvironmentName -ErrorAction Stop)
506491

507492
Write-Verbose -Message "Authorization verified" -Verbose
508493

@@ -569,7 +554,7 @@ function New-Server2016VMImage {
569554
osType = 'Windows'
570555
tenantID = $tenantID
571556
azureStackCredentials = $AzureStackCredentials
572-
ArmEndpoint = $ArmEndpoint
557+
EnvironmentName = $EnvironmentName
573558
location = $location
574559
}
575560

@@ -579,7 +564,7 @@ function New-Server2016VMImage {
579564

580565
#Pre-validate that the VM Image is not already available
581566
$VMImageAlreadyAvailable = $false
582-
if ($(Get-VMImage -publisher $PublishArguments.publisher -offer $PublishArguments.offer -sku $sku -version $PublishArguments.version -ArmEndpoint $ArmEndpoint -tenantID $tenantID -azureStackCredentials $azureStackCredentials -location $PublishArguments.location -ErrorAction SilentlyContinue).Properties.ProvisioningState -eq 'Succeeded') {
567+
if ($(Get-VMImage -publisher $PublishArguments.publisher -offer $PublishArguments.offer -sku $sku -version $PublishArguments.version -EnvironmentName $EnvironmentName -tenantID $tenantID -azureStackCredentials $azureStackCredentials -location $PublishArguments.location -ErrorAction SilentlyContinue).Properties.ProvisioningState -eq 'Succeeded') {
583568
$VMImageAlreadyAvailable = $true
584569
Write-Verbose -Message ('VM Image with publisher "{0}", offer "{1}", sku "{2}", version "{3}" already is present.' -f $publisher,$offer,$sku,$version) -Verbose -ErrorAction Stop
585570
}
@@ -612,7 +597,7 @@ function New-Server2016VMImage {
612597

613598
#Pre-validate that the VM Image is not already available
614599
$VMImageAlreadyAvailable = $false
615-
if ($(Get-VMImage -publisher $PublishArguments.publisher -offer $PublishArguments.offer -sku $sku -version $PublishArguments.version -ArmEndpoint $ArmEndpoint -tenantID $tenantID -azureStackCredentials $azureStackCredentials -location $PublishArguments.location -ErrorAction SilentlyContinue).Properties.ProvisioningState -eq 'Succeeded') {
600+
if ($(Get-VMImage -publisher $PublishArguments.publisher -offer $PublishArguments.offer -sku $sku -version $PublishArguments.version -EnvironmentName $EnvironmentName -tenantID $tenantID -azureStackCredentials $azureStackCredentials -location $PublishArguments.location -ErrorAction SilentlyContinue).Properties.ProvisioningState -eq 'Succeeded') {
616601
$VMImageAlreadyAvailable = $true
617602
Write-Verbose -Message ('VM Image with publisher "{0}", offer "{1}", sku "{2}", version "{3}" already is present.' -f $publisher,$offer,$sku,$version) -Verbose -ErrorAction Stop
618603
}
@@ -767,22 +752,13 @@ Function Get-VMImage{
767752
[System.Management.Automation.PSCredential] $azureStackCredentials,
768753

769754
[Parameter(Mandatory=$true)]
770-
[string] $ArmEndpoint
755+
[string] $EnvironmentName
771756

772757
)
773758

774-
if(!$ARMEndpoint.Contains('https://')){
775-
if($ARMEndpoint.Contains('http://')){
776-
$ARMEndpoint = $ARMEndpoint.Substring(7)
777-
$ARMEndpoint = 'https://' + $ARMEndpoint
778-
}else{
779-
$ARMEndpoint = 'https://' + $ARMEndpoint
780-
}
781-
}
782-
783-
$ArmEndpoint = $ArmEndpoint.TrimEnd("/")
759+
$ARMEndpoint = GetARMEndpoint -EnvironmentName $EnvironmentName -ErrorAction Stop
784760

785-
$subscription, $headers = (Get-AzureStackAdminSubTokenHeader -TenantId $tenantId -AzureStackCredentials $azureStackCredentials -ArmEndpoint $ArmEndpoint)
761+
$subscription, $headers = (Get-AzureStackAdminSubTokenHeader -TenantId $tenantId -AzureStackCredentials $azureStackCredentials -EnvironmentName $EnvironmentName)
786762

787763
$uri = $armEndpoint + '/subscriptions/' + $subscription + '/providers/Microsoft.Compute.Admin/locations/' + $location + '/artifactTypes/platformImage/publishers/' + $publisher
788764
$uri = $uri + '/offers/' + $offer + '/skus/' + $sku + '/versions/' + $version + '?api-version=2015-12-01-preview'
@@ -849,14 +825,17 @@ Function Add-VMExtension{
849825

850826
[Parameter(Mandatory=$true, ParameterSetName='VMExtensionFromLocal')]
851827
[Parameter(Mandatory=$true, ParameterSetName='VMExtesionFromAzure')]
852-
[string] $ArmEndpoint
828+
[string] $EnvironmentName
829+
853830
)
854831

832+
$ARMEndpoint = GetARMEndpoint -EnvironmentName $EnvironmentName -ErrorAction Stop
833+
855834
$resourceGroupName = "addvmextresourcegroup"
856835
$storageAccountName = "addvmextstorageaccount"
857836
$containerName = "addvmextensioncontainer"
858837

859-
$subscription, $headers = (Get-AzureStackAdminSubTokenHeader -TenantId $tenantId -AzureStackCredentials $azureStackCredentials -ArmEndpoint $ArmEndpoint)
838+
$subscription, $headers = (Get-AzureStackAdminSubTokenHeader -TenantId $tenantId -AzureStackCredentials $azureStackCredentials -EnvironmentName $EnvironmentName)
860839

861840
#potentially the RG was not cleaned up when exception happened in previous run. Test for exist
862841
if (-not (Get-AzureRmResourceGroup -Name $resourceGroupName -Location $location -ErrorAction SilentlyContinue)) {
@@ -962,10 +941,13 @@ Function Remove-VMExtension{
962941
[System.Management.Automation.PSCredential] $azureStackCredentials,
963942

964943
[Parameter(Mandatory=$true)]
965-
[string] $ArmEndpoint
944+
[string] $EnvironmentName
945+
966946
)
967947

968-
$subscription, $headers = (Get-AzureStackAdminSubTokenHeader -TenantId $tenantId -AzureStackCredentials $azureStackCredentials -ArmEndpoint $ArmEndpoint)
948+
$ARMEndpoint = GetARMEndpoint -EnvironmentName $EnvironmentName -ErrorAction Stop
949+
950+
$subscription, $headers = (Get-AzureStackAdminSubTokenHeader -TenantId $tenantId -AzureStackCredentials $azureStackCredentials -EnvironmentName $EnvironmentName)
969951

970952
$ArmEndpoint = $ArmEndpoint.TrimEnd("/")
971953
$uri = $armEndpoint + '/subscriptions/' + $subscription + '/providers/Microsoft.Compute.Admin/locations/' + $location + '/artifactTypes/VMExtension/publishers/' + $publisher
@@ -981,3 +963,21 @@ Function Remove-VMExtension{
981963
}
982964
}
983965

966+
Function GetARMEndpoint{
967+
param(
968+
# Azure Stack environment name
969+
[Parameter(Mandatory=$true)]
970+
[string] $EnvironmentName
971+
972+
)
973+
974+
$armEnv = Get-AzureRmEnvironment -Name $EnvironmentName
975+
if($armEnv -ne $null) {
976+
$ARMEndpoint = $armEnv.ResourceManagerUrl
977+
}
978+
else {
979+
Write-Error "The Azure Stack environment with the name $EnvironmentName does not exist. Create one with Add-AzureStackAzureRmEnvironment." -ErrorAction Stop
980+
}
981+
982+
$ARMEndpoint
983+
}

ComputeAdmin/README.md

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,16 @@ The New-Server2016VMImage allows you to add a Windows Server 2016 Evaluation VM
3737

3838
As a prerequisite, you need to obtain the Windows Server 2016 Evaluation ISO which can be found [here](https://www.microsoft.com/en-us/evalcenter/evaluate-windows-server-2016).
3939

40+
You will need to reference your Azure Stack Administrator environment. To create an administrator environment use the below. The ARM endpoint below is the administrator default for a one-node environment.
41+
42+
```powershell
43+
Add-AzureStackAzureRmEnvironment -Name "AzureStackAdmin" -ArmEndpoint "https://adminmanagement.local.azurestack.external" -AadTenant $aadTenant
44+
```
45+
4046
An example usage is the following:
4147
```powershell
4248
$ISOPath = "<Path to ISO>"
43-
New-Server2016VMImage -ISOPath $ISOPath -TenantId $aadTenant -ArmEndpoint "https://adminmanagement.local.azurestack.external"
49+
New-Server2016VMImage -ISOPath $ISOPath -TenantId $aadTenant -EnvironmentName "AzureStackAdmin"
4450
```
4551
Please make sure to specify the correct administrator ARM endpoint for your environment.
4652

@@ -64,9 +70,15 @@ Please note that to use this image for **installing additional Azure Stack servi
6470
- Specify osType as Windows or Linux.
6571
- Include your Azure Active Directory tenant ID in the form *&lt;mydirectory&gt;*.onmicrosoft.com.
6672
- The following is an example invocation of the script:
67-
73+
74+
You will need to reference your Azure Stack Administrator environment. To create an administrator environment use the below. The ARM endpoint below is the administrator default for a one-node environment.
75+
6876
```powershell
69-
Add-VMImage -publisher "Canonical" -offer "UbuntuServer" -sku "14.04.3-LTS" -version "1.0.0" -osType Linux -osDiskLocalPath 'C:\Users\<me>\Desktop\UbuntuServer.vhd' -tenantID <GUID AADTenant>
77+
Add-AzureStackAzureRmEnvironment -Name "AzureStackAdmin" -ArmEndpoint "https://adminmanagement.local.azurestack.external" -AadTenant $aadTenant
78+
```
79+
80+
```powershell
81+
Add-VMImage -publisher "Canonical" -offer "UbuntuServer" -sku "14.04.3-LTS" -version "1.0.0" -osType Linux -osDiskLocalPath 'C:\Users\<me>\Desktop\UbuntuServer.vhd' -tenantID <GUID AADTenant> -EnvironmentName "AzureStackAdmin"
7082
```
7183

7284
Note: The cmdlet requests credentials for adding the VM image. Provide the administrator Azure Active Directory credentials, such as *&lt;Admin Account&gt;*@*&lt;mydirectory&gt;*.onmicrosoft.com, to the prompt.
@@ -82,22 +94,40 @@ To verify that the command ran successfully, go to Marketplace in the portal, an
8294
## Remove a VM Image with PowerShell
8395
Run the below command to remove an uploaded VM image. After removal, tenants will no longer be able to deploy virtual machines with this image.
8496

97+
You will need to reference your Azure Stack Administrator environment. To create an administrator environment use the below. The ARM endpoint below is the administrator default for a one-node environment.
98+
8599
```powershell
86-
Remove-VMImage -publisher "Canonical" -offer "UbuntuServer" -sku "14.04.3-LTS" -version "1.0.0" -tenantID <GUID AADTenant>
100+
Add-AzureStackAzureRmEnvironment -Name "AzureStackAdmin" -ArmEndpoint "https://adminmanagement.local.azurestack.external" -AadTenant $aadTenant
101+
```
102+
103+
```powershell
104+
Remove-VMImage -publisher "Canonical" -offer "UbuntuServer" -sku "14.04.3-LTS" -version "1.0.0" -tenantID <GUID AADTenant> -EnvironmentName "AzureStackAdmin"
87105
```
88106

89107
Note: This cmdlet will remove the associated Marketplace item unless the -KeepMarketplaceItem parameter is specified.
90108

91109
## Add a VM extension to the Compute with PowerShell
110+
You will need to reference your Azure Stack Administrator environment. To create an administrator environment use the below. The ARM endpoint below is the administrator default for a one-node environment.
111+
112+
```powershell
113+
Add-AzureStackAzureRmEnvironment -Name "AzureStackAdmin" -ArmEndpoint "https://adminmanagement.local.azurestack.external" -AadTenant $aadTenant
114+
```
92115
An example usage is the following:
116+
93117
```powershell
94118
$path = "<Path to vm extension zip>"
95-
Add-VMExtension -publisher $publisher -version $version -extensionLocalPath $path -osType Windows -tenantID $aadTenant -azureStackCredentials $azureStackCredentials -type "CustomVmExtension"
119+
Add-VMExtension -publisher "Publisher" -type "Type" -version $version -extensionLocalPath $path -osType Windows -tenantID $aadTenant -azureStackCredentials $azureStackCredentials -EnvironmentName "AzureStackAdmin"
96120
```
97121

98122
# Remove a VM extension with PowerShell
123+
124+
You will need to reference your Azure Stack Administrator environment. To create an administrator environment use the below. The ARM endpoint below is the administrator default for a one-node environment.
125+
126+
```powershell
127+
Add-AzureStackAzureRmEnvironment -Name "AzureStackAdmin" -ArmEndpoint "https://adminmanagement.local.azurestack.external" -AadTenant $aadTenant
128+
```
99129
Run the below command to remove an uploaded VM extension.
100130

101131
```powershell
102-
Remove-VMExtension -publisher $publisher -version $version -osType Windows -tenantID $tenantId -azureStackCredentials $azureStackCredentials -type "CustomVmExtension"
132+
Remove-VMExtension -publisher "Publisher" -type "Type" -version "1.0.0.0" -osType Windows -tenantID $tenantId -azureStackCredentials $azureStackCredentials -EnvironmentName "AzureStackAdmin"
103133
```

0 commit comments

Comments
 (0)