How to express Azure File Share location using Powershell

245 views Asked by At

I am trying to modify current Powershell to point to Azure File Share.

Currently, bottom Powershell is used in a local drive, but I would like to change to pointing to Azure File Share.

param ( [String] $Filename  = '*.csv',
        [String] $SourceDir = 'C:\Test\',
        [String] $TargetDir = 'C:\Test\convert\'
)

ForEach ($csvFile in Get-ChildItem -Path $SourceDir -Filter $Filename){
    (Import-Csv $csvFile | 
     ConvertTo-Csv -NoTypeInformation -Delimiter '|') -replace '[,"]' -replace '\|',',' |
         Set-Content (Join-Path $TargetDir $csvFile.Name)                  
}

I have used the bottom Python file that points to Azure File Share like below (for other project).

So, the goal is modify the top Powershell with something similar to Python code below:

How to express URL, account key in the existing Powershell?

from azure.storage.fileshare import ShareServiceClient, ShareClient, 
ShareDirectoryClient, ShareFileClient
import pandas as pd
import io

account_url = "https://example.file.core.windows.net"
account_key = "xxxx=="

service_client = ShareServiceClient(account_url=account_url, credential=account_key)
share_name = "testname"
share_client = service_client.get_share_client(share_name)

# Get a ShareDirectoryClient object to connect to the directory
directory_path = "Parent/Child"
directory_client = share_client.get_directory_client(directory_path)

What I am trying to do is connect the $ctx to the argument (ForEach(...) ):

Bottom is just an example of applying "Remove-AzDtalakeGen2Item" that I found from this YouTube:

$ctx = New-AzStorageContext -StorageAccountName = 'sample' -StorageAccountKey "xxxxxxxxx"
Remove-AzDatalakeGen2Item -Context $ctx -FileSystem "sample" -Path "/Test/" -force
Remove-AzDatalakeGen2Item -Context $ctx -FileSystem "Sample" -Path "/Test/Convert" -force

(1/12/2024): This is an error message: enter image description here

(1/16/2024): enter image description here

(1/18/2024): enter image description here

Passing values directly (1/18/2024) enter image description here

1

There are 1 answers

24
Martin Iszac On BEST ANSWER

First install Az.Storage using Install-Module -Name Az.Storage -Force -AllowClobber

Here is the script youre looking for:


param (
    [String] $Filename  = '*.csv',
    [String] $SourceDir = 'Test',
    [String] $TargetDir = 'Test/convert'
)

$storageAccountName = "your-storage-account-name"
$storageAccountKey = "your-storage-account-key"
$shareName = "your-share-name"

$ctx = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey

$allFiles = Get-AzStorageFile -Context $ctx -ShareName $shareName -Path $SourceDir 
$csvFiles = $allFiles | Where-Object { $_.Name -like $Filename }

ForEach ($csvFile in $csvFiles) {
    $fileExists = Get-AzStorageFile -Context $ctx -ShareName $shareName -Path "$TargetDir/$($csvFile.Name)" -ErrorAction SilentlyContinue

    if ($null -eq $fileExists) {
        $content = Get-AzStorageFileContent -Context $ctx -ShareName $shareName -Path "$SourceDir/$($csvFile.Name)" |
                    Out-String |
                    ConvertFrom-Csv -Delimiter ',' |
                    ConvertTo-Csv -NoTypeInformation -Delimiter '|' |
                    ForEach-Object { $_ -replace '[,"]' -replace '\|',',' }

        Set-AzStorageFileContent -Context $ctx -ShareName $shareName -Path "$TargetDir/$($csvFile.Name)" -Value $content
    } else {
        Write-Host "File $($csvFile.Name) already exists in the Azure File Share."
    }
}

Get-AzStorageFile is used to check if a file with the same name already exists in the Azure File Share. If the file does not exist, Get-AzStorageFile will return $null and the script will upload the local file. If the file does exist, the script will print a message and skip the upload. Feel free to remove this part if you dont care about the files being rewritten.