E
E
Evgeny Kotov2022-03-24 18:04:13
PowerShell
Evgeny Kotov, 2022-03-24 18:04:13

How to properly populate an array?

Good day!

The question is more about the syntax and correctness of writing the code.

So there is zabbix to check the VPN status
On the WIN client via SNMP we get the name and status, a script was written for this

<#


    
    {"data":[{"{#VPN.NAME}":"vpn_roes","{#VPN.NAME.STATUS}":1}]

#>

function Make-LLD () {
    

 For ($i=1; $i -le 99; $i++){

  $VPNName =  Get-SnmpData -IP 192.168.103.1 -Community public -OID 1.3.6.1.4.1.890.1.6.22.2.4.1.2.+$i | select -Expand Data
  $VPNStatus = Get-SnmpData -IP 192.168.103.1 -Community public -OID 1.3.6.1.4.1.890.1.6.22.2.4.1.5.+$i | select -Expand Data   
  if ($VPNName -eq 'NoSuchInstance'){Break}
  $vpns += @{Name = "{#VPN.NAME}"; e={$VPNName}},
           @{Name = "{#VPN.NAME.STATUS}"; e={$VPNStatus}}
 }
    return ConvertTo-Json @{"data" = [array]$vpns} -Compress
    }

 Write-Host $(Make-LLD)


after the execution of which such a line should be generated
{"data":[{"{#VPN.NAME}":"vpn_roes","{#VPN.NAME.STATUS}":1}]


but somehow the syntax is very bad for me and the table is not filled The

question is the correct spelling of the syntax

Thank you all in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MaxKozlov, 2022-03-24
@eskotov

You're missing the initial [email protected]() assignment,
but the true powershell way is something like this:

function Make-LLD () {
 For ($i=1; $i -le 3; $i++){
  $VPNName =  ...
  $VPNStatus = 
  if ($VPNName -eq 'NoSuchInstance'){Break}
  @{'#VPN.NAME' = $VPNName; '#VPN.NAME.STATUS' = $VPNStatus}
}
   
}
$vpns = Make-LLD
$result = ConvertTo-Json @{"data" = [array]$vpns} -Compress
 Write-Host $result

That is, the function returns data through output, and they are already converted to another format outside

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question