@@ -100,13 +100,11 @@ Function Add-VMImage{
100100 }
101101 }
102102
103- if ($CreateGalleryItem -eq $false -and $PSBoundParameters.ContainsKey (' title' ))
104- {
103+ if ($CreateGalleryItem -eq $false -and $PSBoundParameters.ContainsKey (' title' )) {
105104 Write-Error - Message " The title parameter only applies to creating a gallery item." - ErrorAction Stop
106105 }
107106
108- if ($CreateGalleryItem -eq $false -and $PSBoundParameters.ContainsKey (' description' ))
109- {
107+ if ($CreateGalleryItem -eq $false -and $PSBoundParameters.ContainsKey (' description' )) {
110108 Write-Error - Message " The description parameter only applies to creating a gallery item." - ErrorAction Stop
111109 }
112110
@@ -141,18 +139,15 @@ Function Add-VMImage{
141139 $container = New-AzureStorageContainer - Name $containerName - Permission Blob
142140 }
143141
144- if (($pscmdlet.ParameterSetName -eq " VMImageFromLocal" ) -and (-not $VMImageAlreadyAvailable ))
145- {
142+ if (($pscmdlet.ParameterSetName -eq " VMImageFromLocal" ) -and (-not $VMImageAlreadyAvailable )) {
146143 $storageAccount.PrimaryEndpoints.Blob
147144 $script :osDiskName = Split-Path $osDiskLocalPath - Leaf
148145 $script :osDiskBlobURIFromLocal = ' {0}{1}/{2}' -f $storageAccount.PrimaryEndpoints.Blob.AbsoluteUri , $containerName , $osDiskName
149146 Add-AzureRmVhd - Destination $osDiskBlobURIFromLocal - ResourceGroupName $resourceGroupName - LocalFilePath $osDiskLocalPath - OverWrite
150147
151148 $script :dataDiskBlobURIsFromLocal = New-Object System.Collections.ArrayList
152- if ($PSBoundParameters.ContainsKey (' dataDisksLocalPaths' ))
153- {
154- foreach ($dataDiskLocalPath in $dataDisksLocalPaths )
155- {
149+ if ($PSBoundParameters.ContainsKey (' dataDisksLocalPaths' )) {
150+ foreach ($dataDiskLocalPath in $dataDisksLocalPaths ) {
156151 $dataDiskName = Split-Path $dataDiskLocalPath - Leaf
157152 $dataDiskBlobURI = " https://$storageAccountName .blob.$Domain /$containerName /$dataDiskName "
158153 $dataDiskBlobURIsFromLocal.Add ($dataDiskBlobURI )
@@ -169,57 +164,46 @@ Function Add-VMImage{
169164 # building platform image JSON
170165
171166 # building osDisk json
172- if ($pscmdlet.ParameterSetName -eq " VMImageFromLocal" )
173- {
167+ if ($pscmdlet.ParameterSetName -eq " VMImageFromLocal" ) {
174168 $osDiskJSON = ' "OsDisk":{"OsType":"' + $osType + ' ","Uri":"' + $osDiskBlobURIFromLocal + ' "}'
175169 }
176- else
177- {
170+ else {
178171 $osDiskJSON = ' "OsDisk":{"OsType":"' + $osType + ' ","Uri":"' + $osDiskBlobURI + ' "}'
179172 }
180173
181174 # building details JSON
182175 $detailsJSON = ' '
183- if ($PSBoundParameters.ContainsKey (' billingPartNumber' ))
184- {
176+ if ($PSBoundParameters.ContainsKey (' billingPartNumber' )) {
185177 $detailsJSON = ' "Details":{"BillingPartNumber":"' + $billingPartNumber + ' "}'
186178 }
187179
188180 # building dataDisk JSON
189181 $dataDisksJSON = ' '
190182
191- if ($pscmdlet.ParameterSetName -eq " VMImageFromLocal" )
192- {
193- if ($dataDiskBlobURIsFromLocal.Count -ne 0 )
194- {
195- $dataDisksJSON = ' "DataDisks":['
196- $i = 0
197- foreach ($dataDiskBlobURI in $dataDiskBlobURIsFromLocal )
198- {
199- if ($i -ne 0 )
200- {
183+ if ($pscmdlet.ParameterSetName -eq " VMImageFromLocal" ) {
184+ if ($dataDiskBlobURIsFromLocal.Count -ne 0 ) {
185+ $dataDisksJSON = ' "DataDisks":['
186+ $i = 0
187+ foreach ($dataDiskBlobURI in $dataDiskBlobURIsFromLocal ) {
188+ if ($i -ne 0 ) {
201189 $dataDisksJSON = $dataDisksJSON + ' , '
202190 }
203191
204192 $newDataDisk = ' {"Lun":' + $i + ' , "Uri":"' + $dataDiskBlobURI + ' "}'
205193 $dataDisksJSON = $dataDisksJSON + $newDataDisk ;
206194
207195 ++ $i
208- }
196+ }
209197
210- $dataDisksJSON = $dataDisksJSON + ' ]'
211- }
198+ $dataDisksJSON = $dataDisksJSON + ' ]'
199+ }
212200 }
213- else
214- {
215- if ($dataDiskBlobURIs.Count -ne 0 )
216- {
201+ else {
202+ if ($dataDiskBlobURIs.Count -ne 0 ) {
217203 $dataDisksJSON = ' "DataDisks":['
218204 $i = 0
219- foreach ($dataDiskBlobURI in $dataDiskBlobURIs )
220- {
221- if ($i -ne 0 )
222- {
205+ foreach ($dataDiskBlobURI in $dataDiskBlobURIs ) {
206+ if ($i -ne 0 ) {
223207 $dataDisksJSON = $dataDisksJSON + ' , '
224208 }
225209
@@ -237,13 +221,11 @@ Function Add-VMImage{
237221
238222 $propertyBody = $osDiskJSON
239223
240- if (! [string ]::IsNullOrEmpty($dataDisksJson ))
241- {
224+ if (! [string ]::IsNullOrEmpty($dataDisksJson )) {
242225 $propertyBody = $propertyBody + ' , ' + $dataDisksJson
243226 }
244227
245- if (! [string ]::IsNullOrEmpty($detailsJson ))
246- {
228+ if (! [string ]::IsNullOrEmpty($detailsJson )) {
247229 $propertyBody = $propertyBody + ' , ' + $detailsJson
248230 }
249231
@@ -256,15 +238,12 @@ Function Add-VMImage{
256238 $platformImage = Invoke-RestMethod - Method GET - Uri $uri - ContentType ' application/json' - Headers $Headers
257239
258240 $downloadingStatusCheckCount = 0
259- while ($platformImage.Properties.ProvisioningState -ne ' Succeeded' )
260- {
261- if ($platformImage.Properties.ProvisioningState -eq ' Failed' )
262- {
241+ while ($platformImage.Properties.ProvisioningState -ne ' Succeeded' ) {
242+ if ($platformImage.Properties.ProvisioningState -eq ' Failed' ) {
263243 Write-Error - Message " VM image download failed." - ErrorAction Stop
264244 }
265245
266- if ($platformImage.Properties.ProvisioningState -eq ' Canceled' )
267- {
246+ if ($platformImage.Properties.ProvisioningState -eq ' Canceled' ) {
268247 Write-Error - Message " VM image download was canceled." - ErrorAction Stop
269248 }
270249
@@ -278,8 +257,7 @@ Function Add-VMImage{
278257 $platformImage = Invoke-RestMethod - Method GET - Uri $uri - ContentType ' application/json' - Headers $Headers
279258 }
280259
281- if ($CreateGalleryItem -eq $true -And $platformImage.Properties.ProvisioningState -eq ' Succeeded' )
282- {
260+ if ($CreateGalleryItem -eq $true -And $platformImage.Properties.ProvisioningState -eq ' Succeeded' ) {
283261 # reaquire storage account context
284262 Set-AzureRmCurrentStorageAccount - StorageAccountName $storageAccountName - ResourceGroupName $resourceGroupName
285263 $container = Get-AzureStorageContainer - Name $containerName - ErrorAction SilentlyContinue
@@ -427,7 +405,7 @@ function New-Server2016VMImage {
427405 )
428406 $tmpfile = New-TemporaryFile
429407 " create vdisk FILE=`" $VHDPath `" TYPE=EXPANDABLE MAXIMUM=$VHDSizeInMB " |
430- Out-File - FilePath $tmpfile.FullName - Encoding ascii
408+ Out-File - FilePath $tmpfile.FullName - Encoding ascii
431409
432410 Write-Verbose - Message " Creating VHD at: $VHDPath of size: $VHDSizeInMB MB"
433411 diskpart.exe / s $tmpfile.FullName | Out-Null
@@ -595,13 +573,11 @@ function New-Server2016VMImage {
595573 Write-Verbose - Message " Server Core VHD already found."
596574 }
597575
598- if ($CreateGalleryItem )
599- {
576+ if ($CreateGalleryItem ) {
600577 $description = " This evaluation image should not be used for production workloads."
601578 Add-VMImage - sku $sku - osDiskLocalPath $ImagePath @PublishArguments - title " Windows Server 2016 Datacenter Core Eval" - description $description - CreateGalleryItem $CreateGalleryItem
602579 }
603- else
604- {
580+ else {
605581 Add-VMImage - sku $sku - osDiskLocalPath $ImagePath @PublishArguments - CreateGalleryItem $CreateGalleryItem
606582 }
607583 } catch {
@@ -627,13 +603,11 @@ function New-Server2016VMImage {
627603 }else {
628604 Write-Verbose - Message " Server Full VHD already found."
629605 }
630- if ($CreateGalleryItem )
631- {
606+ if ($CreateGalleryItem ) {
632607 $description = " This evaluation image should not be used for production workloads."
633608 Add-VMImage - sku $sku - osDiskLocalPath $ImagePath @PublishArguments - title " Windows Server 2016 Datacenter Eval" - description $description - CreateGalleryItem $CreateGalleryItem
634609 }
635- else
636- {
610+ else {
637611 Add-VMImage - sku $sku - osDiskLocalPath $ImagePath @PublishArguments - CreateGalleryItem $CreateGalleryItem
638612 }
639613 } catch {
@@ -662,76 +636,88 @@ Function CreateGalleyItem{
662636 [string ] $title ,
663637 [string ] $description
664638 )
665- $workdir = ' {0}\{1}' -f $env: TEMP , [System.Guid ]::NewGuid().ToString()
666- New-Item $workdir - ItemType Directory | Out-Null
667- $basePath = (Get-Module AzureStack.ComputeAdmin).ModuleBase
668- $compressedGalleryItemPath = Join-Path $basePath ' CustomizedVMGalleryItem.azpkg'
669- Copy-Item - Path $compressedGalleryItemPath - Destination " $workdir \CustomizedVMGalleryItem.zip"
670- $extractedGalleryItemPath = Join-Path $workdir ' galleryItem'
671- New-Item - ItemType directory - Path $extractedGalleryItemPath | Out-Null
672- expand-archive - Path " $workdir \CustomizedVMGalleryItem.zip" - DestinationPath $extractedGalleryItemPath - Force
639+ $workdir = ' {0}\{1}' -f $env: TEMP , [System.Guid ]::NewGuid().ToString()
640+ New-Item $workdir - ItemType Directory | Out-Null
641+ $basePath = (Get-Module AzureStack.ComputeAdmin).ModuleBase
642+ $compressedGalleryItemPath = Join-Path $basePath ' CustomizedVMGalleryItem.azpkg'
643+ Copy-Item - Path $compressedGalleryItemPath - Destination " $workdir \CustomizedVMGalleryItem.zip"
644+ $extractedGalleryItemPath = Join-Path $workdir ' galleryItem'
645+ New-Item - ItemType directory - Path $extractedGalleryItemPath | Out-Null
646+ expand-archive - Path " $workdir \CustomizedVMGalleryItem.zip" - DestinationPath $extractedGalleryItemPath - Force
673647
674- $extractedName = ' MarketplaceItem.zip'
675- Invoke-WebRequest - Uri http:// www.aka.ms/ azurestackmarketplaceitem - OutFile " $workdir \MarketplaceItem.zip"
676- Expand-Archive - Path " $workdir \MarketplaceItem.zip" - DestinationPath $workdir - Force
677-
678- # region UIDef
679- $createUIDefinitionPath = Join-Path $extractedGalleryItemPath ' DeploymentTemplates\CreateUIDefinition.json'
680- $JSON = Get-Content $createUIDefinitionPath | Out-String | ConvertFrom-Json
681- $JSON.parameters.osPlatform = $osType
682- $JSON.parameters.imageReference.publisher = $publisher
683- $JSON.parameters.imageReference.offer = $offer
684- $JSON.parameters.imageReference.sku = $sku
685- $JSON | ConvertTo-Json - Compress| set-content $createUIDefinitionPath
686- # endregion
687-
688- # region Manifest
689- $manifestPath = Join-Path $extractedGalleryItemPath ' manifest.json'
690- $JSON = Get-Content $manifestPath | Out-String | ConvertFrom-Json
691-
692- if (! $title )
693- {
694- $title = " {0}-{1}-{2}" -f $publisher , $offer , $sku
648+ $extractedName = ' MarketplaceItem.zip'
649+ $retryAttempts = 1
650+ while ($retryAttempts -le 5 ) {
651+ try {
652+ Write-Verbose - Message " Downloading Azure Stack Marketplace Item Generator Attempt $retryAttempts " - Verbose
653+ Invoke-WebRequest - Uri http:// www.aka.ms/ azurestackmarketplaceitem - OutFile " $workdir \MarketplaceItem.zip"
654+ break
695655 }
696- $name = " $offer$sku "
697- # Remove periods so that the offer and sku can be part of the MarketplaceItem name
698- $name = $name -replace " \." , " -"
699- $JSON.name = $name
700- $JSON.publisher = $publisher
701- $JSON.version = $version
702- $JSON.displayName = $title
703- $JSON.publisherDisplayName = $publisher
704- $JSON.publisherLegalName = $publisher
705- $JSON | ConvertTo-Json - Compress| set-content $manifestPath
706-
707- # endregion
708-
709- # region Strings
710- if (! $description )
711- {
712- $description = " Create a virtual machine from a VM image. Publisher: {0}, Offer: {1}, Sku:{2}, Version: {3}" -f $publisher , $offer , $sku , $version
656+ catch {
657+ $retryAttempts = $retryAttempts + 1
658+ if ($retryAttempts -ge 5 ){
659+ Write-Error " Failed to download Azure Stack Marketplace Item Generator" - ErrorAction Stop
660+ }
713661 }
714- $stringsPath = Join-Path $extractedGalleryItemPath ' strings\resources.resjson'
715- $JSON = Get-Content $stringsPath | Out-String | ConvertFrom-Json
716- $JSON.longSummary = $description
717- $JSON.description = $description
718- $JSON.summary = $description
719- $JSON | ConvertTo-Json - Compress | set-content $stringsPath
720- # endregion
721-
722- $extractedGalleryPackagerExePath = Join-Path $workdir " Azure Stack Marketplace Item Generator and Sample\AzureGalleryPackageGenerator"
723- $galleryItemName = $publisher + " ." + $name + " ." + $version + " .azpkg"
724- $currentPath = $pwd
725- cd $extractedGalleryPackagerExePath
726- .\AzureGalleryPackager.exe package - m $manifestPath - o $workdir
727- cd $currentPath
662+ }
728663
729- # cleanup
730- Remove-Item $extractedGalleryItemPath - Recurse - Force
731- Remove-Item " $workdir \Azure Stack Marketplace Item Generator and Sample" - Recurse - Force
732- Remove-Item " $workdir \CustomizedVMGalleryItem.zip"
733- Remove-Item " $workdir \MarketplaceItem.zip"
734- $azpkg = ' {0}\{1}' -f $workdir , $galleryItemName
735- return Get-Item - LiteralPath $azpkg
664+ Expand-Archive - Path " $workdir \MarketplaceItem.zip" - DestinationPath $workdir - Force
665+
666+ # region UIDef
667+ $createUIDefinitionPath = Join-Path $extractedGalleryItemPath ' DeploymentTemplates\CreateUIDefinition.json'
668+ $JSON = Get-Content $createUIDefinitionPath | Out-String | ConvertFrom-Json
669+ $JSON.parameters.osPlatform = $osType
670+ $JSON.parameters.imageReference.publisher = $publisher
671+ $JSON.parameters.imageReference.offer = $offer
672+ $JSON.parameters.imageReference.sku = $sku
673+ $JSON | ConvertTo-Json - Compress| set-content $createUIDefinitionPath
674+ # endregion
675+
676+ # region Manifest
677+ $manifestPath = Join-Path $extractedGalleryItemPath ' manifest.json'
678+ $JSON = Get-Content $manifestPath | Out-String | ConvertFrom-Json
679+
680+ if (! $title ) {
681+ $title = " {0}-{1}-{2}" -f $publisher , $offer , $sku
682+ }
683+ $name = " $offer$sku "
684+ # Remove periods so that the offer and sku can be part of the MarketplaceItem name
685+ $name = $name -replace " \." , " -"
686+ $JSON.name = $name
687+ $JSON.publisher = $publisher
688+ $JSON.version = $version
689+ $JSON.displayName = $title
690+ $JSON.publisherDisplayName = $publisher
691+ $JSON.publisherLegalName = $publisher
692+ $JSON | ConvertTo-Json - Compress| set-content $manifestPath
693+
694+ # endregion
695+
696+ # region Strings
697+ if (! $description ) {
698+ $description = " Create a virtual machine from a VM image. Publisher: {0}, Offer: {1}, Sku:{2}, Version: {3}" -f $publisher , $offer , $sku , $version
736699 }
700+ $stringsPath = Join-Path $extractedGalleryItemPath ' strings\resources.resjson'
701+ $JSON = Get-Content $stringsPath | Out-String | ConvertFrom-Json
702+ $JSON.longSummary = $description
703+ $JSON.description = $description
704+ $JSON.summary = $description
705+ $JSON | ConvertTo-Json - Compress | set-content $stringsPath
706+ # endregion
707+
708+ $extractedGalleryPackagerExePath = Join-Path $workdir " Azure Stack Marketplace Item Generator and Sample\AzureGalleryPackageGenerator"
709+ $galleryItemName = $publisher + " ." + $name + " ." + $version + " .azpkg"
710+ $currentPath = $pwd
711+ cd $extractedGalleryPackagerExePath
712+ .\AzureGalleryPackager.exe package - m $manifestPath - o $workdir
713+ cd $currentPath
714+
715+ # cleanup
716+ Remove-Item $extractedGalleryItemPath - Recurse - Force
717+ Remove-Item " $workdir \Azure Stack Marketplace Item Generator and Sample" - Recurse - Force
718+ Remove-Item " $workdir \CustomizedVMGalleryItem.zip"
719+ Remove-Item " $workdir \MarketplaceItem.zip"
720+ $azpkg = ' {0}\{1}' -f $workdir , $galleryItemName
721+ return Get-Item - LiteralPath $azpkg
722+ }
737723
0 commit comments