Last active
December 12, 2017 16:47
-
-
Save eosfor/7936a0d4638bce48daffc3b87632387b to your computer and use it in GitHub Desktop.
hack to use Azure Cosmos DB as an inventory for Azure VM Objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using assembly "C:\Users\<user>\AppData\Local\PackageManagement\NuGet\Packages\Microsoft.Azure.DocumentDB.1.19.1\lib\net45\Microsoft.Azure.Documents.Client.dll" | |
$client = [Microsoft.Azure.Documents.Client.DocumentClient]::new([Uri]("https://localhost:8081"),"C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==") | |
$db = $client.CreateDatabaseIfNotExistsAsync([Microsoft.Azure.Documents.Database]@{ID="azureinventory"}) | |
$docCollection = $client.CreateDocumentCollectionIfNotExistsAsync([Microsoft.Azure.Documents.Client.UriFactory]::CreateDatabaseUri("azureinventory"), | |
[Microsoft.Azure.Documents.DocumentCollection]@{id="inventory"}, | |
[Microsoft.Azure.Documents.Client.RequestOptions]@{OfferThroughput = 1000}) | |
$docCollectionURI = [Microsoft.Azure.Documents.Client.UriFactory]::CreateCollectionUri("azureinventory", "inventory") | |
$queryOptions = [Microsoft.Azure.Documents.Client.RequestOptions]@{OfferThroughput = 1000} | |
$hash = [Security.Cryptography.HashAlgorithm]::Create( "SHA1" ) | |
Get-AzureRmVM | % { | |
$oldID = $_.Id | |
$newID = [System.Convert]::ToBase64String([byte[]]$hash.ComputeHash($_.Id.ToCharArray())) #new ID is a SHA1 hash of azure ID value | |
#below hack id to change value of ID after conversion to JSON | |
#otherwise ResourceGroupName becomes null | |
$json = ($_ | Add-Member -Type NoteProperty -Name azureID -Value $oldID -Force -PassThru | ConvertTo-Json) -creplace "`"Id`"", '"id"' #this is to change parameter name ID to id | |
$json2 = (ConvertFrom-Json $json) | |
$json2.id = $newID | |
$o = [Newtonsoft.Json.Linq.JObject]($json2 | ConvertTo-Json) | |
$client.CreateDocumentAsync($docCollectionURI, $o , $options, $true) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment