Save files to Azure fileshare in the sub directory - azure

Below is the runbook code I am using to save the file to azure fileshare. But unable to save in subdirectory.
#Set the context using the storage account name and key
$context = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
$s = Get-AzureStorageShare "X1" –Context $context
$ErrorLogFileName = "Test.csv"
$LogItem = New-Item -ItemType File -Name $ErrorLogFileName
$_ | Out-File -FilePath $ErrorLogFileName -Append
Set-AzureStorageFileContent –Share $s –Source $ErrorLogFileName
Here I have a folder structure like X1/X2. But unable to get there and save the Test.csv. infact able to save it X1 folder on the Azure fileshare. Any idea ?
Thanks in Advance

You can specify the -Path parameter for Set-AzureStorageFileContent.
For example, the file share is X1, and there is a folder X2 inside X1. Then you can use the command below:
Set-AzureStorageFileContent –Share "X1" –Source $ErrorLogFileName -Path "X2" -Context $context
By the way, the command you're using is old, please try to consider using the latest az powershell command. For example, you can use Set-AzStorageFileContent instead of Set-AzureStorageFileContent(if you try to use the az module, then please replace all the old commands with the new ones).

Related

Want to all details of blob storage in one script

Script:
$storageAccountKey = (Get-AzStorageAccountKey -ResourceGroupName "RD-RDIPCloudMigration-AppResources-devtest" -AccountName "rdipstoragegen2").Value[0]
$ctx = New-AzStorageContext -StorageAccountName "rdipstoragegen2" -StorageAccountKey $storageAccountKey
Get-AzDataLakeGen2ChildItem -Context $ctx -FileSystem "rdipdata" -Path "eval/raw/rdipclinicaltrial/data/warehouse/integrated/clntrl_reference_use.db" | export-csv "test.csv" -NoTypeInformation
By using this I am only able to get details of file level, if I run this script again and again in each level the only i can get details of the file inside the folder.
FYI
Kindly help me to get one script by which i can get all the details include file inside the folder.
There is a -Recurse switch within the Get-AzDataLakeGen2ChildItem cmdlet that:
Indicates if will recursively get the Child Item. The default is
false.

Convert .csv file to ZIP file using Powershell and store in Azure blob stroage

I am trying to convert .CSV file to .ZIP using Powershell and then store the ZIP file in Azure blob storage.
Invoke-Sqlcmd -ServerInstance $SvrName -Database $dataname -Username $UserName -Password $Password -Query $getTABLEdetails | Export-CSV $filename1 -Append -NoTypeInformation
With the above command I am storing the output in .CSV file. Now I want to convert the .CSV file .ZIP file. And store the ZIP file in Azure blob storage.
$context = New-AzureStorageContext -StorageAccountName 'Accountname' -StorageAccountKey 'storagekey'
Set-AzureStorageBlobContent -Container zipfiles -File $filename1 -Blob $filename1 -Context $context -Force
With the above command I can store it in .CSV file. But I want to store the file in .ZIP format.
Is their any way I can achieve this?
Please help
Use Compress-Archive to place your CSV file in a ZIP archive file before uploading to Blob storage.
To extract the filename from the CSV file, I use -LeafBase from Split-Path, then format this filename into a ZIP file path using the Format operator -f. Could also just concatenate with (Split-Path -Path $csvFile -LeafBase) + '.zip'.
$storageAccountName = 'Accountname'
$storageAccountKey = 'storagekey'
$containerName = 'zipfiles'
$csvFile = 'data.csv'
# Format into data.zip
$zipFile = '{0}.zip' -f (Split-Path -Path $csvFile -LeafBase)
# Create archive file
# Use -Update to overwrite if necessary
Compress-Archive -Path $csvFile -DestinationPath $zipFile -Update
# Get current storage account context
$context = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
# Upload .zip file to Blob storage container
# Use -Force to skip prompts
Set-AzureStorageBlobContent -Container $containerName -Blob $zipFile -File $zipFile -Context $context -Force
To use the newer Az modules, we can replace New-AzureStorageContext with New-AzStorageContext and Set-AzureStorageBlobContent with Set-AzStorageBlobContent.

Upload data from Azure Automation Account Powershell

I have some code which pulls out a Kusto query from Azure Monitor and I need to upload the data to a blob storage account for long term retention.
I can pull out the data and display it in the azure automation screen when I run through the test pane, but it doesn't upload to blob.
I think the error is here
$SearchResult
$StorageAccountName = Get-AutomationVariable -Name "AccessKey"
$StorageAccountKey = Get-AutomationVariable -Name "StorageAccName"
foreach ($sr in $SearchResult){
$ctx = New-AzureStorageContext -StorageAccountName $StorageAccountName `
-StorageAccountKey $StorageAccountKey
$ContainerName = "Data"
New-AzureStorageContainer -Name $ContainerName -Context $ctx -Permission Blob
$BlobName = "$sr"
Set-AzureStorageBlobContent -Container $ContainerName -Blob $BlobName `
-Context $ctx
The full script is below
https://pastebin.com/embed_iframe/RyLJZVKW
Basically it authenticates using some stored variables and then runs a query which returns the results below (up to that part works), but then I'd like to upload the data to Blob.
The output example can be found at:
https://pastebin.com/embed_iframe/fEF6NsnK
If there's a better way of getting a kusto query stored straight to blob storage I'd be happy to consider that .. thanks everyone :)
Your Set-AzureStorageBlobContent call seems to be missing the -File parameter. You probably get a message complaining about this in the job Error stream.
Assuming that you want to send the data stored in the $sr variable, something like this should work (not tested):
$tempFile = New-TemporaryFile
$sr | Out-File -FilePath $tempFile.FullName
Set-AzureStorageBlobContent ... -File $tempFile.FullName
Remove-Item $tempFile

Grant access to Azure Data Lake Gen2 using a parameterized script

We are trying to grant read/write access to many folders in our Azure data Lake gen 2 containers and although we can do this through the UI, it's quite tedious and has to be repeated for all environments. Has anyone used a better way using Powershell to automate or at least parameterize this process of granted access to Azure Data Lake gen 2 containers and avoid granting access manually?
Unfortunately I couldn't get this to work using the following link or other documentation as it's for Gen 1 but it's very similar to what I need to do for gen 2.
https://www.sqlchick.com/entries/2018/3/17/assigning-data-permissions-for-azure-data-lake-store-part-3
According to my test, we can use the PowerShell to manage Azure Data Lake Gen2 permissions. For more details, please refer to the document
Install the required module
install-Module PowerShellGet –Repository PSGallery –Force
install-Module Az.Storage -Repository PSGallery -RequiredVersion 1.9.1-preview –AllowPrerelease –AllowClobber –Force
Besides, please note that if you want to install the module, you need to meet some conditions
.NET Framework is 4.7.2 or greater installed
PowerShell is 5.1 or higher
Script
Connect-AzAccount
$groupName=""
$accountName=""
$account= Get-AzStorageAccount -ResourceGroupName $groupName -Name $accountName
$ctx = $account.Context
$filesystemName = "test"
$dirname="template/"
$Id = "<the Object ID of user, group or service principal>"
$dir=Get-AzDataLakeGen2Item -Context $ctx -FileSystem $filesystemName -Path $dirname
$acl = New-AzDataLakeGen2ItemAclObject -AccessControlType user -EntityId $id -Permission "rw-" -InputObject $dir.ACL
Update-AzDataLakeGen2Item -Context $ctx -FileSystem $filesystemName -Path $dirname -Acl $acl
$dir=Get-AzDataLakeGen2Item -Context $ctx -FileSystem $filesystemName -Path $dirname
$dir.ACL
Thanks Jim Xu for providing the script above. I'm just complementing the code with the following items :
Get all folders from the container
Assign ACL for all folders
Propagate ACL to all subfolders
$groupName="resource group name"
$accountName="storage account name"
$account= Get-AzStorageAccount -ResourceGroupName $groupName -Name $accountName
$ctx = $account.Context
$filesystemName = "container name"
$Id = (Get-AzADGroup -DisplayName '<type user / group name here>').Id
$items = Get-AzDataLakeGen2ChildItem -Context $ctx -FileSystem $filesystemName
foreach ( $item in $items) {
$dir = Get-AzDataLakeGen2Item -Context $ctx -FileSystem $filesystemName -Path "$($item.Path)/"
$acl = New-AzDataLakeGen2ItemAclObject -AccessControlType group -EntityId $id -Permission "rwx" -InputObject $dir.ACL -DefaultScope
# Update ACL on blob item
Update-AzDataLakeGen2Item -Context $ctx -FileSystem $filesystemName -Path "$($item.Path)/" -Acl $acl
# Propagate ACL to child blob items
Set-AzDataLakeGen2AclRecursive -Context $ctx -FileSystem $filesystemName -Path "$($item.Path)/" -Acl $acl
}

Download multiple powershell files from Azure Storage container as a Zip

How to download all the PowerShell files from the Azure storage
account container to a zip folder through power shell cmdlets?
As of now, the below cmdlet helps to download a specific blob by its name
$blob = Get-AzureStorageBlobContent -Container hemac -Blob "CreateAndDeploy-Eventhubs.ps1" -Context $ctx -Destination $f -Force
First set a folder to download all the blobs
1.Provide the full path to a directory you wish to use for downloaded blob
$DestinationFolder = "<C:\DownloadedBlobs>"
Create the destination directory and download the blob
New-Item -Path $DestinationFolder -ItemType Directory -Force
$blob | Get-AzureStorageBlobContent –Destination $DestinationFolder
Now zip the entire folder.
$folderToZip = "C:\DownloadedBlobs"
$rootfolder = Split-Path -Path $folderToZip
$zipFile = Join-Path -Path $rootfolder -ChildPath "ZippedFile.zip"
Then you should use this - docs
Compress-Archive -Path $folderToZip -DestinationPath $zipFile -Verbose
The zipped file will be in the same directory as the download folder

Resources