Question Exécution tâche planifiée
- michault
- Auteur du sujet
- Hors Ligne
- Membre premium
-
- Messages : 128
- Remerciements reçus 0
[code:1]#Wol+TasksWithCsv.ps1
# Paramètres: $path : Fichier des Adresses MAC & Ip
# $tasks : Tâche à exécuter
# $log : Fichier trace
Param($path, $tasks, $log)
$var=Import-Csv $path
#Définition des constantes
[int]$Interval = 30
[int]$MaxTries = 6
# créer un tableau de correspondance entre les valeurs de retour et leur signification :
# (source: msdn2.microsoft.com/fr-fr/library/aa394350.aspx)
#$pingstatusref = @{}
#$pingstatusref[0] = \"Success\"
#$pingstatusref[11001] = \"Buffer Too Small\"
#$pingstatusref[11002] = \"Destination Net Unreachable\"
#$pingstatusref[11003] = \"Destination Host Unreachable\"
#$pingstatusref[11004] = \"Destination Protocol Unreachable\"
#$pingstatusref[11005] = \"Destination Port Unreachable\"
#$pingstatusref[11006] = \"No Resources\"
#$pingstatusref[11007] = \"Bad Option\"
#$pingstatusref[11008] = \"Hardware Error\"
#$pingstatusref[11009] = \"Packet Too Big\"
#$pingstatusref[11010] = \"Request Timed Out\"
#$pingstatusref[11011] = \"Bad Request\"
#$pingstatusref[11012] = \"Bad Route\"
#$pingstatusref[11013] = \"TimeToLive Expired Transit\"
#$pingstatusref[11014] = \"TimeToLive Expired Reassembly\"
#$pingstatusref[11015] = \"Parameter Problem\"
#$pingstatusref[11016] = \"Source Quench\"
#$pingstatusref[11017] = \"Option Too Big\"
#$pingstatusref[11018] = \"Bad Destination\"
#$pingstatusref[11032] = \"Negotiating IPSEC\"
#$pingstatusref[11050] = \"General Failure\"
foreach($v in $var)
{
WakeUpPC $v.Adresse_MAC
$i = 0
Start-Sleep -Seconds $Interval
$Date = Get-Date -Format G
$PingStatus = Get-WmiObject Win32_PingStatus -Filter (\"Address='\" + $v.IpAdresse + \"'\"«»)
$Date + \" : \" + $v.IpAdresse + \" : PingStatus : \" + $PingStatus.StatusCode | Out-File $log -Append -NoClobber
While ($PingStatus.StatusCode -ne 0) {
Start-Sleep -Seconds $Interval
$Date = Get-Date -Format G
$i++
if ($i -lt $MaxTries) {
$PingStatus = Get-WmiObject Win32_PingStatus -Filter (\"Address='\" + $v.IpAdresse + \"'\"«»)
$Date + \" : \" + $v.IpAdresse + \" : PingStatus : \" + $PingStatus.StatusCode | Out-File $log -Append -NoClobber
} else {
$Date + \" : \" + $v.IpAdresse + \" : Opération abortée: Nombre de tentative max atteint\" | Out-File $log -Append -NoClobber
Break
}
}
Start-Sleep -Seconds $Interval
$IpAdr = [string]$v.IpAdresse
$Date = Get-Date -Format G
schtasks /run /tn \"$($tasks)\" /s $IpAdr
if ($?)
{
$Date + \" : \" + $v.IpAdresse + \" : \" + $tasks + \" lancé\" | Out-File $log -Append -NoClobber
}
else
{
$Date + \" : \" + $v.IpAdresse + \" : Echec lancement \" + $tasks | Out-File $log -Append -NoClobber
}
}[/code:1]
La fonction wakeuppc, je l'ai pompé sur ce super site.
[code:1]Function WakeUpPC {
# Paramètres: $computer : PC à réveiller (adresse MAC)
Param($computer)
#Définition des constantes
[Byte[]]$paquet = [Byte[]] (,0xFF * 6)
$Adresse_MAC = [string]$computer
$paquet += (($Adresse_MAC.split('-') | foreach {[byte] ('0x' + $_)}) * 16)
$UdpClient= New-Object system.Net.Sockets.UdpClient
$udpClient.Connect(([System.Net.IPAddress]::Broadcast),1600)
$UdpClient.Send($paquet,$paquet.length)
}[/code:1]
Connexion ou Créer un compte pour participer à la conversation.
- michault
- Auteur du sujet
- Hors Ligne
- Membre premium
-
- Messages : 128
- Remerciements reçus 0
Pour faire suite au déploiement d'une ta^che planifiée voilà un 2°post qui l'exécute. Avec encore une fois, un gros coup de main de Laurent.
[code:1]#Wol+TasksWithCsv.ps1
# Paramètres: $path : Fichier des Adresses MAC & Ip
# $tasks : Tâche à exécuter
# $log : Fichier trace
Param($path, $tasks, $log)
$var=Import-Csv $path
#Définition des constantes
[int]$Interval = 30
[int]$MaxTries = 6
# créer un tableau de correspondance entre les valeurs de retour et leur signification :
# (source: msdn2.microsoft.com/fr-fr/library/aa394350.aspx)
#$pingstatusref = @{}
#$pingstatusref[0] = \"Success\"
#$pingstatusref[11001] = \"Buffer Too Small\"
#$pingstatusref[11002] = \"Destination Net Unreachable\"
#$pingstatusref[11003] = \"Destination Host Unreachable\"
#$pingstatusref[11004] = \"Destination Protocol Unreachable\"
#$pingstatusref[11005] = \"Destination Port Unreachable\"
#$pingstatusref[11006] = \"No Resources\"
#$pingstatusref[11007] = \"Bad Option\"
#$pingstatusref[11008] = \"Hardware Error\"
#$pingstatusref[11009] = \"Packet Too Big\"
#$pingstatusref[11010] = \"Request Timed Out\"
#$pingstatusref[11011] = \"Bad Request\"
#$pingstatusref[11012] = \"Bad Route\"
#$pingstatusref[11013] = \"TimeToLive Expired Transit\"
#$pingstatusref[11014] = \"TimeToLive Expired Reassembly\"
#$pingstatusref[11015] = \"Parameter Problem\"
#$pingstatusref[11016] = \"Source Quench\"
#$pingstatusref[11017] = \"Option Too Big\"
#$pingstatusref[11018] = \"Bad Destination\"
#$pingstatusref[11032] = \"Negotiating IPSEC\"
#$pingstatusref[11050] = \"General Failure\"
foreach($v in $var)
{
WakeUpPC $v.Adresse_MAC
$i = 0
Start-Sleep -Seconds $Interval
$Date = Get-Date -Format G
$PingStatus = Get-WmiObject Win32_PingStatus -Filter (\"Address='\" + $v.IpAdresse + \"'\"«»)
$Date + \" : \" + $v.IpAdresse + \" : PingStatus : \" + $PingStatus.StatusCode | Out-File $log -Append -NoClobber
While ($PingStatus.StatusCode -ne 0) {
Start-Sleep -Seconds $Interval
$Date = Get-Date -Format G
$i++
if ($i -lt $MaxTries) {
$PingStatus = Get-WmiObject Win32_PingStatus -Filter (\"Address='\" + $v.IpAdresse + \"'\"«»)
$Date + \" : \" + $v.IpAdresse + \" : PingStatus : \" + $PingStatus.StatusCode | Out-File $log -Append -NoClobber
} else {
$Date + \" : \" + $v.IpAdresse + \" : Opération abortée: Nombre de tentative max atteint\" | Out-File $log -Append -NoClobber
Break
}
}
Start-Sleep -Seconds $Interval
$IpAdr = [string]$v.IpAdresse
$Date = Get-Date -Format G
schtasks /run /tn \"$($tasks)\" /s $IpAdr
if ($?)
{
$Date + \" : \" + $v.IpAdresse + \" : \" + $tasks + \" lancé\" | Out-File $log -Append -NoClobber
}
else
{
$Date + \" : \" + $v.IpAdresse + \" : Echec lancement \" + $tasks | Out-File $log -Append -NoClobber
}
}[/code:1]
La fonction wakeuppc, je l'ai pompé sur ce super site.
[code:1]Function WakeUpPC {
# Paramètres: $computer : PC à réveiller (adresse MAC)
Param($computer)
#Définition des constantes
[Byte[]]$paquet = [Byte[]] (,0xFF * 6)
$Adresse_MAC = [string]$computer
$paquet += (($Adresse_MAC.split('-') | foreach {[byte] ('0x' + $_)}) * 16)
$UdpClient= New-Object system.Net.Sockets.UdpClient
$udpClient.Connect(([System.Net.IPAddress]::Broadcast),1600)
$UdpClient.Send($paquet,$paquet.length)
}[/code:1]
Y'a du y avoir un problème, je me retrouve avec 3 posts identiques. Désolé de foutre la pagaille:whistle:
Connexion ou Créer un compte pour participer à la conversation.
- Vous êtes ici :
-
Accueil
-
forum
-
PowerShell
-
Contributions à la communauté
- Exécution tâche planifiée