La Référence Absolue sur les Technologies Microsoft




[Windows PowerShell] Calculer l'uptime d'un serveur

Accueil > Script > reseau > [Windows PowerShell] Calculer l'uptime d'un serveur

[Windows PowerShell] Calculer l'uptime d'un serveur

Par Pierre-maxime JEANNEY, LABORATOIRE SUPINFO DES TECHNOLOGIES MICROSOFT
Publiée le 04/05/2007 vers 9h.

Voici une fonction assez utile pour des administrateurs désirant savoir depuis combien de temps un serveur est démarré.

Cette fonction s'ajoute au profile de l'utilisateur afin de pouvoir l'utilisée.

Function Get-Uptime {
      Param([string]$server)
      Begin {
           function Uptime {
                param([string]$srv)
                $os = Get-WmiObject Win32_OperatingSystem -ComputerName $srv
                $uptime = $os.LastBootUpTime
                return $uptime
           } 
           function ConvertDate {
                 param([string]$date)
                 $year = $date.substring(0,4)
                 $Month = $date.Substring(4,2)
                 $Day = $date.Substring(6,2)
                 $hour = $date.Substring(8,2)
                 $min = $date.Substring(10,2)
                 $sec = $date.Substring(12,2)
                 $RebootTime = new-Object System.DateTime($year,$month,$day,$hour,$min,$sec)
                 $now = [System.DateTime]::Now
                 $uptime = $now.Subtract($RebootTime)
                 Write-Host "==> Uptime: " -NoNewLine
                 Write-Host "$($uptime.days)" -NoNewLine -ForeGroundColor Green
                 Write-Host " days, " -NoNewLine
                 Write-Host "$($uptime.Hours)" -NoNewLine -ForeGroundColor Green
                 Write-Host " hours, " -NoNewLine
                 Write-Host "$($uptime.Minutes)" -NoNewLine -ForeGroundColor Green
                 Write-Host " minutes, " -NoNewLine
                 Write-Host "$($uptime.seconds)" -NoNewLine -ForeGroundColor Green
                 Write-Host " seconds, "
            } 
                 Write-Host 
                 $process = @()
       }
       Process {
              if($_){
                    if($_.ServerName ){
                             $process += $_.ServerName
                     }
                    else{
                             $process += $_
                    }
              }
        }
       End {
              if($Server){
                     $process += $server
              }
              foreach ($Server in $process){
                        $result = uptime $server
                        Write-Host "Uptime for Server [" -NoNewline
                        Write-Host $server -NoNewline -foregroundcolor Cyan
                        Write-Host "]"
                        ConvertDate $result
                        Write-Host
               }
               Write-Host
         }
}

Ensuite la fonction est accessible en tapant :

PS C:\> Get-Uptime monserveur

Source : http://bsonposh.com/modules/wordpress/?p=16



Proposer un script

Notre avis :

Votre avis :

Réactions :
0

Votants :
9

Visites :
9717

1
2
3
4
5

Scripts Windows Prcdent :

Activer les clichés instantanés



Retrouvez ci-dessous les autres sections du Laboratoire Microsoft