33
44# requires -Modules AzureStack .Connect
55
6+ <#
7+ . SYNOPSIS
8+ Add-AzureStackVMSSGalleryItem: Adds the VMSS Gallery Item to your Azure Stack Marketplace.
9+ Remove-AzureStackVMSSGalleryItem: Adds the VMSS Gallery Item from your Azure Stack Marketplace.
10+ Add-VMImage: Uploads a VM Image to your Azure Stack and creates a Marketplace item for it.
11+ Remove-VMImage: Removes an existing VM Image from your Azure Stack. Does not delete any
12+ maketplace items created by Add-VMImage.
13+ New-Server2016VMImage: Creates and Uploads a new Server 2016 Core and / or Full Image and
14+ creates a Marketplace item for it.
15+ Get-VMImage: Gets a VM Image from your Azure Stack as an Administrator to view the provisioning state of the image.
16+ Add-VMExtension: Uploads a VM extension to your Azure Stack.
17+ Remove-VMExtension: Removes an existing VM extension from your Azure Stack.
18+ #>
19+
620function Add-AzureStackVMSSGalleryItem {
21+ [CmdletBinding ()]
22+ Param (
23+ [Parameter (Mandatory = $false )]
24+ [ValidatePattern (" ^[0-9a-zA-Z]+$" )]
25+ [ValidateLength (1 , 128 )]
26+ [String ] $Location = ' local'
27+ )
728
829 $rgName = " vmss.gallery"
930
10- New-AzureRmResourceGroup - Name $rgName - Location local - Force
31+ New-AzureRmResourceGroup - Name $rgName - Location $Location - Force
1132
1233 $saName = " vmssgallery"
1334
14- $sa = New-AzureRmStorageAccount - ResourceGroupName $rgName - Location local - Name $saName - Type Standard_LRS
35+ $sa = New-AzureRmStorageAccount - ResourceGroupName $rgName - Location $Location - Name $saName - Type Standard_LRS
1536
1637 $cName = " gallery"
1738
@@ -24,7 +45,8 @@ function Add-AzureStackVMSSGalleryItem {
2445
2546 $fileName = " microsoft.vmss.1.3.6.azpkg"
2647
27- $blob = $container | Set-AzureStorageBlobContent –File ([System.IO.Path ]::GetDirectoryName($PSCommandPath ) + " \" + $fileName ) –Blob $fileName - Force
48+ $blob = Set-AzureStorageBlobContent –File ($PSScriptRoot + " \" + $fileName ) –Blob $fileName - Container $cName - Force
49+ $container = Get-AzureStorageContainer - Name $cName - ErrorAction SilentlyContinue
2850
2951 $uri = $blob.Context.BlobEndPoint + $container.Name + " /" + $blob.Name
3052
@@ -39,19 +61,6 @@ function Remove-AzureStackVMSSGalleryItem {
3961 }
4062}
4163
42- <#
43- . SYNOPSIS
44- Contains 6 functions.
45- Add-VMImage: Uploads a VM Image to your Azure Stack and creates a Marketplace item for it.
46- Remove-VMImage: Removes an existing VM Image from your Azure Stack. Does not delete any
47- maketplace items created by Add-VMImage.
48- New-Server2016VMImage: Creates and Uploads a new Server 2016 Core and / or Full Image and
49- creates a Marketplace item for it.
50- Get-VMImage: Gets a VM Image from your Azure Stack as an Administrator to view the provisioning state of the image.
51- Add-VMExtension: Uploads a VM extension to your Azure Stack.
52- Remove-VMExtension: Removes an existing VM extension from your Azure Stack.
53- #>
54-
5564Function Add-VMImage {
5665
5766 [CmdletBinding (DefaultParameterSetName = ' VMImageFromLocal' )]
@@ -160,9 +169,9 @@ Function Add-VMImage{
160169 # same for storage
161170 $storageAccount = Get-AzureRmStorageAccount - Name $storageAccountName - ResourceGroupName $resourceGroupName - ErrorAction SilentlyContinue
162171 if (-not ($storageAccount )) {
163- $storageAccount = New-AzureRmStorageAccount - Name $storageAccountName - Location $location - ResourceGroupName $resourceGroupName - Type Standard_LRS
172+ $storageAccount = New-AzureRmStorageAccount - Name $storageAccountName - Location $location - ResourceGroupName $resourceGroupName - Type Standard_LRS
164173 }
165- Set-AzureRmCurrentStorageAccount - StorageAccountName $storageAccountName - ResourceGroupName $resourceGroupName
174+ Set-AzureRmCurrentStorageAccount - StorageAccountName $storageAccountName - ResourceGroupName $resourceGroupName
166175 # same for container
167176 $container = Get-AzureStorageContainer - Name $containerName - ErrorAction SilentlyContinue
168177 if (-not ($container )) {
@@ -297,11 +306,7 @@ Function Add-VMImage{
297306 $galleryItemURI = ' {0}{1}/{2}' -f $storageAccount.PrimaryEndpoints.Blob.AbsoluteUri , $containerName , $galleryItem.Name
298307
299308
300- if ((Get-Module AzureStack).Version -ge [System.Version ] " 1.2.9" ){
301- Add-AzureRMGalleryItem - GalleryItemUri $galleryItemURI
302- }else {
303- Add-AzureRMGalleryItem - SubscriptionId $subscription - GalleryItemUri $galleryItemURI - ApiVersion 2015 - 04 - 01
304- }
309+ Add-AzureRMGalleryItem - GalleryItemUri $galleryItemURI
305310
306311 # cleanup
307312 Remove-Item $GalleryItem
@@ -383,12 +388,7 @@ Function Remove-VMImage{
383388 $name = " $offer$sku "
384389 # Remove periods so that the offer and sku can be retrieved from the Marketplace Item name
385390 $name = $name -replace " \." , " -"
386- if ((Get-Module AzureStack).Version -ge [System.Version ] " 1.2.9" ){
387- Get-AzureRMGalleryItem | Where-Object {$_.Name -contains " $publisher .$name .$version " } | Remove-AzureRMGalleryItem
388- }else {
389- Get-AzureRMGalleryItem - ApiVersion 2015 - 04 - 01 | Where-Object {$_.Name -contains " $publisher .$name .$version " } | Remove-AzureRMGalleryItem - ApiVersion 2015 - 04 - 01
390- }
391-
391+ Get-AzureRMGalleryItem | Where-Object {$_.Name -contains " $publisher .$name .$version " } | Remove-AzureRMGalleryItem
392392 }
393393
394394}
@@ -431,7 +431,8 @@ function New-Server2016VMImage {
431431 [Parameter ()]
432432 [bool ] $CreateGalleryItem = $true ,
433433
434- [switch ] $Net35
434+ [Parameter ()]
435+ [bool ] $Net35 = $true
435436 )
436437 begin {
437438 function CreateWindowsVHD {
@@ -460,7 +461,7 @@ function New-Server2016VMImage {
460461 Write-Verbose - Message " Preparing VHD"
461462
462463 $VHDMount = Mount-DiskImage - ImagePath $VHDPath - PassThru - ErrorAction Stop
463- $disk = $VHDMount | Get-DiskImage | Get-Disk - ErrorAction Stop
464+ $disk = $VHDMount | Get-DiskImage - ErrorAction Stop | Get-Disk - ErrorAction SilentlyContinue
464465 $disk | Initialize-Disk - PartitionStyle MBR - ErrorAction Stop
465466 $partition = New-Partition - UseMaximumSize - Disknumber $disk.DiskNumber - IsActive:$True - AssignDriveLetter - ErrorAction Stop
466467 $volume = Format-Volume - Partition $partition - FileSystem NTFS - confirm:$false - ErrorAction Stop
@@ -677,7 +678,7 @@ Function CreateGalleyItem{
677678 [string ] $title ,
678679 [string ] $description
679680 )
680- $workdir = ' {0}\ {1}' -f $ env: TEMP , [System.Guid ]::NewGuid().ToString()
681+ $workdir = ' {0}{1}' -f [ System.IO.Path ]::GetTempPath() , [System.Guid ]::NewGuid().ToString()
681682 New-Item $workdir - ItemType Directory | Out-Null
682683 $basePath = (Get-Module AzureStack.ComputeAdmin).ModuleBase
683684 $compressedGalleryItemPath = Join-Path $basePath ' CustomizedVMGalleryItem.azpkg'
@@ -818,7 +819,6 @@ Function Add-VMExtension{
818819
819820 [Parameter (Mandatory = $true , ParameterSetName = ' VMExtensionFromLocal' )]
820821 [Parameter (Mandatory = $true , ParameterSetName = ' VMExtesionFromAzure' )]
821- [ValidatePattern (“ \d+\.\d+\.\d+” )]
822822 [String ] $version ,
823823
824824 [Parameter (ParameterSetName = ' VMExtensionFromLocal' )]
@@ -904,7 +904,7 @@ Function Add-VMExtension{
904904 $uri = $armEndpoint + ' /subscriptions/' + $subscription + ' /providers/Microsoft.Compute.Admin/locations/' + $location + ' /artifactTypes/VMExtension/publishers/' + $publisher
905905 $uri = $uri + ' /types/' + $type + ' /versions/' + $version + ' ?api-version=2015-12-01-preview'
906906
907- Log - Info $uri
907+ Write-Verbose $uri
908908
909909 # building request body JSON
910910 if ($pscmdlet.ParameterSetName -eq " VMExtensionFromLocal" ) {
@@ -958,10 +958,9 @@ Function Remove-VMExtension{
958958 [String ] $publisher ,
959959
960960 [Parameter (Mandatory = $true )]
961- [ValidatePattern (“ \d+\.\d+\.\d+” )]
962961 [String ] $version ,
963962
964- [String ] $type = " CustomScriptExtension " ,
963+ [String ] $type ,
965964
966965 [Parameter (Mandatory = $true )]
967966 [ValidateSet (' Windows' , ' Linux' )]
@@ -989,7 +988,7 @@ Function Remove-VMExtension{
989988 $uri = $armEndpoint + ' /subscriptions/' + $subscription + ' /providers/Microsoft.Compute.Admin/locations/' + $location + ' /artifactTypes/VMExtension/publishers/' + $publisher
990989 $uri = $uri + ' /types/' + $type + ' /versions/' + $version + ' ?api-version=2015-12-01-preview'
991990
992- Log - Info $uri
991+ Write-Verbose $uri
993992
994993 try {
995994 Invoke-RestMethod - Method DELETE - Uri $uri - ContentType ' application/json' - Headers $headers
0 commit comments