How do I create a custom array in powershell?

Braden Pintar

I am trying to sort out arrays in PS. The problem I am trying to solve is to return a list of replicated VMs and some basic stats.

Having read through a multitude of sites and suggestions the best I could get is the following script:

$myArray = @()

$vms = get-vm | where-object { $_.replicationstate -ne "Disabled" }

foreach ($vm in $vms)
{
    $vmRepl = Get-VMReplication

    $replfreq = (New-TimeSpan -seconds $vmRepl.replicationfrequencysec)
    $lastrepl = $vmRepl.lastreplicationtime
    $nextrepl = $lastrepl + $replfreq
    $secfrom = [math]::Round((New-TimeSpan -start     $vmRepl.lastreplicationtime).TotalSeconds)
    $secto = [math]::Round((New-TimeSpan -end ($vmRepl.lastreplicationtime + $replfreq)).TotalSeconds)

    $obj = New-Object System.Object
    $obj | Add-Member -MemberType NoteProperty -Name Name -Value $vmRepl.Name
    $obj | Add-Member -MemberType NoteProperty -Name ReplicationFrequencySec -Value $vmRepl.replicationfrequencysec
    $obj | Add-Member -MemberType NoteProperty -Name SecondsSinceLastRepl -Value $secfrom
    $obj | Add-Member -MemberType NoteProperty -Name SecondsUntilNextRepl -Value $secto
    $obj | Add-Member -MemberType NoteProperty -Name LastReplication -Value $lastrepl
    $obj | Add-Member -MemberType NoteProperty -Name NextReplication -Value $nextrepl

    $myArray += $obj

}

write-output $myArray | ft -AutoSize

This works when I only have one VM, but when there are multiple ones the output appears within curly braces.

I think I am on the right track finally. I just need someone to help me sort out the remaining piece(s) of the puzzle.

The other weird thing is that the New-TimeSpan stops working with multiple VMs.

Thanks in advance.

Braden

bluuf

The biggest probem with your script is : you start a foreach loop but you don't use any element from the array you're looping through. You just loop through the same data for each item in the array.

Basicly the current script retreives a list of VMs, then for each entry you fetch the replication status of all the machines in the array. Then you do some processing on this set and then add this set to a new object (and this goes on for each entry in your list). For a good explanation on the usage of foreach see http://blogs.technet.com/b/heyscriptingguy/archive/2014/04/28/basics-of-powershell-looping-foreach.aspx

I would also suggest to use [PSCustomObject] instead of new-object / add-member : it's easier to use, the code is easier to read and it also maintains the order of the properties you set with it (since you're using get-vm I assume you have PS3 or higher)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I create an anonymous object in PowerShell?

From Dev

How do I create a custom element in dart?

From Dev

How do I create a custom nonlinear filter?

From Dev

How do I create a custom 4x4 array using NumPy?

From Dev

How do I create a custom struct to assign the contents of array into usable variables?

From Dev

How do I create an extension to allow an array of a custom type to conform to a protocol?

From Dev

How do I create a custom cursor with a custom hotspot in wxPython?

From Dev

How do I create an array of tuples?

From Dev

How do I create and initialize an immutable array?

From Dev

How do I create type for array of ProvidedType?

From Dev

How do I create an array of objects in json?

From Dev

How do I create an array of View Controllers?

From Dev

How do I create and initialize an immutable array?

From Dev

How do I create an array of strings?

From Dev

How do I create an Array of Objects in C?

From Dev

Powershell - how do I edit an existing property in a custom object

From Dev

Powershell - how do I add multiple objects to a property on a custom object?

From Dev

How do I create an alias to Format-Table in Powershell?

From Dev

How do i invoke sc create from a powershell script

From Dev

How do I create a PowerShell alias (or function) that contains an option?

From Dev

Laravel: How do I create a custom pivot model?

From Dev

How do I properly create custom text codecs?

From Dev

How do I create a custom control that accepts <Run/> text?

From Dev

How do I create a custom namespace or class in javascript

From Dev

How do I create a custom scala library using sbt?

From Dev

How do I create a custom HTML tag, without using customElement?

From Dev

How do I create a custom event in an AngularJs service

From Dev

How do I create a custom entity that can be read by all users?

From Dev

how do i create a custom uiview from a xib

Related Related

  1. 1

    How do I create an anonymous object in PowerShell?

  2. 2

    How do I create a custom element in dart?

  3. 3

    How do I create a custom nonlinear filter?

  4. 4

    How do I create a custom 4x4 array using NumPy?

  5. 5

    How do I create a custom struct to assign the contents of array into usable variables?

  6. 6

    How do I create an extension to allow an array of a custom type to conform to a protocol?

  7. 7

    How do I create a custom cursor with a custom hotspot in wxPython?

  8. 8

    How do I create an array of tuples?

  9. 9

    How do I create and initialize an immutable array?

  10. 10

    How do I create type for array of ProvidedType?

  11. 11

    How do I create an array of objects in json?

  12. 12

    How do I create an array of View Controllers?

  13. 13

    How do I create and initialize an immutable array?

  14. 14

    How do I create an array of strings?

  15. 15

    How do I create an Array of Objects in C?

  16. 16

    Powershell - how do I edit an existing property in a custom object

  17. 17

    Powershell - how do I add multiple objects to a property on a custom object?

  18. 18

    How do I create an alias to Format-Table in Powershell?

  19. 19

    How do i invoke sc create from a powershell script

  20. 20

    How do I create a PowerShell alias (or function) that contains an option?

  21. 21

    Laravel: How do I create a custom pivot model?

  22. 22

    How do I properly create custom text codecs?

  23. 23

    How do I create a custom control that accepts <Run/> text?

  24. 24

    How do I create a custom namespace or class in javascript

  25. 25

    How do I create a custom scala library using sbt?

  26. 26

    How do I create a custom HTML tag, without using customElement?

  27. 27

    How do I create a custom event in an AngularJs service

  28. 28

    How do I create a custom entity that can be read by all users?

  29. 29

    how do i create a custom uiview from a xib

HotTag

Archive