Vrac de scripts Powershell

Cette petite fiche regroupe en vrac plusieurs scripts Powershell utile.

Sauvegarde d'une BDD d'un serveur SQL Express

snippet.powershell
$dt = Get-Date -Format ddMMyyyyHHmmss
$dbname = 'orchard'
$username = 'orchard'
$dir = 'B:\BDD'
$bakfile = "$dir\orchard_$($dt).bak"
$limit = (Get-Date).AddMinutes(-30)
$Extension = "*.bak"
 
Get-ChildItem -Path $dir -Filter $Extension -Force | Where-Object {$_.CreationTime -lt $limit} | Remove-Item
 
Write-Output  "backup database to disk file = $bakfile"
 
Backup-SqlDatabase -ServerInstance VADE-AD1\SQLExpress -Database $dbname -BackupFile $bakfile