|
<# |
|
Add-AzureRmAccount |
|
#> |
|
$subscriptionName = "Visual Studio Enterprise" |
|
$resourceGroup = "storage-pcFileShare" |
|
$location = "east us 2" |
|
$storageAccountName = "pcFileShare" |
|
$fileShareName = "pcFiles" |
|
$driveLetter = "X" |
|
$jobName = "Mount $driveLetter Drive" |
|
|
|
Select-AzureRmSubscription -SubscriptionName $subscriptionName |
|
|
|
#region Create Resource Group |
|
$rg = Get-AzureRmResourceGroup -Name $resourceGroup -Location $location -ErrorAction SilentlyContinue |
|
if ($rg) { |
|
Write-Host "$resourceGroup resource group already exists..." |
|
} |
|
else { |
|
Write-Host "Creating $resourceGroup Resource Group..." |
|
$rg = New-AzureRmResourceGroup -Name $resourceGroup -Location $location -Force |
|
} |
|
#endregion |
|
|
|
#region Create Storage Account |
|
$sa = $rg | Get-AzureRMStorageAccount -StorageAccountName $storageAccountName -ErrorAction SilentlyContinue |
|
if ($sa) { |
|
Write-Host "$storageAccountName storage account already exists..." |
|
} |
|
else { |
|
Write-Host "Checking to see if $storageAccountName is valid..." |
|
$valid = Get-AzureRmStorageAccountNameAvailability -Name $storageAccountName.ToLower() |
|
if ($valid.NameAvailable) { |
|
Write-Host "$storageAccountName is valid..." |
|
Write-Host "Creating $storageAccountName Storage Account..." |
|
$sa = $rg | New-AzureRMStorageAccount -StorageAccountName $storageAccountName.ToLower() -Location $location -Type "Standard_GRS" -EnableEncryptionService File |
|
} |
|
else { |
|
Write-Host $valid.Message |
|
Exit |
|
} |
|
} |
|
#endregion |
|
|
|
#region Create File Share |
|
# Get Storage Key |
|
$key = ($sa | Get-AzureRmStorageAccountKey).Value | Select -First 1 |
|
# Create Context |
|
$ctx = New-AzureStorageContext -StorageAccountName $storageAccountName.ToLower() -StorageAccountKey $key |
|
$csa = Set-AzureRmCurrentStorageAccount -Context $ctx |
|
# Create File Share |
|
$ss = Get-AzureRmStorageAccount | Get-AzureStorageShare | Where-Object {$_.Name -eq $fileShareName.ToLower()} |
|
if ($ss.count -gt 0) { |
|
Write-Host("$fileShareName file share already exists...") |
|
} |
|
else { |
|
Write-Host("Creating $fileShareName file share...") |
|
$ss = New-AzureStorageShare -Name $fileShareName.ToLower() -Context $ctx |
|
} |
|
#endregion |
|
|
|
#region Store Credentials |
|
Write-Host "Storing file share credentials..." |
|
$add = "$storageAccountName.file.core.windows.net" |
|
cmdkey /add:$add /user:AZURE\$storageAccountName /pass:$key |
|
#endregion |
|
|
|
#region Mount File Share |
|
$root = "\\" + $storageAccountName + ".file.core.windows.net\" + $fileShareName |
|
$psd = Get-PSDrive | Where-Object {$_.DisplayRoot -eq $root} |
|
if ($psd.Count -gt 0) { |
|
Write-Host "File share already exists as drive $psd..." |
|
} |
|
else { |
|
$command = "net use " + $driveLetter + ": " + $root |
|
$sb = [scriptblock]::Create($command) |
|
$sb.Invoke() |
|
} |
|
#endregion |
|
|
|
#region Create Log In Task |
|
$user = whoami |
|
$sj = Get-ScheduledJob -Name $jobName -ErrorAction SilentlyContinue |
|
if ($sj.count -gt 0) { |
|
Write-Host "$jobName scheduled job already exists..." |
|
} |
|
else { |
|
Write-Host "Creating $jobName scheduled Job..." |
|
$sj = Register-ScheduledJob –Name $jobName ` |
|
-ScriptBlock $sb ` |
|
-ScheduledJobOption (New-ScheduledJobOption -ContinueIfGoingOnBattery:$true -StartIfOnBattery:$true) ` |
|
-Trigger (New-JobTrigger -AtLogOn -User $user) |
|
} |
|
Write-Host "Updating $jobName scheduled task..." |
|
$path = (Get-ScheduledTask $jobName).TaskPath |
|
$principal = New-ScheduledTaskPrincipal -UserId $user |
|
$st = Set-ScheduledTask -TaskName $jobName -Principal $principal -TaskPath $path |
|
#endregion |