142 lines
4.0 KiB
PowerShell
142 lines
4.0 KiB
PowerShell
<#
|
|
This script monitors specified folder and when new file is copied there it transfers this file to specified Azure blob storage.
|
|
After transfer script moves files to movedfiles subfolder.
|
|
#>
|
|
|
|
param
|
|
(
|
|
[Parameter(Mandatory=$true, HelpMessage = "Azure storage account name which will be used for storing files")]
|
|
[string]$azureStorageAccountName,
|
|
[Parameter(Mandatory=$true, HelpMessage = "Azure storage account key")]
|
|
[string]$azureStorageAccountKey,
|
|
|
|
[Parameter(Mandatory=$true, HelpMessage = "Folder on local file system which should be monitored by script")]
|
|
[string]$pathToMonitor,
|
|
[Parameter(Mandatory=$true, HelpMessage = "Prefixes of the files which should be moved to Azure blob storage")]
|
|
[string]$fileNamePrefix,
|
|
[Parameter(Mandatory=$true, HelpMessage = "Extensions of the files which should be moved to Azure blob storage")]
|
|
[string]$fileNameExtension
|
|
)
|
|
|
|
# logging
|
|
function Log-File {
|
|
param (
|
|
$msg
|
|
)
|
|
|
|
$msg = [System.DateTime]::Now.ToString("yyyy.MM.dd HH:mm:ss.ffff") + "`t" + $msg
|
|
($msg) | Out-File $global:logFile -Append
|
|
}
|
|
|
|
function Log-Info {
|
|
param (
|
|
$msg
|
|
)
|
|
|
|
Write-Host $msg -foregroundcolor green
|
|
Log-File $msg
|
|
}
|
|
|
|
function Log-Warn {
|
|
param (
|
|
$msg
|
|
)
|
|
|
|
Write-Host $msg -foregroundcolor yellow
|
|
Log-File $msg
|
|
}
|
|
|
|
function Log-Error {
|
|
param (
|
|
$msg
|
|
)
|
|
|
|
Write-Host $msg -foregroundcolor red
|
|
Log-File $msg
|
|
}
|
|
|
|
# files processing
|
|
function Move-File-To-Azure {
|
|
param (
|
|
$fileName,
|
|
$filePath,
|
|
$timestamp
|
|
)
|
|
Log-Info ("Process file $filePath")
|
|
if (!(Test-Path $filePath -PathType Leaf)) {
|
|
Log-Error ("File $filePath doesn't exist")
|
|
return
|
|
}
|
|
|
|
Log-Info ("Open Azure session")
|
|
$ctx = New-AzureStorageContext -StorageAccountName $azureStorageAccountName -StorageAccountKey $azureStorageAccountKey
|
|
$azureStorageContainerName = "idp"
|
|
Log-Info ("Connect to container $azureStorageContainerName")
|
|
$container = Get-AzureStorageContainer -Name $azureStorageContainerName -Context $ctx -ErrorAction SilentlyContinue
|
|
if (!$container) {
|
|
Log-Info ("Container $azureStorageContainerName doesn't exist. Create it")
|
|
New-AzureStorageContainer -Name $azureStorageContainerName -Context $ctx -Permission Blob
|
|
Log-Info ("Container $azureStorageContainerName is successfully created")
|
|
}
|
|
|
|
$blobName = $timestamp + "/" + $fileName
|
|
Log-Info ("Transfering file $fileName to Azure blob storage $blobName...")
|
|
Set-AzureStorageBlobContent -File $filePath -Container $azureStorageContainerName -Blob $blobName -Context $ctx
|
|
Log-Info ("File has been sucessfully transfered")
|
|
|
|
Log-Info ("Move $fileName file to $global:movedFiles")
|
|
Move-Item -Path $filePath -Destination $global:movedFiles
|
|
Log-Info ("File has been successfully moved")
|
|
}
|
|
|
|
# initialization
|
|
$currentDir = $PSScriptRoot
|
|
Set-Location $currentDir
|
|
|
|
$logFolder = [System.IO.Path]::Combine($PSScriptRoot, "logs")
|
|
if (!(Test-Path $logFolder)) {
|
|
New-Item -ItemType Directory -Force -Path $logFolder
|
|
}
|
|
$timestamp = [System.DateTime]::Now.ToString("yyyyMMddHHmmss")
|
|
$global:logFile = [System.IO.Path]::Combine($logFolder, [System.String]::Format("taloyhtio_idp_{0}.txt", $timestamp))
|
|
|
|
$global:movedFiles = [System.IO.Path]::Combine($PSScriptRoot, "movedfiles/" + $timestamp)
|
|
if (!(Test-Path $global:movedFiles)) {
|
|
New-Item -ItemType Directory -Force -Path $global:movedFiles
|
|
}
|
|
|
|
Log-Info ("Start new session")
|
|
|
|
if (!(Test-Path $pathToMonitor)) {
|
|
Log-Error ("Path $pathToMonitor doesn't exist")
|
|
return
|
|
}
|
|
|
|
if (!$pathToMonitor.EndsWith("/") -and !$pathToMonitor.EndsWith("\")) {
|
|
$pathToMonitor += "/"
|
|
}
|
|
|
|
$files = Get-ChildItem $pathToMonitor
|
|
if ($files.Count -eq 0) {
|
|
Log-Info ("Folder $pathToMonitor is empty")
|
|
return
|
|
}
|
|
|
|
if (!$fileNameExtension.StartsWith(".")) {
|
|
$fileNameExtension = "." + $fileNameExtension
|
|
}
|
|
|
|
foreach ($f in $files) {
|
|
if (!$f.Name.ToLower().StartsWith($fileNamePrefix)) {
|
|
Log-Warn ("File $($f.Name) doesn't start with $fileNamePrefix. Skip it")
|
|
continue
|
|
}
|
|
|
|
if ($f.Extension.ToLower() -ne $fileNameExtension.ToLower()) {
|
|
Log-Warn ("File $($f.Name) doesn't have extension $fileNameExtension. Skip it")
|
|
continue
|
|
}
|
|
|
|
|
|
Move-File-To-Azure $f.Name $f.FullName $timestamp
|
|
} |