Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Connect-AzureRmSubscription (
- $AzureSubscriptionId,
- $AzureSubscriptionUsername,
- $AzureSubscriptionPassword
- ) {
- [System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
- if ($null -eq $AzureSubscriptionId -or $null -eq $AzureSubscriptionUsername -or $null -eq $AzureSubscriptionPassword) {
- throw "The variable set for Azure Subscription information has not been added or the SubscriptionId, SubscriptionName, AzureSubscriptionUsername and AzureSubscriptionPassword variables have not been set for this environment"
- }
- $securepassword = $AzureSubscriptionPassword | ConvertTo-SecureString -AsPlainText -Force
- $credential = New-Object PSCredential -ArgumentList $AzureSubscriptionUsername, $securepassword
- [int]$retryCount = 5
- for ($i = 1; $i -le $retryCount; $i++) {
- if ($loginFailureException -or ($i -eq 1) ) {
- $loginFailureException = $null
- Write-Verbose ('Login to azure subscription {0}, with username {1}' -f $AzureSubscriptionId, $AzureSubscriptionUsername) -Verbose
- Login-AzureRmAccount -Credential $credential -SubscriptionId $AzureSubscriptionId -ErrorAction SilentlyContinue -ErrorVariable loginFailureException
- }
- if ($loginFailureException.Count -gt 0) {
- Write-Verbose "Attempt $i` Result: $loginFailureException" -Verbose
- }
- if (($i -lt $retryCount) -and ($loginFailureException.Count -gt 0)) {
- Write-Verbose "Retry..." -Verbose
- Start-Sleep -Seconds 3
- }
- if ($loginFailureException.Count -eq 0) {
- Write-Verbose "Login successful." -Verbose
- break
- }
- }
- if ($loginFailureException) {
- throw $loginFailureException
- }
- }
- function Set-AzureResourceGroup {
- param
- (
- [Parameter(Mandatory = $true)]
- [ValidateNotNullOrEmpty()]
- [string]$ResourceGroupName,
- [Parameter(Mandatory = $true)]
- [ValidateNotNullOrEmpty()]
- [string]$Location
- )
- try {
- $CheckResourceGroupExists = Get-AzureRmResourceGroup -Name $ResourceGroupName -ErrorAction $ErrorActionPreference
- }
- catch {
- if ($Error[0].Exception.Message -match "Provided resource group does not exist") {
- Write-Verbose ("Resource Group {0} not found, creating" -f $ResourceGroupName) -Verbose
- }
- else {
- throw ("Could not determine if resource group already exists`n{0}" -f $Error[0])
- }
- }
- if ($null -eq $CheckResourceGroupExists) {
- New-AzureRmResourceGroup -Name $ResourceGroupName -Location $Location
- }
- else {
- Write-Verbose ("Resource Group {0} already exists" -f $ResourceGroupName) -Verbose
- }
- }
- function Set-AzureDataLakeStoreAccount {
- param
- (
- [Parameter(Mandatory = $true)]
- [ValidateNotNullOrEmpty()]
- [string]$ResourceGroupName,
- [Parameter(Mandatory = $true)]
- [ValidateNotNullOrEmpty()]
- [string]$DataLakeStoreName,
- [Parameter(Mandatory = $true)]
- [ValidateNotNullOrEmpty()]
- [string]$Location
- )
- try {
- if ((Test-AzureRmDataLakeStoreAccount -Name $dataLakeStoreName -ResourceGroupName $ResourceGroupName) -eq $false) {
- Write-Verbose "Data Lake Storage Account does not exist. Creating..." -Verbose
- try {
- New-AzureRmDataLakeStoreAccount -ResourceGroupName $resourceGroupName -Name $dataLakeStoreName -Location $Location
- }
- catch {
- $_.Exception
- }
- finally {
- if ((Test-AzureRmDataLakeStoreAccount -Name $dataLakeStoreName) -eq $true) {
- Write-Verbose "Data Lake Storeage Account Created." -Verbose
- }
- else {
- Write-Error "Oh dear."
- Throw
- }
- }
- }
- else {
- Write-Verbose "Data Lake Storage Account Exists." -Verbose
- }
- }
- catch {
- $_.Exception
- }
- }
- function Set-AzureDataLakeStoreDirectory {
- param
- (
- [Parameter(Mandatory = $true)]
- [ValidateNotNullOrEmpty()]
- [string]$ResourceGroupName,
- [Parameter(Mandatory = $true)]
- [ValidateNotNullOrEmpty()]
- [string]$DataLakeStoreName,
- [Parameter(Mandatory = $true)]
- [ValidateNotNullOrEmpty()]
- [string]$Location,
- [Parameter(Mandatory = $true)]
- [ValidateNotNullOrEmpty()]
- [string]$DatalakeStoreDirectoryRoot,
- [Parameter(Mandatory = $true)]
- [ValidateNotNullOrEmpty()]
- [string]$DataLakeStoreNewDirectory
- )
- try {
- Write-Verbose "Testing Data Lake Store Directory exists..." -Verbose
- $DirectoryToCreate = Get-AzureRmDataLakeStoreChildItem -AccountName $dataLakeStoreName -Path $DatalakeStoreDirectoryRoot | Select-Object -ExpandProperty Name
- if ($null -eq $DirectoryToCreate) {
- Write-Verbose "no root path. Creating full path..." -Verbose
- $DirectoryPath = Join-Path $DataLakeStoreDirectoryRoot $DataLakeStoreNewDirectory
- New-AzureRmDataLakeStoreItem -Folder -AccountName $dataLakeStoreName -Path $DirectoryPath
- }
- elseif (($DirectoryToCreate -notcontains $DataLakeStoreNewDirectory) -eq $false) {
- Write-Verbose "Data Lake Storage Directory does not exist. Creating..." -Verbose
- try {
- $DirectoryPath = Join-Path $DataLakeStoreDirectoryRoot $DataLakeStoreNewDirectory
- New-AzureRmDataLakeStoreItem -Folder -AccountName $dataLakeStoreName -Path $DirectoryPath
- }
- catch {
- $_.Exception
- }
- finally {
- Get-AzureRmDataLakeStoreChildItem -AccountName $dataLakeStoreName -Path $DirectoryPath
- }
- }
- else {
- Write-Verbose "Data Lake Storage Directory exists. Exiting..." -Verbose
- }
- }
- catch {
- $_.Exception
- }
- }
- function Set-AzureDataLakeStoreItems {
- param
- (
- [Parameter(Mandatory = $true)]
- [ValidateNotNullOrEmpty()]
- [string]$ResourceGroupName,
- [Parameter(Mandatory = $true)]
- [ValidateNotNullOrEmpty()]
- [string]$DataLakeStoreName,
- [Parameter(Mandatory = $true)]
- [ValidateNotNullOrEmpty()]
- [string]$Location,
- [Parameter(Mandatory = $true)]
- [ValidateNotNullOrEmpty()]
- [string]$DirectoryPath,
- [Parameter(Mandatory = $true)]
- [ValidateNotNullOrEmpty()]
- [string]$SourceItemsPath
- )
- try
- {
- Write-Verbose "Importing Items To Data Lake Store..." -Verbose
- Import-AzureRmDataLakeStoreItem -AccountName $dataLakeStoreName -Path $SourceItemsPath -Destination $DirectoryPath
- }
- catch
- {
- Throw $_.Exception
- }
- }
- $ErrorActionPreference = "SilentlyContinue"
- $DataLakeStoreResourceGroup = "<rg>"
- $ResourceGroupLocation = "northeurope"
- $DataLakeStoreName = "<dl>"
- $dataLakeStoreLocation = "North Europe"
- $thisAzureSubscriptionId = "<guid>"
- $thisAzureSubscriptionUsername = "<uname>"
- $thisAzureSubscriptionPassword = "<pword>"
- $thisDataLakeStoreDirectoryRoot = "<root>"
- $thisDataLakeStoreNewDirectory = "<dir>"
- $DirectoryPathForitem = $thisDataLakeStoreDirectoryRoot + $thisDataLakeStoreNewDirectory
- $thisSourceItemsPath = "<folderToUpload>"
- Connect-AzureRmSubscription -AzureSubscriptionId $thisAzureSubscriptionId -AzureSubscriptionUsername $thisAzureSubscriptionUsername -AzureSubscriptionPassword $thisAzureSubscriptionPassword
- Set-AzureResourceGroup -ResourceGroupName $DataLakeStoreResourceGroup -Location $ResourceGroupLocation
- Set-AzureDataLakeStoreAccount -ResourceGroupName $DataLakeStoreResourceGroup -DataLakeStoreName $DataLakeStoreName -Location $dataLakeStoreLocation
- Set-AzureDataLakeStoreDirectory -ResourceGroupName $DataLakeStoreResourceGroup -DataLakeStoreName $DataLakeStoreName -Location $dataLakeStoreLocation -DatalakeStoreDirectoryRoot $thisDataLakeStoreDirectoryRoot -DataLakeStoreNewDirectory $thisDataLakeStoreNewDirectory
- Set-AzureDataLakeStoreItems -ResourceGroupName $DataLakeStoreResourceGroup -DataLakeStoreName $DataLakeStoreName -Location $dataLakeStoreLocation -DirectoryPath $DirectoryPathForitem -SourceItemsPath $thisSourceItemsPath
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement