~~~ps
# Définir les paramètres
$sourcePath = "C:\Chemin\Vers\DossierSource"
$destinationPath = "C:\Chemin\Vers\DossierDestination"
$scheduledTime = "15:30" # Heure planifiée au format HH:mm
# Fonction pour déplacer le dossier
function Move-Folder {
if (Test-Path $sourcePath) {
Move-Item -Path $sourcePath -Destination $destinationPath -Force
Write-Host "Dossier déplacé avec succès de $sourcePath vers $destinationPath"
} else {
Write-Host "Le dossier source $sourcePath n'existe pas"
}
}
# Boucle principale
while ($true) {
$currentTime = Get-Date -Format "HH:mm"
if ($currentTime -eq $scheduledTime) {
Move-Folder
break
}
Start-Sleep -Seconds 60 # Attendre 1 minute avant de vérifier à nouveau
}
~~~
To execute a script when a specific event, such as the occurrence of the word "baba," appears in a log file, you can use a combination of PowerShell and a file system watcher. This method will continuously monitor the log file for changes and trigger the script when the specified event occurs.
Here's how you can set this up using PowerShell:
### Step 1: Create the PowerShell Script
1. **Create a PowerShell Script**: Open a text editor and paste the following PowerShell script. Save it with a `.ps1` extension, e.g., `MonitorLog.ps1`.
```powershell
# Define the path to the log file and the script to execute
$logFilePath = "C:\path\to\your\logfile.log"
$scriptToExecute = "C:\path\to\your\script.bat" # or .ps1 for PowerShell script
# Create a file system watcher to monitor the log file
$fileWatcher = New-Object System.IO.FileSystemWatcher
$fileWatcher.Path = Split-Path $logFilePath
$fileWatcher.Filter = (Split-Path $logFilePath -Leaf)
$fileWatcher.NotifyFilter = [System.IO.NotifyFilters]'LastWrite'
# Define the action to take when the log file changes
$action = {
# Read the log file and check for the event
$content = Get-Content -Path $logFilePath -Tail 10
if ($content -match "baba") {
# Execute the script
Start-Process -FilePath $scriptToExecute
}
}
# Keep the script running
while ($true) {
Start-Sleep -Seconds 1
}
```
### Step 2: Run the PowerShell Script
1. **Open PowerShell as Administrator**: You may need administrative privileges to execute scripts, especially if they affect system settings or files.
2. **Set Execution Policy**: If your system's execution policy prevents scripts from running, you can change it temporarily by running:
```powershell
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
```
3. **Execute the Script**: Run the PowerShell script you created:
```powershell
.\MonitorLog.ps1
```
### Explanation
- **File System Watcher**: This PowerShell feature monitors changes to the specified log file.
- **Action**: When the file changes, the script reads the last few lines and checks for the presence of the word "baba."
- **Script Execution**: If "baba" is found, the specified script is executed.
### Notes
- **Script Path**: Make sure to replace the paths in the script with the actual paths to your log file and the script you want to execute.
- **Log File Path**: Ensure the log file path is correct and accessible.
- **Continuous Monitoring**: The script runs continuously, monitoring the log file for changes.
This setup provides a simple and effective way to automate responses to specific events in a log file.
Que fait cette commande ?
Cette commande ferme toutes les sessions déconnectées qui ne datent pas d’aujourd’hui. En effet je considère qu’une session déconnectées depuis plus d’un jour n’a rien à faire sur une serveur.
⚠️ Cela peut engendrer une perte de données si vos utilisateurs ont un logiciel ouvert (comme Word) parce que la session sera fermée en mode forcé (les modifications non enregistrées seront perdues).
Voici une réponse concise et précise à votre question :
Le problème que vous rencontrez est lié à la limitation du nombre de caractères dans les chemins d'accès des fichiers sur Windows. Cette limitation est de 260 caractères pour le chemin d'accès complet et de 248 caractères pour le nom du répertoire.[1][5]
Pour outrepasser cette limitation, vous pouvez utiliser le préfixe "\\?\" devant le chemin d'accès. Cela permet d'étendre la limite à 32 767 caractères.[5] Voici comment modifier votre script :
La limite par défaut du nombre de caractères dans un chemin d'accès Windows est de 260 caractères (MAX_PATH). Cependant, il est possible de supprimer ou d'augmenter cette limite avec PowerShell[1][2] :
## Supprimer la limite de 260 caractères
Pour supprimer complètement la limite de 260 caractères, vous pouvez suivre ces étapes :
1. Ouvrir l'Éditeur du Registre Windows (regedit.exe)
2. Accéder à la clé de registre : `HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem`
3. Modifier la valeur `LongPathsEnabled` et la définir sur `1` pour l'activer
Une fois cette modification effectuée, Windows pourra gérer des chemins d'accès de jusqu'à 32 767 caractères.
## Augmenter la limite à 32 767 caractères
Vous pouvez également augmenter la limite à 32 767 caractères sans la supprimer complètement. Pour cela :
1. Ouvrir une fenêtre PowerShell en tant qu'administrateur
2. Exécuter la commande suivante :
```powershell
Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem' -Name 'LongPathsEnabled' -Value 1
```
Cette commande modifie directement la clé de registre `LongPathsEnabled` pour activer la prise en charge des longs chemins d'accès.
Notez que même après avoir augmenté cette limite, certaines applications comme l'Explorateur Windows ou l'Invite de commandes peuvent encore avoir des limitations. Seules les applications qui utilisent l'API Windows appropriée pourront bénéficier de cette augmentation de limite[2].