Skip to content

Instantly share code, notes, and snippets.

@neerajks77
Last active February 4, 2024 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save neerajks77/d1e278384d8c1f11580c5b134c2788d4 to your computer and use it in GitHub Desktop.
Save neerajks77/d1e278384d8c1f11580c5b134c2788d4 to your computer and use it in GitHub Desktop.
This PowerShell is invoked when a new file is uploaded to the blob container. It gets the data and passes on to the other PowerShell for deployment of virtual machine. This approach has been taken to make the code more modular.
$Creds = Get-AutomationPSCredential -Name 'Creds'
$TenantID = Get-AutomationVariable -Name 'TenantID'
Connect-AzAccount -ServicePrincipal -Credential $Creds -Tenant $TenantID
$vResourceGroupname = "atcsl"
$vAutomationAccountName = "atcslautomation"
$vaStorageAccount = Get-AutomationVariable -Name "StorageAccount"
$StartTime = Get-Date
$EndTime = $startTime.AddHours(1.0)
$stgAccount = Get-AzStorageAccount -Name $vaStorageAccount -ResourceGroupName $vResourceGroupname
$SASToken = New-AzStorageAccountSASToken -Service 'Blob' -ResourceType Container,Object -Permission "racwdlup" -startTime $StartTime -ExpiryTime $EndTime -Context $stgAccount.Context
$stgcontext = New-AzStorageContext -storageAccountName $stgAccount.StorageAccountName -SasToken $SASToken
try {Import-Module ImportExcel} catch {throw ; return}
$Path = "$env:TEMP\VMDetails.xlsx"
Remove-Item $Path -ErrorAction SilentlyContinue
$blob = Get-AzStorageBlob -Container 'createvm' -Blob 'CreateVM.xlsx' -Context $stgcontext
$tmp = Get-AzStorageBlobContent -CloudBlob $blob.ICloudBlob -Destination $Path -Context $stgcontext #.Context.FileEndPoint + "atcslfileshare"
#import-module psexcel #it wasn't auto loading on my machine
#$details = new-object System.Collections.ArrayList
$ExcelValue = Import-Excel -Path $Path -WorksheetName 'Sheet1' -StartRow 1 -StartColumn 1
#$Data = $ExcelValue | ConvertTo-Json
foreach($vmdetail in $ExcelValue) {
#$vmdetail
$VMParameter = @(
$vmdetail.Name,
$vmdetail.Computername,
$vmdetail.Location,
$vmdetail.RGName,
$vmdetail.LocalAdminUser,
$vmdetail.Password,
$vmdetail.Size,
$vmdetail.PublisherName,
$vmdetail.OfferName,
$vmdetail.SKU
)
#$VMParameter
.\CreateVirtualMachineActionRunbook.ps1 $VMParameter
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment