Skip to content

Commit c315440

Browse files
yingqunpkuknithinc
authored andcommitted
China Cloud support and a few bug fixes (#412)
* China Cloud support * minor fix * deployment parameter update to -InfraAzureEnvironment from -AzureEnvironment * if else tree instead of nested conditions for easier readability and the option to change parameters * Add China Cloud support; Add VM Extension support; Fixed a typo.
1 parent 44cab70 commit c315440

File tree

1 file changed

+113
-94
lines changed

1 file changed

+113
-94
lines changed

Syndication/AzureStack.MarketplaceSyndication.psm1

Lines changed: 113 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,43 @@
77
Requires an Azure Stack System to be registered for the subscription used to login
88
#>
99

10-
function Sync-AzSOfflineMarketplaceItem{
11-
[CmdletBinding(DefaultParameterSetName='SyncOfflineAzsMarketplaceItem')]
10+
function Sync-AzSOfflineMarketplaceItem {
11+
[CmdletBinding(DefaultParameterSetName = 'SyncOfflineAzsMarketplaceItem')]
1212

13-
Param(
14-
[Parameter(Mandatory=$false, ParameterSetName='SyncOfflineAzsMarketplaceItem')]
13+
Param(
14+
[Parameter(Mandatory = $false, ParameterSetName = 'SyncOfflineAzsMarketplaceItem')]
1515
[ValidateNotNullorEmpty()]
1616
[String] $Cloud = "AzureCloud",
1717

18-
[Parameter(Mandatory=$true, ParameterSetName='SyncOfflineAzsMarketplaceItem')]
18+
[Parameter(Mandatory = $true, ParameterSetName = 'SyncOfflineAzsMarketplaceItem')]
1919
[ValidateNotNullorEmpty()]
2020
[String] $Destination,
2121

22-
[Parameter(Mandatory=$true, ParameterSetName='SyncOfflineAzsMarketplaceItem')]
22+
[Parameter(Mandatory = $true, ParameterSetName = 'SyncOfflineAzsMarketplaceItem')]
2323
[ValidateNotNullorEmpty()]
2424
[String] $AzureTenantID,
2525

26-
[Parameter(Mandatory=$true, ParameterSetName='SyncOfflineAzsMarketplaceItem')]
26+
[Parameter(Mandatory = $true, ParameterSetName = 'SyncOfflineAzsMarketplaceItem')]
2727
[ValidateNotNullorEmpty()]
2828
[String] $AzureSubscriptionID
2929

30-
)
30+
)
3131

3232

3333

34-
$azureAccount = Add-AzureRmAccount -subscriptionid $AzureSubscriptionID -TenantId $AzureTenantID
34+
$azureAccount = Add-AzureRmAccount -subscriptionid $AzureSubscriptionID -TenantId $AzureTenantID -Environment $Cloud
3535

3636
$azureEnvironment = Get-AzureRmEnvironment -Name $Cloud
3737

38-
$resources=Get-AzureRmResource
39-
$resource=$resources.resourcename
40-
$registrations=$resource|where-object {$_ -like "AzureStack*"}
38+
$resources = Get-AzureRmResource
39+
$resource = $resources.resourcename
40+
$registrations = $resource|where-object {$_ -like "AzureStack*"}
4141
if ($registrations.count -gt 1) {
4242
$Registration = $registrations[0]
43-
}
44-
else {
43+
}
44+
else {
4545
$Registration = $registrations
46-
}
46+
}
4747

4848

4949
# Retrieve the access token
@@ -52,121 +52,140 @@ function Sync-AzSOfflineMarketplaceItem{
5252

5353

5454
$uri1 = "$($azureEnvironment.ResourceManagerUrl.ToString().TrimEnd('/'))/subscriptions/$($AzureSubscriptionID.ToString())/resourceGroups/azurestack/providers/Microsoft.AzureStack/registrations/$($Registration.ToString())/products?api-version=2016-01-01"
55-
$Headers = @{ 'authorization'="Bearer $($Token.AccessToken)"}
55+
$Headers = @{ 'authorization' = "Bearer $($Token.AccessToken)"}
5656
$products = (Invoke-RestMethod -Method GET -Uri $uri1 -Headers $Headers).value
5757

5858

59-
$Marketitems=foreach ($product in $products)
60-
{
61-
switch($product.properties.productKind)
62-
{
63-
'virtualMachine'
64-
{
59+
$Marketitems = foreach ($product in $products) {
60+
switch ($product.properties.productKind) {
61+
'virtualMachine' {
6562
Write-output ([pscustomobject]@{
66-
Id = $product.name.Split('/')[-1]
67-
Type = "Virtual Machine"
68-
Name = $product.properties.displayName
69-
Description = $product.properties.description
70-
Publisher = $product.properties.publisherDisplayName
71-
Version = $product.properties.offerVersion
72-
Size = Set-String -size $product.properties.payloadLength
73-
})
63+
Id = $product.name.Split('/')[-1]
64+
Type = "Virtual Machine"
65+
Name = $product.properties.displayName
66+
Description = $product.properties.description
67+
Publisher = $product.properties.publisherDisplayName
68+
Version = $product.properties.offerVersion
69+
Size = Set-String -size $product.properties.payloadLength
70+
})
7471
}
7572

76-
'virtualMachineExtension'
77-
{
73+
'virtualMachineExtension' {
7874
Write-output ([pscustomobject]@{
79-
Id = $product.name.Split('/')[-1]
80-
Type = "Virtual Machine Extension"
81-
Name = $product.properties.displayName
82-
Description = $product.properties.description
83-
Publisher = $product.properties.publisherDisplayName
84-
Version = $product.properties.productProperties.version
85-
Size = Set-String -size $product.properties.payloadLength
86-
})
75+
Id = $product.name.Split('/')[-1]
76+
Type = "Virtual Machine Extension"
77+
Name = $product.properties.displayName
78+
Description = $product.properties.description
79+
Publisher = $product.properties.publisherDisplayName
80+
Version = $product.properties.productProperties.version
81+
Size = Set-String -size $product.properties.payloadLength
82+
})
8783
}
8884

89-
Default
90-
{
85+
Default {
9186
Write-Warning "Unknown product kind '$_'"
9287
}
9388
}
9489
}
9590

9691

9792

98-
$Marketitems|Out-GridView -Title 'Azure Marketplace Items' -PassThru|foreach{
93+
$Marketitems|Out-GridView -Title 'Azure Marketplace Items' -PassThru|foreach {
9994

100-
$productid=$_.id
95+
$productid = $_.id
10196

102-
# get name of azpkg
103-
$uri2 = "$($azureEnvironment.ResourceManagerUrl.ToString().TrimEnd('/'))/subscriptions/$($AzureSubscriptionID.ToString())/resourceGroups/azurestack/providers/Microsoft.AzureStack/registrations/$Registration/products/$($productid)?api-version=2016-01-01"
104-
Write-Debug $URI2
105-
$Headers = @{ 'authorization'="Bearer $($Token.AccessToken)"}
106-
$productDetails = Invoke-RestMethod -Method GET -Uri $uri2 -Headers $Headers
107-
$azpkgName = $productDetails.properties.galleryItemIdentity
97+
# get name of azpkg
98+
$uri2 = "$($azureEnvironment.ResourceManagerUrl.ToString().TrimEnd('/'))/subscriptions/$($AzureSubscriptionID.ToString())/resourceGroups/azurestack/providers/Microsoft.AzureStack/registrations/$Registration/products/$($productid)?api-version=2016-01-01"
99+
Write-Debug $URI2
100+
$Headers = @{ 'authorization' = "Bearer $($Token.AccessToken)"}
101+
$productDetails = Invoke-RestMethod -Method GET -Uri $uri2 -Headers $Headers
102+
$azpkgName = $productDetails.properties.galleryItemIdentity
108103

109104

110-
# get download location for apzkg
111-
$uri3 = "$($azureEnvironment.ResourceManagerUrl.ToString().TrimEnd('/'))/subscriptions/$($AzureSubscriptionID.ToString())/resourceGroups/azurestack/providers/Microsoft.AzureStack/registrations/$Registration/products/$productid/listDetails?api-version=2016-01-01"
112-
$uri3
113-
$downloadDetails = Invoke-RestMethod -Method POST -Uri $uri3 -Headers $Headers
105+
# get download location for apzkg
106+
$uri3 = "$($azureEnvironment.ResourceManagerUrl.ToString().TrimEnd('/'))/subscriptions/$($AzureSubscriptionID.ToString())/resourceGroups/azurestack/providers/Microsoft.AzureStack/registrations/$Registration/products/$productid/listDetails?api-version=2016-01-01"
107+
$uri3
108+
$downloadDetails = Invoke-RestMethod -Method POST -Uri $uri3 -Headers $Headers
109+
110+
#Create Legal Terms POPUP
111+
$a = new-object -comobject wscript.shell
112+
$intAnswer = $a.popup($productDetails.properties.description, `
113+
0, "Legal Terms", 4)
114+
If ($intAnswer -eq 6) {
115+
# download azpkg
116+
$azpkgsource = $downloadDetails.galleryPackageBlobSasUri
117+
$FileExists = Test-Path "$destination\$azpkgName.azpkg"
118+
$DestinationCheck = Test-Path $destination
119+
If ($DestinationCheck -eq $false) {
120+
new-item -ItemType Directory -force $destination
121+
}
122+
else {}
114123

115-
#Create Legal Terms POPUP
116-
$a = new-object -comobject wscript.shell
117-
$intAnswer = $a.popup($productDetails.properties.description, `
118-
0,"Legal Terms",4)
119-
If ($intAnswer -eq 6)
124+
If ($FileExists -eq $true) {Remove-Item "$destination\$azpkgName.azpkg" -force} else {
125+
New-Item "$destination\$azpkgName.azpkg"
126+
}
127+
$azpkgdestination = "$destination\$azpkgName.azpkg"
128+
Start-BitsTransfer -source $azpkgsource -destination $azpkgdestination -Priority High
129+
130+
switch ($downloadDetails.productKind) {
131+
'virtualMachine' {
132+
133+
# download vhd
134+
$vhdName = $productDetails.properties.galleryItemIdentity
135+
$vhdSource = $downloadDetails.properties.osDiskImage.sourceBlobSasUri
136+
If ([string]::IsNullOrEmpty($vhdsource)) {exit} else {
137+
$FileExists = Test-Path "$destination\$vhdName.vhd"
138+
If ($FileExists -eq $true) {Remove-Item "$destination\$vhdName.vhd" -force} else {
139+
New-Item "$destination\$vhdName.vhd"
140+
}
141+
$vhdDestination = "$destination\$vhdName.vhd"
120142

121-
{
122-
# download azpkg
123-
$azpkgsource = $downloadDetails.galleryPackageBlobSasUri
124-
$FileExists=Test-Path "$destination\$azpkgName.azpkg"
125-
$DestinationCheck=Test-Path $destination
126-
If ($DestinationCheck -eq $false)
127-
{
128-
new-item -ItemType Directory -force $destination} else{}
129-
130-
If ($FileExists -eq $true) {Remove-Item "$destination\$azpkgName.azpkg" -force} else {
131-
New-Item "$destination\$azpkgName.azpkg"}
132-
$azpkgdestination = "$destination\$azpkgName.azpkg"
133-
Start-BitsTransfer -source $azpkgsource -destination $azpkgdestination -Priority High
143+
Start-BitsTransfer -source $vhdSource -destination $vhdDestination -Priority High
144+
}
145+
}
146+
'virtualMachineExtension' {
147+
# download zip
148+
$zipName = $productDetails.properties.galleryItemIdentity
149+
$zipsource = $downloadDetails.properties.sourceBlob.uri
150+
If ([string]::IsNullOrEmpty($zipsource)) {exit} else {
151+
$FileExists = Test-Path "$destination\$zipName.zip"
152+
If ($FileExists -eq $true) {Remove-Item "$destination\$zipName.zip" -force} else {
153+
New-Item "$destination\$zipName.zip"
154+
}
155+
$zipDestination = "$destination\$zipName.zip"
134156

157+
Start-BitsTransfer -source $zipsource -destination $zipDestination -Priority High
158+
}
159+
}
135160

161+
Default {
162+
Write-Warning "Unknown product kind '$_'"
163+
}
164+
}
165+
}
136166

137-
# download vhd
138-
$vhdName = $productDetails.properties.galleryItemIdentity
139-
$vhdSource = $downloadDetails.properties.osDiskImage.sourceBlobSasUri
140-
If ([string]::IsNullOrEmpty($vhdsource)) {exit} else {
141-
$FileExists=Test-Path "$destination\$productid.vhd"
142-
If ($FileExists -eq $true) {Remove-Item "$destination\$productid.vhd" -force} else {
143-
New-Item "$destination\$productid.vhd" }
144-
$vhdDestination = "$destination\$productid.vhd"
145-
146-
Start-BitsTransfer -source $vhdSource -destination $vhdDestination -Priority High
147-
}
148-
}
149-
150-
else {
151-
$a.popup("Legal Terms not accpeted, canceling")
152-
}
167+
else {
168+
$a.popup("Legal Terms not accepted, canceling")
169+
}
153170

154-
}
171+
}
155172
}
156173

157174

158175

159176
function Set-String {
160177
param (
161-
[parameter(mandatory=$true)]
162-
[long] $size
163-
)
178+
[parameter(mandatory = $true)]
179+
[long] $size
180+
)
164181

165182
if ($size -gt 1073741824) {
166183
return [string]([math]::Round($size / 1073741824)) + " GB"
167-
} elseif ($size -gt 1048576) {
184+
}
185+
elseif ($size -gt 1048576) {
168186
return [string]([math]::Round($size / 1048576)) + " MB"
169-
} else {return "<1 MB"}
187+
}
188+
else {return "<1 MB"}
170189
}
171190

172191
Export-ModuleMember -Function Sync-AzSOfflineMarketplaceItem

0 commit comments

Comments
 (0)