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
First install
Az.Storage
usingInstall-Module -Name Az.Storage -Force -AllowClobber
Here is the script youre looking for:
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.