Importing powershell outputs to InfluxDB

Hello,

I have created a new InfluxDB to allow me to see if I can import information from powershell that is on a remote Windows server into my InfluxDB. I’m failing miserably.

I want to import basic counters from my Windows server and see if I can get them to show in Grafana:

I’m trying to run this on the Windows server:

$influxDB = "http://192.168.18.22:8086"

while($true)
{
$valuesList Get-Counter -Counter “\Memory\Available Bytes”,“\Processor(*)% Processor Time”
$MemoryActive = [math]::Round($valuesList.CounterSamples[0[.CookedValue,3) / 1024 /1024
$MemoryFreeMB = [math]::Round($MemoryActive)
$processorActive = [math]::Round($valuesList.CounterSamples[1].CookedValue,2)

$Metrics = @{
“cpu active” = $processorActive
“Free RAM MB” = $MemoryFreeMB
}
Write-Influx -Measure Server -Tags @{Server=$env:COMPUTERNAME} -Metrics $Metrics -Database TestDB -Server http://192.168.18.22:8086 -Verbose

I get the errors:

At line:4 char:13
  • $valuesList Get-Counter -Counter “\Memory\Available Bytes”,"\Processo …
  •         ~~~~~~~~~~~
    

Unexpected token ‘Get-Counter’ in expression or statement.
At line:5 char:58

  • $MemoryActive = [math]::Round($valuesList.CounterSamples[0[.CookedVal …
  •                                                      ~
    

Array index expression is missing or not valid.
At line:5 char:58

  • $MemoryActive = [math]::Round($valuesList.CounterSamples[0[.CookedVal …
  •                                                      ~
    

Missing ‘)’ in method call.
At line:5 char:58

  • … ive = [math]::Round($valuesList.CounterSamples[0[.CookedValue,3) / 10 …
  •                                                ~~~~~~~~~~~~~~
    

Unexpected token ‘0[.CookedValue’ in expression or statement.
At line:5 char:72

  • … ve = [math]::Round($valuesList.CounterSamples[0[.CookedValue,3) / 102 …
  •                                                             ~
    

Missing argument in parameter list.
At line:3 char:1

  • {
  • ~
    Missing closing ‘}’ in statement block or type definition.
    At line:5 char:74
  • … = [math]::Round($valuesList.CounterSamples[0[.CookedValue,3) / 1024 …
  •                                                             ~
    

Unexpected token ‘)’ in expression or statement.
+ CategoryInfo : ParserError: (:slight_smile: , ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken

I was following this guide How to turn Powershell data into dashboards with Influxdb and Grafana - YouTube

Can you see where I have gone wrong?

Thanks

Hello

It seems there are some errors in your PowerShell the corrected code is:

while($true) {
    $valuesList = Get-Counter -Counter "\Memory\Available Bytes","\Processor(*)\% Processor Time"
    $MemoryActive = [math]::Round($valuesList.CounterSamples[0].CookedValue,3) / 1024 /1024
    $MemoryFreeMB = [math]::Round($MemoryActive)
    $processorActive = [math]::Round($valuesList.CounterSamples[1].CookedValue,2)

    $Metrics = @{
    “cpu active” = $processorActive
    “Free RAM MB” = $MemoryFreeMB
    }
    Write-Influx -Measure Server -Tags @{Server=$env:COMPUTERNAME} -Metrics $Metrics -Database TestDB -Server http://192.168.18.22:8086 -Verbose
}

Also to confirm you are using the Influxdb v1.x version and not the version 2?

Have a great day