Powershell Datapoints
Sorry for spamming but i cant find a way to edit my post and i can reply to my post to add infor
My Second Question is regarding the configuration of datapoints.
Can some give me an example how to configure datapoints to pick up my values. Currently i tried this.
Metric type Raw metric Post Processor Alert threshold
IsAutoManaged gauge output keyvalue(IsAutoManaged)
IsOnCDrive gauge output keyvalue(IsOnCDrive)
OtherFilesExist gauge output keyvalue(OtherFilesExist)
PageFileDrive gauge output keyvalue(PageFileDrive)
My Test Datasource
# Clears the CLI of any text
Clear-Host
# Clears memory of all previous variables
Remove-Variable * -ErrorAction SilentlyContinue
#------------------------------------------------------------------------------------------------------------
# Initialize Variables
$TargetHostName = '##SYSTEM.SYSNAME##' + '.' + '##SYSTEM.DOMAIN##'
$CollectorHostName = $env:computername
$TargetUserName = '##wmi.user##'
$TargetPassword = '##wmi.pass##'
$DebugMode = $false$ScriptBlock = {
param (
[bool]$Debug
)
# Get pagefile information
$pageFileInfo = Get-CimInstance -Class Win32_PageFileUsage | Select-Object *
$CompSysResults = Get-CimInstance win32_computersystem -Namespace 'root\cimv2'# Initialize variables
$pageFileDrive = ""
$isOnC = $false
$isAuto = $false
$otherFilesExist = $falseif ($pageFileInfo) {
$pageFilePath = $pageFileInfo.Name
$pageFileDrive = Split-Path -Path $pageFilePath -Qualifier# Check if pagefile is on C: drive
$isOnC = $pageFileDrive -eq "C:"# Check if pagefile size is set to auto
$isAuto = $CompSysResults.AutomaticManagedPagefile# Output results
Write-Host "PageFileDrive=$pageFileDrive"
Write-Host "IsOnCDrive=$isOnC"
Write-Host "IsAutoManaged=$isAuto"# Check for other files/folders on pagefile drive
if ($pageFileDrive -eq "D:") {
$otherItems = Get-ChildItem -Path $pageFileDrive -Force | Where-Object { $_.FullName -ne $pageFilePath }
$otherFilesExist = $otherItems.Count -gt 0
Write-Host "OtherFilesExist=$otherFilesExist"
}}
}
if ($TargetHostName -eq $CollectorHostName)
{
Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList @($DebugMode,$Instance)
}
else
{
#If WMI creds undefined - invoke remote command without creds, if not - create credential object and use it for authorization
if (($TargetUserName -Match "wmi.user" -and $TargetPassword -Match "wmi.pass") -or
($TargetUserName -eq "" -and $TargetPassword -eq ""))
{
Invoke-Command -ComputerName $TargetHostName -ScriptBlock $ScriptBlock -ArgumentList @($DebugMode,$Instance)
}
else
{
$TargetCredentials = New-Object System.Management.Automation.PSCredential ($TargetUserName, $(ConvertTo-SecureString $TargetPassword -AsPlainText -Force))
Invoke-Command -ComputerName $TargetHostName -ScriptBlock $ScriptBlock -Credential $TargetCredentials -ArgumentList @($DebugMode,$Instance)
}
}Exit 0
Windows Cert
$Instance = '##WILDVALUE##'
$TargetHostName = '##SYSTEM.SYSNAME##' + '.' + '##SYSTEM.DOMAIN##'
$CollectorHostName = $env:computername
$TargetUserName = '##wmi.user##'
$TargetPassword = '##wmi.pass##'
$DebugMode = $false$ScriptBlock = {
param (
[bool]$Debug,
[String]$SerialNumber
)
if ($Cert = Get-ChildItem -Path cert:LocalMachine -Recurse | Where-Object { $_.SerialNumber -eq $SerialNumber } | Sort-Object -Unique) {
$TimeSpan = New-TimeSpan -Start (Get-Date) -End $Cert.NotAfter
$DaysLeft = $TimeSpan.Days
Write-Host "DaysLeft=$DaysLeft"
}
}if ($TargetHostName -eq $CollectorHostName)
{
Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList @($DebugMode,$Instance)
}
else
{
#If WMI creds undefined - invoke remote command without creds, if not - create credential object and use it for authorization
if (($TargetUserName -Match "wmi.user" -and $TargetPassword -Match "wmi.pass") -or
($TargetUserName -eq "" -and $TargetPassword -eq ""))
{
Invoke-Command -ComputerName $TargetHostName -ScriptBlock $ScriptBlock -ArgumentList @($DebugMode,$Instance)
}
else
{
$TargetCredentials = New-Object System.Management.Automation.PSCredential ($TargetUserName, $(ConvertTo-SecureString $TargetPassword -AsPlainText -Force))
Invoke-Command -ComputerName $TargetHostName -ScriptBlock $ScriptBlock -Credential $TargetCredentials -ArgumentList @($DebugMode,$Instance)
}
}Exit 0
Mike Moniz
·2 years agoI didn't go over all of that, and I'm not clear what the "Windows Cert" section is about?
One thing that I did notice is that your Datapoints must be numbers. For example $isOnC would output True or False which will not work. Your code would need to convert that to numbers, for example something like this:
$isOnC = if ($pageFileDrive -eq "C:") { 1 } else { 0 }
You can still output text in the output, but LM will ignore them (but useful when using Poll Now)
Also it looks like your code is doing a PSRemote session to the other server which then runs a WMI query on itself. Generally I would suggest just doing a direct WMI query from the collector itself using "Get-CimInstance -Computername" unless you have a specific reason to do it the PSRemote way. There is likely existing DataSources in LM that can you copy code from to see how.
LM User
·2 years agoExactly. Your datapoint output has to be a number: integer, float, signed integer, signed float.