Skip to content

Error when using "Write-Influx" without Tags #38

@DerT94

Description

@DerT94

If the command "Write-Influx" is used without tags, an error occurs.

Working Code

Get-Process -Name winlogon | ConvertTo-Metric -Measure test -MetricProperty CPU,PagedmemorySize -TagProperty Handles,Id,ProcessName | Write-Influx -Bucket test -Server https://influxserver:8086 -Token "token" -Verbose

Not working Code

Get-Process -Name winlogon | ConvertTo-Metric -Measure test -MetricProperty CPU,PagedmemorySize  | Write-Influx -Bucket test -Server https://influxserver:8086 -Token "token" -Verbose

Error

Invoke-RestMethod : {"code":"invalid","message":"unable to parse 'test, CPU=\"\" ': missing tag key\nunable to parse 'test, PagedmemorySize=233472 ': missing tag key"}
In C:\Users\ant\Documents\WindowsPowerShell\Modules\Influx\1.0.101\Public\Write-Influx.ps1:202 Zeichen:25
+ ...             Invoke-RestMethod -Uri $URI -Method Post -Body $Body -Hea ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

The error is caused by setting an incorrect comma in the function
According to the following issue, no comma may be present if no tags are transmitted
influxdata/influxdb#3600

In the code, you are using
if ($MetricObject.Tags) { #Build String for Tags }

But $MetricObject.Tags is an emptry hashtable and an empty hastable evaluates "true".
PowerShell/PowerShell#10201

You have to change the code from

  if ($MetricObject.Tags)) {
                $TagData = foreach ($Tag in $MetricObject.Tags.Keys) {
                    if ([string]::IsNullOrEmpty($MetricObject.Tags[$Tag])) {
                        Write-Warning "$Tag skipped as it's value was null or empty, which is not permitted by InfluxDB."
                    }
                    else {
                        "$($Tag | Out-InfluxEscapeString)=$($MetricObject.Tags[$Tag] | Out-InfluxEscapeString)"
                    }
                }
                $TagData = $TagData -Join ','
                $TagData = ",$TagData"
            }

to

  if (($MetricObject.Tags).count -ne 0) {
                $TagData = foreach ($Tag in $MetricObject.Tags.Keys) {
                    if ([string]::IsNullOrEmpty($MetricObject.Tags[$Tag])) {
                        Write-Warning "$Tag skipped as it's value was null or empty, which is not permitted by InfluxDB."
                    }
                    else {
                        "$($Tag | Out-InfluxEscapeString)=$($MetricObject.Tags[$Tag] | Out-InfluxEscapeString)"
                    }
                }
                $TagData = $TagData -Join ','
                $TagData = ",$TagData"
            }

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions