Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell-for each return single line

I created a script for returning Azure vault replicated status for every AzureVM

 $items = Get-AzureRmRecoveryServicesAsrReplicationProtectedItem -ProtectionContainer $Containers
    foreach ($item in $items)
  {
   $a=$item.ReplicationHealth,$item.RecoveryAzureVMName
   write-host $a  
}

getting desired output:

Normal VM1
Normal VM2
Critical VM3
Normal VM4
Critical VM5

I need above output to pass to zabbix.Unfortunately, zabbix can accept only one line in single loop,so i need to modify for loop somehow,to return first line in output, then in second run to return second line and so on

for getting first line (Normal VM1) i tried

foreach ($item in $items)
{
write-host $item.ReplicationHealth,$item.RecoveryAzureVMName | select -First 1
}

but i'm getting all five lines in output, is it possible to put output to array and then to return particular line from it ?


1 Answers

By passing items this way you will enumerate them instead of concatenate results.

Try this way:

foreach ($item in $items)
{
    $a = '{0} {1}' -f $item.ReplicationHealth,$item.RecoveryAzureVMName
    Write-Host $a  
}

Using -f operator starts with a format string, followed by one or more objects or expressions that will be converted to strings and inserted at a specified place in the format string.

The {0} in the format string is a format item. 0 is the index of the object whose string value will be inserted at that position. (Indexes start at 0.) If the object to be inserted is not a string, its ToString method is called to convert it to one before inserting it in the result string.

Example:

'{0} is an {1}' -f 'This','example'
This is an example
like image 175
Kirill Pashkov Avatar answered Sep 25 '26 19:09

Kirill Pashkov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!