IT Support Diary Everyday IT support issues and solutions

Useful PowerShell Scripts

03.20.2009 · Posted in Linux

Windows PowerShell is a new scripting language from Microsoft.  Below are some useful PS scripts I’ve written for Zabbix. We are using Zabbix Sender and Zabbix Trapper to check for file age, file size and string in files.

 

To check File Age in Minutes

# FileAging.ps1
# This script developed and tested with PowerShell RTM
 

Param($filepath)
if ($filepath -isnot [string]) {
Write-Host “ERROR: You must specify a file path!” `
-foregroundcolor “RED” -backgroundcolor “Black”;`
return
}
#verify $file exists as a path
if (Test-Path $filepath) {

    $now=Get-Date
    #only get file objects, not folder objects
    $file=Get-ChildItem -path $filepath

    #enumerate files and get information

    $ageminutes=($now.subtract(($file.LastWriteTime))).minutes
    $agehours=($now.subtract(($file.LastWriteTime))).hours
    $agedays=($now.subtract(($file.LastWriteTime))).days

    $ageminutes = $ageminutes + ($agehours * 60) + ($agedays * 24 * 60)

    $SendZabbix = $ageminutes
  }
  else {
  Write-Host “Failed to find” $filepath.ToUpper() -foregroundcolor “Red” `
  -backgroundcolor “Black”
  $SendZabbix = 1000000
  }

 

Check File Size in KB

# FileSize.ps1
# This script developed and tested with PowerShell RTM
 

Param($filepath)
if ($filepath -isnot [string]) {
Write-Host “ERROR: You must specify a file path!” `
-foregroundcolor “RED” -backgroundcolor “Black”;`
return
}
#verify $file exists as a path
if (Test-Path $filepath) {

    $file=Get-ChildItem -path $filepath

    #enumerate files and get information
    $filesize=($file.Length)
   
    #get file size in bytes
    $filesizekb = “{0:N0}” -f ($filesize / 1kb)    

    #get file size in kb
    $SendZabbix = $filesizekb
 

  }
  else {
  Write-Host “Failed to find” $filepath.ToUpper() -foregroundcolor “Red” `
  -backgroundcolor “Black”
  $SendZabbix = 1000000
  }

 

String Search in File

# PowerShell cmdlet to find a string pattern in a file.
# Parameter 1 = the file to be searched    (mandatory)
# Parameter 2 = the string to find in the file   (mandatory)
# Parameter 3 = the length of the string   (optional)
# Parameter 4 = the delimiter between strings   (optional)

# parameters 3 and 4 are used in conjuction and are used to find a string
# of a certain length that is clearly seperated from other text in the file
# with a delimiter of some sort.

# When parameters 3 and 4 are not specified then finding he string is any line
# is sufficent to report found.
Param($Location, $SearchStr, $StrLen, $SplitToken)

If ($Location -eq $null -or $SearchStr -eq $null)
{
 write-host “Syntax: test.ps1 <File Path> <Search String> | <Length of String> <Delimiter>”
 write-host “”
 write-host “parameters 3 and 4 are optional but must be used in conjunction.”
 write-host “When not used the routine does a simple search for the string in every line.”
 write-host “When parameters 3 and 4 are specified the routine searches for the string of a certain length”
 Write-host “and that the string is delimited from the rest of line in someway.”

 write-host 0
}

$SendZabbix = 0
If ($SplitToken -eq $null -and $StrLen -eq $null)
{
 $str = select-string -pattern $SearchStr -path $Location

 If ($str -ne $null)
 { 
  $SendZabbix = 1
 }

}
Else
{
 foreach ( $xx in (select-string -pattern $SearchStr -path $Location | foreach-object {$_.line} | foreach-object  {$_.split($SplitToken)}) )
 {
  if ( ($xx.length -eq $StrLen) -and ($xx -eq $SearchStr) )
  {
   $SendZabbix = 1
  }
 }
}

Leave a Reply