Upload a file to azure data lake storage using PowerShell

Viewed 224

The code below works manually but what I'm trying to do is find a way I can run it in an automated way through a job and log in to my azure account also not expose the account key in the code.

any advice?

{Connect-AzAccount
Select-AzSubscription -SubscriptionId <subId>
$ctx = New-AzStorageContext -StorageAccountName <Accounttest> -UseConnectedAccount
$ctx = New-AzStorageContext -StorageAccountName <Accounttest> 
 -StorageAccountKey <accoutnKey>
$filesystemName = "DataFiles"
New-AzStorageContainer -Context $ctx -Name $filesystemName
# folder directory
$filesystemName = " DataFiles "
$dirname = "my-directory/"
New-AzDataLakeGen2Item -Context $ctx -FileSystem $filesystemName -Path $dirname -Directory

#Upload a file to a directory
$localSrcFile =  "upload2.txt"
$filesystemName = " DataFiles "
$dirname = "my-directory/"
$destPath = $dirname + (Get-Item $localSrcFile).Name
New-AzDataLakeGen2Item -Context $ctx -FileSystem $filesystemName -Path $destPath -Source $localSrcFile -Force
 
}
1 Answers

I think it will be better to put your PowerShell script in a DevOps pipeline in Azure Pipelines for instance. A Service principal would be needed. You would need to configure the service connection in Azure DevOps and your service principal should be Contributor on the resource group where the Data Lake Storage is located.

See link: Manage service connections

Related