အဲဒါ ဘာ စခြတ္လည္း - TopicsExpress



          

အဲဒါ ဘာ စခြတ္လည္း ဆရာတို႕ေရ တားက အဲဒီအပိုင္းေတြ တစ္ကယ္နားမလည္တဲ့ End User ေတာက္မို႕လို႕ပါ [CmdletBinding()] Param ( [Parameter(ValueFromPipeline=$true, ValueFromPipelinebyPropertyName=$true)] [Alias(Servers)] [string[]]$Name = (Get-Content c:\utils\servers.txt), [int]$AlertThreshold = 10, [string]$Path = c:\utils\uptime.html, [string]$Credential = surly\administrator, [string]$PathToCred = c:\utils ) Begin { Function Get-Credentials { Param ( [String]$AuthUser = $env:USERNAME ) $Key = [byte]29,36,18,74,72,75,85,52,73,44,0,21,98,76,99,28 #Build the path to the credential file $CredFile = $AuthUser.Replace(\,~) $File = $PathToCred + \Credentials-$CredFile.crd #And find out if its there, if not create it If (-not (Test-Path $File)) { (Get-Credential $AuthUser).Password | ConvertFrom-SecureString -Key $Key | Set-Content $File } #Load the credential file $Password = Get-Content $File | ConvertTo-SecureString -Key $Key $AuthUser = (Split-Path $File -Leaf).Substring(12).Replace(~,\) $AuthUser = $AuthUser.Substring(0,$AuthUser.Length - 4) $Credential = New-Object System.Management.Automation.PsCredential($AuthUser,$Password) Return $Credential } Write-Verbose $(Get-Date): Script begins! #Define static HTML $HeaderHTML = @ body { background-color:#DCDCDC; } table { border:1px solid gray; font:normal 12px verdana, arial, helvetica, sans-serif; border-collapse: collapse; padding-left:30px; padding-right:30px; } th { color:black; text-align:left; border: 1px solid black; font:normal 16px verdana, arial, helvetica, sans-serif; font-weight:bold; background-color: #6495ED; padding-left:6px; padding-right:6px; } td.up { background-color:#32CD32; border: 1px solid black; } td.down { background-color:#B22222; border: 1px solid black; } td { border: 1px solid black; padding-left:6px; padding-right:6px; } div.red { background-color:#B22222; float:left; text-align:right; } div.green { background-color:#32CD32; float:left; } div.free { background-color:#7FFF00; float:left; text-align:right; } a.detail { cursor:pointer; color:#1E90FF; text-decoration:underline; } Server Uptime Status Report Server NameStatusLowest Disk Status @ $DiskDetailHeaderHTML = @ Disk Detail Report @ $FooterHTML = @ @ $AllComputers = @() } Process { #Gather all computer names before processing ForEach ($Computer in $Name) { $AllComputers += $Computer } } End { #Sort the servers by name, then start getting information Write-Verbose Sort server names and gather Credential information $Name = $Name | Sort $DiskData = @() If ($Credential) { $Cred = Get-Credentials $Credential } ForEach ($Computer in $AllComputers) { Write-Verbose Testing $Computer... $ErrorReport = $null If (Test-Connection $Computer -Quiet) { #Set parameters for splat, determine if checking local $CredParameter = @{ ComputerName = $Computer ErrorAction = Stop } If ($Computer.ToUpper() -notlike *$($env:COMPUTERNAME.ToUpper())* -and $Cred) { $CredParameter.Add(Credential,$Cred) } #Get uptime information Try { $WMI = Get-WmiObject Win32_OperatingSystem @CredParameter If ($WMI) { $Uptime = New-TimeSpan -Start $($WMI.ConvertToDateTime($WMI.LastBootUpTime)) -End (Get-Date) $UpText = $($Uptime.Days)d, $($Uptime.Hours)h, $($Uptime.Minutes)m } Else { $UpText = Up } #Get disk information and pretty up the data $Disks = Get-WmiObject Win32_LogicalDisk -Filter DriveType=3 @CredParameter | Select ` @{LABEL=Server;EXPRESSION={$Computer}}, @{LABEL=DriveLetter;EXPRESSION={$_.DeviceID}}, @{LABEL=Size;EXPRESSION={[int]({0:N0} -f ($_.Size/1gb))}}, @{LABEL=FreeSize;EXPRESSION={[int]({0:N0} -f ($_.FreeSpace/1gb))}}, @{LABEL=perUsed;EXPRESSION={[int]({0:N0} -f ((($_.Size - $_.FreeSpace)/$_.Size)*100))}}, @{LABEL=perFree;EXPRESSION={[int]({0:N0} -f (100-(($_.Size - $_.FreeSpace)/$_.Size)*100))}}, VolumeName $DiskData += $Disks } Catch { Write-Verbose Error encountered gathering information for $Computer $ErrorReport = $Error[0] $Error.Clear | Out-Null } #Create the simple Status table If ($ErrorReport) { $UpText = WMI Error $DiskHTML = $($Error[0]) } ElseIf ($Disks) { $LowDisk = $Disks | Sort FreeSize | Select -First 1 If ($LowDisk.perFree -le $AlertThreshold) { $FreeClass = red } Else { $FreeClass = free } $DiskHTML = $($LowDisk.DriveLetter) $($LowDisk.Size)gb ($($LowDisk.perUsed)% used)$($LowDisk.FreeSize)gb free ($($LowDisk.perFree)%)`n } Else { $DiskHTML = } $DetailHTML += $Computer$UpText$DiskHTML`n } Else { $DetailHTML += $ComputerDOWN`n } } #Disk Details Report Write-Verbose WMI data gathered, making the report $Servers = $DiskData | Select Server -Unique ForEach ($Server in $Servers) { $Server = $Server.Server $DiskDetailHTML += $Server $DiskDetailHTML += $DiskDetailHTML += Drive LetterVolume NameTotal Disk SpaceUsedFreeUsage`n $Disks = $DiskData | Where { $_.Server -eq $Server } | Sort DriveLetter ForEach ($Disk in $Disks) { $DiskDetailHTML += $($Disk.DriveLetter)$($Disk.VolumeName)$($Disk.Size)gb$($Disk.Size - $Disk.FreeSize)gb$($Disk.FreeSize)gb If ($Disk.perFree -le $AlertThreshold) { $FreeClass = red } Else { $FreeClass = free } $DiskDetailHTML +=  $($Disk.perFree)%`n } $DiskDetailHTML += `n } #Combine all the HTML fragments and save to a file $HTML = $HeaderHTML + $DetailHTML + $DiskDetailHeaderHTML + $DiskDetailHTML + $FooterHTML $HTML | Out-File $Path Write-Verbose $(Get-Date): Script completed! }
Posted on: Thu, 28 Aug 2014 16:52:12 +0000

Trending Topics



Recently Viewed Topics




© 2015