420-1Q1-AA Help

Solutions

Solution : Exercice 1

$date = Get-Date $username = $env:USERNAME $computername = $env:COMPUTERNAME $rapportPath = "C:\Temp\rapport.txt" "Date et heure : $date`nUtilisateur : $username`nNom de l'ordinateur : $computername" | Out-File -FilePath $rapportPath

Solution : Exercice 2

for ($i = 1; $i -le 10; $i++) { if ($i % 2 -eq 0) { Write-Output "$i : Pair" } else { Write-Output "$i : Impair" } }

Solution : Exercice 3

$sourcePath = "C:\Source" $backupPath = "C:\Backup" if (-not (Test-Path -Path $backupPath)) { New-Item -ItemType Directory -Path $backupPath } $files = Get-ChildItem -Path $sourcePath -Filter "*.txt" foreach ($file in $files) { Copy-Item -Path $file.FullName -Destination $backupPath } Write-Output "Nombre de fichiers copiés : $($files.Count)"

Solution : Exercice 4

Get-Service $serviceName = Read-Host "Entrez le nom du service" $service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue if ($service) { Write-Output "L'état actuel du service '$serviceName' est : $($service.Status)" $action = Read-Host "Voulez-vous démarrer ou arrêter le service ? (start/stop)" if ($action -eq 'start') { Start-Service -Name $serviceName Write-Output "Service '$serviceName' démarré." } elseif ($action -eq 'stop') { Stop-Service -Name $serviceName Write-Output "Service '$serviceName' arrêté." } else { Write-Output "Action non reconnue." } } else { Write-Output "Service non trouvé." }

Solution : Exercice 5

$directoryPath = Read-Host "Entrez le chemin du répertoire à analyser" $files = Get-ChildItem -Path $directoryPath -Recurse -File foreach ($file in $files) { if ($file.Length -gt 100MB) { Write-Output "Fichier : $($file.FullName) - Taille : $([math]::Round($file.Length / 1MB, 2)) MB" } }
Last modified: 31 October 2024