Ad Widget

Collapse

Elevated Powershell using Zabbix Agent

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • frater
    Senior Member
    • Oct 2010
    • 340

    #1

    Elevated Powershell using Zabbix Agent

    This is my first powershell script I wrote.
    I much prefer bash, but with the help of Google I was able to finish this... ;-)
    After finding out that one of the servers we're monitoring had a harddisk that wasn't being back-uped I wanted a way to monitor this.

    The powershell script is working, but it needs to be run as administrator.
    I haven't even tried to run it from Zabbix as I know the first command would fail.

    I have this now in zabbix_agentd.conf

    Code:
    UserParameter=reg.info[*],%systemroot%\system32\cscript.exe /nologo /T:10 "C:\Program Files (x86)\Zabbix Agent\reginfo.vbs" "$1" "$2" $3 "$4"
    UserParameter=net.ping[*], ping -n 1 -w 1 "$1" 2>null | find  /i "TTL=" /c
    UserParameter=vfs.files.exists[*], dir "$1" 2>null | find /c "bytes "
    UserParameter=vfs.files.countsuffix[*], dir "$2\*.$1" 2>null | find /c /i ".$1"
    UserParameter=system.notbackuped,powershell -File "C:\Program Files (x86)\Zabbix Agent\NotBackuped.ps1"
    It's the last line...
    What is needed to run that file using administrator privileges?
    Hopefully this can be done with a minimal impact on general security.

    Here's the script for those interested:

    Code:
    add-pssnapin windows.serverbackup 
    
    # Get Harddisks with a driveletter
    $harddisks = GET-WMIOBJECT -query "SELECT * from win32_logicaldisk where Size > 49999999999 and DriveType = 3"  | Where-Object {$_.VolumeName -NotMatch 'Back'} | ForEach-Object {$_.DeviceID}
    
    # Extract only the driveletters
    # $harddisks = select-string -pattern "DeviceID" WMI_LOGICALDISKS | foreach { $_.ToString().split(" ")[6] }
    
    # Get Windows Backup Policy
    Get-WBPolicy >WBPolicy
    
    # Extract the line containing only the backuped items
    select-string -pattern "VolumesToBackup" WBPolicy | foreach { $_.ToString().split("{")[1] } >BACKUPED
    
    $notbackuped = ""
    
    # Parse file with driveletters and check if they exist in the current backup-set
    foreach ($line in $harddisks)
    {
      $isbackuped = select-string -pattern $line BACKUPED
      if (!$isbackuped) {
        $notbackuped = "$notbackuped $line"
      }
    }
    
    # Write a dot for a system that has all harddisks backuped or the missing harddisks
    if ($notbackuped) {
      Write-Host $notbackuped
    }
    else
    {
      Write-Host "."
    }
    Last edited by frater; 15-01-2015, 19:26.
    Zabbix agents on Linux, FreeBSD, Windows, AVM-Fritz!box, DD-WRT and QNAP
  • frater
    Senior Member
    • Oct 2010
    • 340

    #2
    Somehow it just works.
    No need to run it from an elevated command.
    I didn't expect it to run because I needed to run powershell as administrator otherwise I couldn't even execute the first command (the snap-in).

    But I was trying my Zabbix installer and noticed it just worked....

    So... No questions.
    Maybe move this thread to cookbook???

    Now I need to install the new agent on all these Windows Machines....

    Edit: It doesn't work in Windows 2008R2, I'm not really interested in why. These 2 clients are postponing that upgrade already too long... (not to mention 2 clients with 2003)
    Last edited by frater; 15-01-2015, 22:07.
    Zabbix agents on Linux, FreeBSD, Windows, AVM-Fritz!box, DD-WRT and QNAP

    Comment

    Working...