Skip to content

Instantly share code, notes, and snippets.

@junichia
Created March 29, 2017 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junichia/87cb4e23e8b13421254b263d013dabe8 to your computer and use it in GitHub Desktop.
Save junichia/87cb4e23e8b13421254b263d013dabe8 to your computer and use it in GitHub Desktop.
param(
[parameter(Mandatory=$true)]
[string]$customernumber
)
$servicePrincipalConnection = Get-AutomationConnection -Name 'AzureRunAsConnection'
Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
$srcStorageAccount = Get-AutomationVariable -Name 'srcStorageAccount'
$srcStorageAccountResourceGroup = Get-AutomationVariable -Name 'srcStorageAccountResourceGroup'
$srcStorageContainerName = Get-AutomationVariable -Name 'srcStorageContainerName'
$srcVHD = Get-AutomationVariable -Name 'srcVHD'
$srcContext = Get-AzureRmStorageAccount `
-Name $srcStorageAccount `
-ResourceGroupName $srcStorageAccountResourceGroup
$destResourceGroupName = (Get-AutomationVariable -Name 'ResourcePrefix') + $CustomerNumber
$destStorageAccountName = $destResourceGroupName.ToLower()
$destStorageContainerName = Get-AutomationVariable -Name 'destStorageContainerName'
$destLocation = Get-AutomationVariable -Name 'destLocation'
$destVHD = $destStorageAccountName + (Get-AutomationVariable -Name 'destVHD')
New-AzureRmResourceGroup `
-Location $destLocation `
-Name $destResourceGroupName `
-ErrorAction Continue `
-Force
New-AzureRmStorageAccount `
-ResourceGroupName $destResourceGroupName `
-AccountName $destStorageAccountName `
-Location $destLocation `
-Type "Standard_LRS" `
-ErrorAction Continue
$destContext = Get-AzureRmStorageAccount `
-Name $destStorageAccountName `
-ResourceGroupName $destResourceGroupName
New-AzureStorageContainer -Name $destStorageContainerName -Context $destContext.Context -ErrorAction Continue
$blob1 = Start-AzureStorageBlobCopy `
-SrcContainer $srcStorageContainerName `
-SrcBlob $srcVHD `
-srcContext $srcContext.Context `
-DestContainer $destStorageContainerName `
-DestBlob $destVHD `
-DestContext $destContext.Context -Force
$status = $blob1 | Get-AzureStorageBlobCopyState
While($status.Status -eq "Pending")
{
$status = $blob1 | Get-AzureStorageBlobCopyState
Start-Sleep 10
$status
}
## deployment
$deploymentName = $destResourceGroupName
$TemplateUri = Get-AutomationVariable -Name 'TemplateUri'
$AdminCredential = Get-AutomationPSCredential -Name 'cloudadmin'
$AdminUserName = $AdminCredential.userName
$Ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($AdminCredential.password)
$AdminPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($Ptr)
$parameters = @{
"adminUserName"=$AdminUserName
"adminPassword"=$AdminPassword
"CustomerNumber"=$customernumber
}
New-AzureRmResourceGroupDeployment `
-Name $deploymentName `
-ResourceGroupName $destResourceGroupName `
-TemplateUri $TemplateUri `
-TemplateParameterObject $parameters `
-mode Incremental
$VMName = "ocms" + $customernumber
$AzureVMResourceGroup = "ocms" + $customernumber
$ResourceGroupName = "rm-pharaojp"
$AutomationAccountName = "automationaccount2"
Register-AzureRmAutomationDscNode `
-AzureVMName $VMName `
-NodeConfigurationName CreateNewADForest.localhost `
-ConfigurationMode ApplyOnly `
-ActionAfterReboot ContinueConfiguration `
-ResourceGroupName $ResourceGroupName `
-RebootNodeIfNeeded $true `
-AzureVMResourceGroup $AzureVMResourceGroup `
-AutomationAccountName $AutomationAccountName `
-AllowModuleOverwrite $true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment