Azure/VegaShellAzure.ps1
function GetKeyVault { <# .SYNOPSIS Cette fonction sert récupérer la valeur d'un keyvault .DESCRIPTION SecretVaultName --> Nom du keyvault SecretVaultNameValue --> Nom du Secret EncryptOutput --> Choisis si la sortie est securestring ou string .EXAMPLE GetKeyVault -SecretVaultName $VaultName -SecretVaultNameValue $VaultValueLogin #> param( [ValidateNotNullOrEmpty()] [Parameter(Position = 1, Mandatory = $true)] $SecretVaultName, [ValidateNotNullOrEmpty()] [Parameter(Position = 2, Mandatory = $true)] $SecretVaultNameValue, [Parameter(Position = 3, Mandatory = $false, HelpMessage = 'si activé, la sortie est en securestring')] [switch]$EncryptOutput ) begin { try { import-module Az.KeyVault } catch { install-module -Name Az.KeyVault -AllowClobber -force } } process { try { if ($EncryptOutput) { $SecretProd = (Get-AzKeyVaultSecret -VaultName $SecretVaultName -Name $SecretVaultNameValue).SecretValue $SecretKey = $SecretProd } else { $SecretProd = Get-AzKeyVaultSecret -VaultName $SecretVaultName -Name $SecretVaultNameValue $ssPtrProd = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecretProd.SecretValue) $SecretKey = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($ssPtrProd) [System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($ssPtrProd) } } catch [System.Management.Automation.PSInvalidOperationException] { Connect-AzAccount } catch { tryCatchError } } end { Write-Information "Récupération du KeyVault $SecretVaultName\$SecretVaultNameValue effectué" return $SecretKey } } function DownloadFileAzureStorageContainer { <# .SYNOPSIS Cette fonction sert a effectuer un dowload de 1 seul fichier depuis un compte de storage azure .DESCRIPTION storageAccountName --> nom du storage account $storageAccountKey --> chaine de connexion $container --> nom du container $fileFullPath --> fullpath du fichier $OutPath --> Dossier de sortie .EXAMPLE $param = @{ storageAccountName = 'dat830700sta009' storageAccountKey = (GetKeyVault -SecretVaultName SEC[...]KVT001 -SecretVaultNameValue sqlbackups-storageaccount-accesskey) container = 's[...]sql007-database0000006p' fileFullPath = 'YPN_DBMODELE/YPN_DBMODELE_Full' OutPath = (get-item C:\temp\#testenvoi) } downloadFileAzureStorageContainer @param #> param( [ValidateNotNullOrEmpty()] [parameter(Mandatory = $true)] [string]$storageAccountName, [ValidateNotNullOrEmpty()] [parameter(Mandatory = $true)] [string]$storageAccountKey, [ValidateNotNullOrEmpty()] [parameter(Mandatory = $true)] [string]$container, [ValidateNotNullOrEmpty()] [parameter(Mandatory = $true)] [string]$fileFullPath, [ValidateNotNullOrEmpty()] [parameter(Mandatory = $true)] [System.IO.FileSystemInfo]$OutPath ) begin { try { Import-Module -Name az.storage -Verbose:$false | Out-null $Context = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey } catch { Write-Warning "Echec du New-AzStorageContext" TryCatchError } } process { try { $checkBlobFile = Get-AzStorageBlob -container $container -context $Context -blob "$fileFullPath*" | Sort-Object LastModified -Descending | Select-Object -First 1 Get-AzStorageBlobContent -Blob $checkBlobFile.Name -Container $container -Context $Context -Destination $OutPath -Force | Out-null } catch { Write-Warning "Echec du Get-AzStorageBlob" TryCatchError } } end { $file = Get-item -Path $OutPath\$($checkBlobFile.Name) return $file } } function UploadFileAzureStorageContainer { <# .SYNOPSIS Cette fonction sert a effectuer un Upload de 1 fichier/dossier vers un compte de storage azure .DESCRIPTION storageAccountName --> nom du storage account $storageAccountKey --> chaine de connexion $container --> nom du container $fileFullPath --> fullpath du fichier .EXAMPLE $param = @{ storageAccountName = 'loopbackupinquiry' storageAccountKey = (GetKeyVault -SecretVaultName SEC[...]KVT001 -SecretVaultNameValue Value-accesskey) container = 'backup' fileFullPath = get-item -path "C:\temp\#testenvoi" } UploadFileAzureStorageContainer @param #> param( [ValidateNotNullOrEmpty()] [parameter(Mandatory = $true)]$storageAccountName, [ValidateNotNullOrEmpty()] [parameter(Mandatory = $true)]$storageAccountKey, [ValidateNotNullOrEmpty()] [parameter(Mandatory = $true)]$container, [ValidateNotNullOrEmpty()] [parameter(Mandatory = $true, HelpMessage = "fullPath file(get-item), directory(folder) recommanded, file directly not recommanded")] [System.IO.FileSystemInfo]$fileFullPath ) begin { try { azcopy copy } catch [System.Management.Automation.RemoteException] { $moduleAzCopy = $true } catch [System.Management.Automation.CommandNotFoundException] { $moduleAzCopy = $false try { Invoke-WebRequest -Uri "https://aka.ms/downloadazcopy-v10-windows" -OutFile C:\temp\AzCopy.zip -UseBasicParsing Expand-Archive C:\temp\AzCopy.zip C:\temp\AzCopy -Force Remove-item C:\temp\AzCopy.zip } catch { write-warning "Telecharger le fichier depuis Cette page et l'extraire dans c:\temp\azcopy ` https://docs.microsoft.com/fr-fr/azure/storage/common/storage-use-azcopy-v10" pause } } try { Import-Module -Name az.storage -Verbose:$false | Out-null } catch { try { Install-Module -Name az.storage Import-Module -Name az.storage -Verbose:$false | Out-null } catch { write-warning "Import module en erreur" break } } $StartTime = get-date $EndTime = (get-date).AddHours(1) $Context = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey $SAS = New-AzStorageContainerSASToken -Name $container -Permission acwd -Context $Context -StartTime $StartTime -ExpiryTime $EndTime } process { try { if ($moduleAzCopy -eq $true) { $azCopy = azcopy copy $fileFullPath "https://loopbackupinquiry.blob.core.windows.net/$container/$SAS" --recursive --log-level ERROR } else { Set-Location -Path (get-childitem c:\temp\*\azcopy.exe -Recurse).Directory.FullName $azCopy = .\azcopy.exe copy $fileFullPath "https://loopbackupinquiry.blob.core.windows.net/$container/$SAS" --recursive --log-level ERROR } } catch { write-warning "Fonction en erreur" $PSItem.Exception.Message break } } end { $succesCount = ([regex]::Matches($azCopy, "(Number of Transfers Completed: )(\d{0,3}) ").groups[2]).value $errorCount = ([regex]::Matches($azCopy, "(Number of Transfers Failed: )(\d{0,3}) ").groups[2]).value $Result = @(New-Object PSObject -Property @{'Number of Transfers Completed' = $succesCount; 'Number of Transfers Failed' = $errorCount }) return $Result } } |