July 9, 2025

USEFUL-IT

A blog for USEFUL-IT information

Powershell scripts

Uptime

Function Get-Uptime {
[CmdletBinding()]
  param(
    [Parameter(
      Position=0,
      ValueFromPipeline=$true,
      ValueFromPipelineByPropertyName=$true)]
    [String[]]$ComputerName=$env:computername
  )

foreach ($comp in $ComputerName){

   $OS = Get-WmiObject Win32_OperatingSystem -ComputerName $comp
   $SystemTZ = Get-WmiObject -Class Win32_TimeZone -ComputerName $comp
   $LocalTZ = Get-WmiObject -Class Win32_TimeZone
   $upTime = (Get-Date) - $OS.ConvertToDateTime($OS.LastBootUpTime)

     [PSCustomObject]@{
     SystemName = $OS.PSComputerName
     LocalTimeZone = $LocalTZ | select -ExpandProperty description
     SystemTimeZone = $SystemTZ | select -ExpandProperty description
     LastBoot_LocalTime = $OS.ConvertToDateTime($OS.LastBootUpTime)
     LastBoot_SystemTime = [datetime]::ParseExact(
     $OS.LastBootUpTime.split('.')[0],'yyyyMMddHHmmss',$null)
     TotalUptime = $upTime.ToString()
     TotalDaysUp = [math]::round($upTime.TotalDays, 2)
     TotalHoursUp = [math]::round($upTime.TotalHours, 2)
     TotalMinutesUp = [math]::round($upTime.TotalMinutes, 2)
     TotalSecondsUp = [math]::round($upTime.TotalSeconds, 2)
     TotalMillisecondsUp = [math]::round($upTime.TotalMilliseconds, 2)

    }
    }
}
PS> $comps = "CA-Server","NY-Server","localhost"
PS> Get-Uptime -ComputerName $comps

SystemName          : CA-Server
LocalTimeZone       : (UTC-06:00) Central Time (US & Canada)
SystemTimeZone      : (UTC-08:00) Pacific Time (US & Canada)
LastBoot_LocalTime  : 3/16/2013 7:45:17 PM
LastBoot_SystemTime : 3/16/2013 5:45:17 PM
TotalUptime         : 27.02:15:28.1364414
TotalDaysUp         : 27.09
TotalHoursUp        : 650.26
TotalMinutesUp      : 39015.47
TotalSecondsUp      : 2340928.14
TotalMillisecondsUp : 2340928136.44

SystemName          : NY-Server
LocalTimeZone       : (UTC-06:00) Central Time (US & Canada)
SystemTimeZone      : (UTC-05:00) Eastern Time (US & Canada)
LastBoot_LocalTime  : 3/6/2013 5:22:35 PM
LastBoot_SystemTime : 3/6/2013 6:22:35 PM
TotalUptime         : 37.04:38:34.3182684
TotalDaysUp         : 37.19
TotalHoursUp        : 892.64
TotalMinutesUp      : 53558.57
TotalSecondsUp      : 3213514.32
TotalMillisecondsUp : 3213514318.27

SystemName          : BERKENBILEJW530
LocalTimeZone       : (UTC-06:00) Central Time (US & Canada)
SystemTimeZone      : (UTC-06:00) Central Time (US & Canada)
LastBoot_LocalTime  : 4/12/2013 9:09:43 PM
LastBoot_SystemTime : 4/12/2013 9:09:43 PM
TotalUptime         : 00:51:26.4508818
TotalDaysUp         : 0.04
TotalHoursUp        : 0.86
TotalMinutesUp      : 51.44
TotalSecondsUp      : 3086.45
TotalMillisecondsUp : 3086450.88

 

 EventLog FileSize

Get-WmiObject -Class Win32_NTEventLogFile -filter "LogFileName = 'Application'" | Select CSName, FileSize | ft @{Expression={$_.CSName};Label="Hostname"}, @{Expression={$_.FileSize};Label="FileSize (Bytes)"} -AutoSize
function StringVersions {
param([string]$inputString)
  $obj = New-Object PSObject
  $obj | Add-Member NoteProperty HostName($logsize.CSName)
  $obj | Add-Member NoteProperty LogSize($logsize.FileSize)
  Write-Output $obj
  }

$hosts = Read-Host "Provide Servernames (use ; as separator) : "
$hostsarr = ($hosts).split(";")

Foreach ($a in $hostsarr) {
        $LogSize=Get-WmiObject -ComputerName $a -Class Win32_NTEventLogFile -filter "LogFileName = 'Application'"# | Select CSName, FileSize
        stringVersions $item
    }

Eventlogs 2

# ----------------------------------------------------------------------------- 
# Script: HTML_EventlogSize.ps1 
# Usage: HTML_EventlogSize.ps1 -Servers <server1>,<Server2> -eventlogs Application,System
# Author: ed wilson, msft 
# Date: 08/06/2012 15:11:03 
# Keywords: Scripting Techniques, Web Pages and HTAs 
# comments: Get-Wmiobject, New-Object, Get-Date, Convertto-HTML, Invoke-Item 
# HSG-8-7-2012
#
# Modified : 01/24/2013
# By : Wessel van Sandwijk
# Added date format
# ----------------------------------------------------------------------------- 
Param( 
  [string]$path = "\\readynas\Scripts\Reports\EventlogSize.html",
  [array]$servers = @("labhyp001","labhyp002"),
  [array]$eventlogs = @("Application","System")
) 

Function Get-EventlogSize 
{ Param ([string[]]$servers) 
  Foreach ($s in $servers)  
    {  
        Foreach ($l in $eventlogs)
            {
                $os = Get-WmiObject -Class Win32_NTEventLogFile -cn $s -filter "LogFileName = '$l'"
                $EventLogSize = $os.FileSize
                $system=$os.CSname
                New-Object psobject -Property @{
                FileSize = '{0:N0}' -f $eventLogSize;
                Eventlog=$l;
                #computer=$s; 
                System=$system;
                ReportDate =$(get-date)
                }
            }
    }
}

# Entry Point *** 

Get-EventlogSize -servers $servers #-eventlogs $eventlogs #|  
#ConvertTo-Html -As Table -body " 
#  <h1>Server Uptime Report</h1> 
#  The following report was run on $(get-date)" >> $path
#  Invoke-Item $path