Skip to content

Commit 036428f

Browse files
committed
Add Module
1 parent 37363c2 commit 036428f

2 files changed

Lines changed: 163 additions & 1 deletion

File tree

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# See LICENSE.txt in the project root for license information.
3+
4+
<#
5+
.SYNOPSIS
6+
List all Azure Marketplace Items available for syndication and allows to download them
7+
Requires an Azure Stack System to be registered for the subscription used to login
8+
#>
9+
10+
function Sync-AzSOfflineMarketplaceItems{
11+
[CmdletBinding(DefaultParameterSetName='SyncOfflineAzsMarketplaceItems')]
12+
13+
Param(
14+
[Parameter(Mandatory=$false, ParameterSetName='SyncOfflineAzsMarketplaceItems')]
15+
[ValidateNotNullorEmpty()]
16+
[String] $Cloud = "AzureCloud",
17+
18+
[Parameter(Mandatory=$true, ParameterSetName='SyncOfflineAzsMarketplaceItems')]
19+
[ValidateNotNullorEmpty()]
20+
[String] $Destination,
21+
22+
[Parameter(Mandatory=$false, ParameterSetName='SyncOfflineAzsMarketplaceItems')]
23+
[ValidateNotNullorEmpty()]
24+
[String] $tenantid
25+
26+
)
27+
28+
29+
If ($tenantid.isPresent){
30+
$azureAccount = Add-AzureRmAccount -TenantId $tenantid
31+
}
32+
33+
else{
34+
35+
$azureAccount = Add-AzureRmAccount
36+
}
37+
38+
39+
$azureEnvironment = Get-AzureRmEnvironment -Name $Cloud
40+
41+
$subscription=Get-AzureRmSubscription
42+
$subscriptionId=$subscription[0].SubscriptionId
43+
44+
$resources=Get-AzureRmResource
45+
$resource=$resources.resourcename
46+
$registrations=$resource|where-object {$_ -like "AzureStack*"}
47+
$registration = $registrations[0]
48+
49+
# Retrieve the access token
50+
$tokens = [Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCache]::DefaultShared.ReadItems()
51+
$token = $tokens |Where Resource -EQ $azureEnvironment.ActiveDirectoryServiceEndpointResourceId |Where DisplayableId -EQ $azureAccount.Context.Account.Id |Sort ExpiresOn |Select -Last 1
52+
53+
54+
$uri1 = "$($azureEnvironment.ResourceManagerUrl.ToString().TrimEnd('/'))/subscriptions/$($subscriptionId.ToString())/resourceGroups/azurestack/providers/Microsoft.AzureStack/registrations/$($Registration.ToString())/products?api-version=2016-01-01"
55+
$Headers = @{ 'authorization'="Bearer $($Token.AccessToken)"}
56+
$products = (Invoke-RestMethod -Method GET -Uri $uri1 -Headers $Headers).value
57+
58+
59+
$Marketitems=foreach ($product in $products)
60+
{
61+
switch($product.properties.productKind)
62+
{
63+
'virtualMachine'
64+
{
65+
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+
})
74+
}
75+
76+
'virtualMachineExtension'
77+
{
78+
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+
})
87+
}
88+
89+
Default
90+
{
91+
Write-Warning "Unknown product kind '$_'"
92+
}
93+
}
94+
}
95+
96+
97+
98+
$Marketitems|Out-GridView -Title 'Azure Marketplace Items' -PassThru|foreach{
99+
100+
$productid=$_.id
101+
102+
# get name of azpkg
103+
$uri2 = "$($azureEnvironment.ResourceManagerUrl.ToString().TrimEnd('/'))/subscriptions/$($SubscriptionId.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
108+
109+
110+
# get download location for apzkg
111+
$uri3 = "$($azureEnvironment.ResourceManagerUrl.ToString().TrimEnd('/'))/subscriptions/$($SubscriptionId.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
114+
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)
120+
121+
{
122+
# download azpkg
123+
$azpkgsource = $downloadDetails.galleryPackageBlobSasUri
124+
$FileExists=Test-Path "$destination\$azpkgName.azpkg"
125+
If ($FileExists -eq $true) {Remove-Item "$destination\$azpkgName.azpkg" -force} else {
126+
New-Item "$destination\$azpkgName.azpkg"}
127+
$azpkgdestination = "$destination\$azpkgName.azpkg"
128+
Start-BitsTransfer -source $azpkgsource -destination $azpkgdestination -Priority High
129+
130+
# download vhd
131+
$vhdName = $productDetails.properties.galleryItemIdentity
132+
$vhdSource = $downloadDetails.properties.osDiskImage.sourceBlobSasUri
133+
$FileExists=Test-Path "$destination\$productid.vhd"
134+
If ($FileExists -eq $true) {Remove-Item "$destination\$productid.vhd" -force} else {
135+
New-Item "$destination\$productid.vhd" }
136+
$vhdDestination = "$destination\$productid.vhd"
137+
Start-BitsTransfer -source $vhdSource -destination $vhdDestination -Priority High
138+
}
139+
140+
else {
141+
$a.popup("Legal Terms not accpeted, canceling")
142+
}
143+
144+
}
145+
}
146+
147+
148+
149+
function Set-String {
150+
param (
151+
[parameter(mandatory=$true)]
152+
[long] $size
153+
)
154+
155+
if ($size -gt 1073741824) {
156+
return [string]([math]::Round($size / 1073741824)) + " GB"
157+
} elseif ($size -gt 1048576) {
158+
return [string]([math]::Round($size / 1048576)) + " MB"
159+
} else {return "<1 MB"}
160+
}
161+
162+
Export-ModuleMember -Function Sync-AzSOfflineMarketplaceItems

Syndication/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Import-Module .\AzureStack.MarketplaceSyndication.psm1
2828
Sync-AzSOfflineMarketplaceItems -destination c:\donwloadfolder
2929
```
3030

31-
##Optional PARAMETERS
31+
##Optional Parameters
3232

3333
Parameter: Cloud
3434
Default: AzureCloud

0 commit comments

Comments
 (0)