Advertisement
Guest User

Untitled

a guest
Jul 14th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.24 KB | None | 0 0
  1. function Connect-AzureRmSubscription (
  2. $AzureSubscriptionId,
  3. $AzureSubscriptionUsername,
  4. $AzureSubscriptionPassword
  5.  
  6. ) {
  7.  
  8. [System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
  9. if ($null -eq $AzureSubscriptionId -or $null -eq $AzureSubscriptionUsername -or $null -eq $AzureSubscriptionPassword) {
  10. 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"
  11. }
  12. $securepassword = $AzureSubscriptionPassword | ConvertTo-SecureString -AsPlainText -Force
  13. $credential = New-Object PSCredential -ArgumentList $AzureSubscriptionUsername, $securepassword
  14. [int]$retryCount = 5
  15. for ($i = 1; $i -le $retryCount; $i++) {
  16.  
  17. if ($loginFailureException -or ($i -eq 1) ) {
  18. $loginFailureException = $null
  19. Write-Verbose ('Login to azure subscription {0}, with username {1}' -f $AzureSubscriptionId, $AzureSubscriptionUsername) -Verbose
  20. Login-AzureRmAccount -Credential $credential -SubscriptionId $AzureSubscriptionId -ErrorAction SilentlyContinue -ErrorVariable loginFailureException
  21. }
  22.  
  23. if ($loginFailureException.Count -gt 0) {
  24. Write-Verbose "Attempt $i` Result: $loginFailureException" -Verbose
  25. }
  26.  
  27. if (($i -lt $retryCount) -and ($loginFailureException.Count -gt 0)) {
  28. Write-Verbose "Retry..." -Verbose
  29. Start-Sleep -Seconds 3
  30. }
  31.  
  32. if ($loginFailureException.Count -eq 0) {
  33. Write-Verbose "Login successful." -Verbose
  34. break
  35. }
  36. }
  37. if ($loginFailureException) {
  38. throw $loginFailureException
  39. }
  40. }
  41.  
  42. function Set-AzureResourceGroup {
  43.  
  44. param
  45. (
  46. [Parameter(Mandatory = $true)]
  47. [ValidateNotNullOrEmpty()]
  48. [string]$ResourceGroupName,
  49. [Parameter(Mandatory = $true)]
  50. [ValidateNotNullOrEmpty()]
  51. [string]$Location
  52. )
  53. try {
  54. $CheckResourceGroupExists = Get-AzureRmResourceGroup -Name $ResourceGroupName -ErrorAction $ErrorActionPreference
  55. }
  56. catch {
  57. if ($Error[0].Exception.Message -match "Provided resource group does not exist") {
  58. Write-Verbose ("Resource Group {0} not found, creating" -f $ResourceGroupName) -Verbose
  59. }
  60. else {
  61. throw ("Could not determine if resource group already exists`n{0}" -f $Error[0])
  62. }
  63. }
  64.  
  65. if ($null -eq $CheckResourceGroupExists) {
  66. New-AzureRmResourceGroup -Name $ResourceGroupName -Location $Location
  67. }
  68.  
  69. else {
  70. Write-Verbose ("Resource Group {0} already exists" -f $ResourceGroupName) -Verbose
  71. }
  72. }
  73.  
  74. function Set-AzureDataLakeStoreAccount {
  75.  
  76. param
  77. (
  78. [Parameter(Mandatory = $true)]
  79. [ValidateNotNullOrEmpty()]
  80. [string]$ResourceGroupName,
  81. [Parameter(Mandatory = $true)]
  82. [ValidateNotNullOrEmpty()]
  83. [string]$DataLakeStoreName,
  84. [Parameter(Mandatory = $true)]
  85. [ValidateNotNullOrEmpty()]
  86. [string]$Location
  87. )
  88. try {
  89. if ((Test-AzureRmDataLakeStoreAccount -Name $dataLakeStoreName -ResourceGroupName $ResourceGroupName) -eq $false) {
  90. Write-Verbose "Data Lake Storage Account does not exist. Creating..." -Verbose
  91. try {
  92. New-AzureRmDataLakeStoreAccount -ResourceGroupName $resourceGroupName -Name $dataLakeStoreName -Location $Location
  93. }
  94. catch {
  95. $_.Exception
  96. }
  97. finally {
  98. if ((Test-AzureRmDataLakeStoreAccount -Name $dataLakeStoreName) -eq $true) {
  99. Write-Verbose "Data Lake Storeage Account Created." -Verbose
  100. }
  101. else {
  102. Write-Error "Oh dear."
  103. Throw
  104. }
  105. }
  106. }
  107. else {
  108. Write-Verbose "Data Lake Storage Account Exists." -Verbose
  109. }
  110. }
  111. catch {
  112. $_.Exception
  113. }
  114. }
  115.  
  116. function Set-AzureDataLakeStoreDirectory {
  117.  
  118. param
  119. (
  120. [Parameter(Mandatory = $true)]
  121. [ValidateNotNullOrEmpty()]
  122. [string]$ResourceGroupName,
  123. [Parameter(Mandatory = $true)]
  124. [ValidateNotNullOrEmpty()]
  125. [string]$DataLakeStoreName,
  126. [Parameter(Mandatory = $true)]
  127. [ValidateNotNullOrEmpty()]
  128. [string]$Location,
  129. [Parameter(Mandatory = $true)]
  130. [ValidateNotNullOrEmpty()]
  131. [string]$DatalakeStoreDirectoryRoot,
  132. [Parameter(Mandatory = $true)]
  133. [ValidateNotNullOrEmpty()]
  134. [string]$DataLakeStoreNewDirectory
  135. )
  136. try {
  137. Write-Verbose "Testing Data Lake Store Directory exists..." -Verbose
  138. $DirectoryToCreate = Get-AzureRmDataLakeStoreChildItem -AccountName $dataLakeStoreName -Path $DatalakeStoreDirectoryRoot | Select-Object -ExpandProperty Name
  139.  
  140. if ($null -eq $DirectoryToCreate) {
  141. Write-Verbose "no root path. Creating full path..." -Verbose
  142. $DirectoryPath = Join-Path $DataLakeStoreDirectoryRoot $DataLakeStoreNewDirectory
  143. New-AzureRmDataLakeStoreItem -Folder -AccountName $dataLakeStoreName -Path $DirectoryPath
  144. }
  145. elseif (($DirectoryToCreate -notcontains $DataLakeStoreNewDirectory) -eq $false) {
  146. Write-Verbose "Data Lake Storage Directory does not exist. Creating..." -Verbose
  147. try {
  148. $DirectoryPath = Join-Path $DataLakeStoreDirectoryRoot $DataLakeStoreNewDirectory
  149. New-AzureRmDataLakeStoreItem -Folder -AccountName $dataLakeStoreName -Path $DirectoryPath
  150. }
  151. catch {
  152. $_.Exception
  153. }
  154. finally {
  155. Get-AzureRmDataLakeStoreChildItem -AccountName $dataLakeStoreName -Path $DirectoryPath
  156. }
  157. }
  158. else {
  159. Write-Verbose "Data Lake Storage Directory exists. Exiting..." -Verbose
  160. }
  161. }
  162. catch {
  163. $_.Exception
  164. }
  165. }
  166.  
  167. function Set-AzureDataLakeStoreItems {
  168.  
  169. param
  170. (
  171. [Parameter(Mandatory = $true)]
  172. [ValidateNotNullOrEmpty()]
  173. [string]$ResourceGroupName,
  174. [Parameter(Mandatory = $true)]
  175. [ValidateNotNullOrEmpty()]
  176. [string]$DataLakeStoreName,
  177. [Parameter(Mandatory = $true)]
  178. [ValidateNotNullOrEmpty()]
  179. [string]$Location,
  180. [Parameter(Mandatory = $true)]
  181. [ValidateNotNullOrEmpty()]
  182. [string]$DirectoryPath,
  183. [Parameter(Mandatory = $true)]
  184. [ValidateNotNullOrEmpty()]
  185. [string]$SourceItemsPath
  186. )
  187. try
  188. {
  189. Write-Verbose "Importing Items To Data Lake Store..." -Verbose
  190. Import-AzureRmDataLakeStoreItem -AccountName $dataLakeStoreName -Path $SourceItemsPath -Destination $DirectoryPath
  191. }
  192. catch
  193. {
  194. Throw $_.Exception
  195. }
  196. }
  197.  
  198. $ErrorActionPreference = "SilentlyContinue"
  199. $DataLakeStoreResourceGroup = "<rg>"
  200. $ResourceGroupLocation = "northeurope"
  201. $DataLakeStoreName = "<dl>"
  202. $dataLakeStoreLocation = "North Europe"
  203. $thisAzureSubscriptionId = "<guid>"
  204. $thisAzureSubscriptionUsername = "<uname>"
  205. $thisAzureSubscriptionPassword = "<pword>"
  206. $thisDataLakeStoreDirectoryRoot = "<root>"
  207. $thisDataLakeStoreNewDirectory = "<dir>"
  208. $DirectoryPathForitem = $thisDataLakeStoreDirectoryRoot + $thisDataLakeStoreNewDirectory
  209. $thisSourceItemsPath = "<folderToUpload>"
  210.  
  211. Connect-AzureRmSubscription -AzureSubscriptionId $thisAzureSubscriptionId -AzureSubscriptionUsername $thisAzureSubscriptionUsername -AzureSubscriptionPassword $thisAzureSubscriptionPassword
  212. Set-AzureResourceGroup -ResourceGroupName $DataLakeStoreResourceGroup -Location $ResourceGroupLocation
  213. Set-AzureDataLakeStoreAccount -ResourceGroupName $DataLakeStoreResourceGroup -DataLakeStoreName $DataLakeStoreName -Location $dataLakeStoreLocation
  214. Set-AzureDataLakeStoreDirectory -ResourceGroupName $DataLakeStoreResourceGroup -DataLakeStoreName $DataLakeStoreName -Location $dataLakeStoreLocation -DatalakeStoreDirectoryRoot $thisDataLakeStoreDirectoryRoot -DataLakeStoreNewDirectory $thisDataLakeStoreNewDirectory
  215. Set-AzureDataLakeStoreItems -ResourceGroupName $DataLakeStoreResourceGroup -DataLakeStoreName $DataLakeStoreName -Location $dataLakeStoreLocation -DirectoryPath $DirectoryPathForitem -SourceItemsPath $thisSourceItemsPath
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement