Issue with tab.

Mawu 21 Reputation points
2022-02-17T21:28:13.747+00:00

Hello,
I need some help because, i have an issue with a tab.

Bellow my code i don't have any error when it running.

I want to clarify that in this code i've add variable for you to see it's not empty
We can see the "foreach" workshops correctly but
$tablivemounttemp is empty consequently $tablivemount is null

Write-Host " Récupération des Instant Recovery VM en cours" -ForegroundColor Magenta  
  
$tablivemount = @()  
  
$vm_live_mount = Invoke-RubrikRESTCall -Endpoint vmware/vm/snapshot/mount -api 1 -Method GET  
  
   
if ($vm_live_mount.total -eq 0){  
  
    $tablivemounttemp = @()  
  
    $tablivemounttemp = New-Object psobject  
  
    $tablivemounttemp | Add-Member -NotePropertyName "DestinationId" -NotePropertyValue "Pas de migration en attente"  
  
    $tablivemount +=$tablivemounttemp}  
  
    Else{  
  
        forEach($LiveMount in $vm_live_mount.data){  
  
        $DestinationId = ($ALLmanagedvm.data |Where-Object {$_.id -eq $LiveMount.VmId}).name  
  
        $DestinationId  
  
        $SourceId = ($ALLmanagedvm.data |Where-Object {$_.id -eq $LiveMount.mountedVmId}).name  
  
        $SourceId  
  
        $mountTimestamp = ([datetime]$LiveMount.mountTimestamp).ToLocalTime().ToString("d/MM/y HH:mm:ss")  
  
        $mountTimestamp  
  
        $snapshotDate = ([datetime]$LiveMount.snapshotDate).ToLocalTime().ToString("d/MM/y HH:mm:ss")  
  
        $snapshotDate  
  
        $tablivemounttemp = @()  
  
        $tablivemounttemp = New-Object psobject  
  
        $tablivemounttemp | Add-Member -NotePropertyName "DestinationId" -NotePropertyValue $DestinationId  
  
        $tablivemounttemp | Add-Member -NotePropertyName "SourceId" -NotePropertyValue $SourceId  
  
        $tablivemounttemp | Add-Member -NotePropertyName "Datedemontage" -NotePropertyValue $mountTimestamp  
  
        $tablivemounttemp | Add-Member -NotePropertyName "Datedusnapshot" -NotePropertyValue $snapshotDate  
  
        $tablivemount +=$tablivemounttemp}  
  
    }  
  
$tablivemount | Select-Object DestinationId,SourceId,Datedemontage,Datedusnapshot  
  
Récupération des Instant Recovery VM en cours  
  
aaaaaaa  
  
aaaaaaa  
  
16/02/22 16:19:36  
  
15/02/22 19:30:10  
  
bbbbbb  
  
bbbbbb  
  
16/02/22 16:19:04  
  
15/02/22 19:30:10  
  
   
  
DestinationId SourceId Datedemontage Datedusnapshot  
  
------------- -------- ------------- --------------  

Someone van help me?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,504 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 46,551 Reputation points
    2022-02-18T15:47:00.197+00:00

    Somewhere in your script you've set $tablivemount to be a variable having a type of "string". When you use concatenation (i.e., the "+=") operator PowerShell is using the "ToString" method of the PSCustomObject and concatenating that string to the existing value in $tablivemount.

    Change the definition of that variable to [array]$tablivemount = @() on line 6 of my code example and rerun the code.

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Rich Matheisen 46,551 Reputation points
    2022-02-18T03:11:05.173+00:00

    What version of PowerShell are you using? A long time ago (PowerShell 2) it was necessary to use the "-Passthru" parameter with the "Add-Member" cmdlet.

    This is a slight rewrite of your code. It does the same thing as your code but doesn't use Add-Member. Does it produce the same result?

    Write-Host " Récupération des Instant Recovery VM en cours" -ForegroundColor Magenta
    #################################
    # Where is $ALLmanagedvm defined?
    #################################
    
    $tablivemount = @()
    
    $vm_live_mount = Invoke-RubrikRESTCall -Endpoint vmware/vm/snapshot/mount -api 1 -Method GET
    if ($vm_live_mount.total -eq 0) {
        $tablivemount += [PSCustomObject]@{
                            DestinationId = "Pas de migration en attente"
                            SourceId = ""
                            Datedemontage = ""
                            Datedusnapshot = ""
                         }
    }
    Else {
        ForEach ($LiveMount in $vm_live_mount.data) {
            $tablivemount += [PSCustomObject]@{
                                DestinationId = ($ALLmanagedvm.data | Where-Object { $_.id -eq $LiveMount.VmId }).name
                                SourceId = ($ALLmanagedvm.data | Where-Object { $_.id -eq $LiveMount.mountedVmId }).name
                                Datedemontage = ([datetime]$LiveMount.mountTimestamp).ToLocalTime().ToString("d/MM/y HH:mm:ss")
                                Datedusnapshot = ([datetime]$LiveMount.snapshotDate).ToLocalTime().ToString("d/MM/y HH:mm:ss")
                            }
        }
    }
    
    $tablivemount
    

    Note: I don't see the variable $ALLmanagedvm in your example.

    0 comments No comments

  2. Mawu 21 Reputation points
    2022-02-18T13:28:51.133+00:00

    Hello,

    I using powershell 5.

    I've tried your code and it's works

    $tablivemount = @()  
      
    $vm_live_mount = Invoke-RubrikRESTCall -Endpoint vmware/vm/snapshot/mount -api 1 -Method GET  
      
    if ($vm_live_mount.total -eq 0){  
      
        $tablivemount += [PSCustomObject]@{  
      
            DestinationId = "Pas de migration en attente"  
            SourceId = ""  
            Datedemontage = ""  
            Datedusnapshot= ""}  
        }  
      
        Else{  
      
            forEach($LiveMount in $vm_live_mount.data){  
            $tablivemount += [PSCustomObject]@{  
                DestinationId = ($ALLmanagedvm.data |Where-Object {$_.id -eq $LiveMount.VmId}).name  
                SourceId = ($ALLmanagedvm.data |Where-Object {$_.id -eq $LiveMount.mountedVmId}).name  
                Datedemontage = ([datetime]$LiveMount.mountTimestamp).ToLocalTime().ToString("d/MM/y HH:mm:ss")  
                Datedusnapshot= ([datetime]$LiveMount.snapshotDate).ToLocalTime().ToString("d/MM/y HH:mm:ss")  
      
                }  
      
            }  
      
        }  
    $tablivemount  
    

    The return is :

    $tablivemount  
      
    @{DestinationId=aaaaa; SourceId=aaaaa; Datedemontage=18/02/22 09:18:37; Datedusnapshot=15/02/22 19:30:10}@{DestinationId=aaaaa; SourceId=bbbbb; Datedemontage=18/02/22 09:19:00; Datedusnapshot=14/02/22 19:33:27}  
    

    How i can exploit with Format Table ?

    $tablivemount | Select-object DestinationId, SourceId | FT  
      
    DestinationId SourceId  
      
    ------------- --------  
      
                           
    

    $ALLmanagedvm is declare on another part of this script.

    Just for information I ve also tried my script with -passthru and it return that:

    ...  
    $tablivemount_temp = @()  
      
            $tablivemount_temp = New-Object psobject  
            $tablivemount_temp | Add-Member -NotePropertyName "DestinationId" -NotePropertyValue $DestinationId -PassThru  
            $tablivemount_temp | Add-Member -NotePropertyName "SourceId" -NotePropertyValue $SourceId -PassThru  
            $tablivemount_temp | Add-Member -NotePropertyName "Datedemontage" -NotePropertyValue $mountTimestamp  
            $tablivemount_temp | Add-Member -NotePropertyName "Datedusnapshot" -NotePropertyValue $snapshotDate  
            $tablivemount +=$tablivemount_temp}  
        }  
      
    $tablivemount | Select-Object DestinationId, SourceId, Datedemontage, Datedusnapshot  
    

    it's return

    $tablivemount | Select-Object DestinationId, SourceId, Datedemontage, Datedusnapshot  
      
    Récupération des Instant Recovery VM en cours  
      
    DestinationId  
    -------------  
    aaaaaa  
    aaaaaa  
    

    If you can explain me what's wrong ?

    0 comments No comments

  3. Mawu 21 Reputation points
    2022-02-23T20:16:54.16+00:00

    Hello,

    You're right. when I change definition of my variable the data are put in.
    I get the result like i want :)

    Thank you for helped me

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.