1

I have an object which contains multiple arrays of strings. Nothing I do can display these in a decent format in Powershell. I have looked everywhere on this site and many others and been unable to find an answer. Is this something that should be done with Python or a separate programming language? It appears powershell doesn't want users to export arrays to CSV. Anyways let me know if you all have a fix for this.

Import-Module VMware.VimAutomation.Core

Connect-VIServer -Server vcenter-appliance.swacu.net -Force   $prodservers = get-vm | select Name  Connect-VIServer -Server drvcenter-appliance.swacu.net -Force  $replicaserversfull = get-vm | select Name
    
    
$notinprod = Foreach($replica in $replicaservers){
        $shortname = $replica.name.trim('_replica')
        $shortnametwo = if($shortname -like "*_TTN"){$shortname.trim('_TTN')}
        $shortnames = New-Object -TypeName PSObject -Property @{
        ShortName = $replica.name.trim('_replica')   
            }
         
        if($prodservers.name -contains $shortname){
            
        }
        elseif($prodservers.name -contains $shortnametwo) {
            
        }
        else{
        New-Object -TypeName PSObject -Property @{
        SystemName = $replica.name    
            }   | Select SystemName
        } } 
$NotInReplica = Foreach($prodserver in $prodservers){
        $half = $replicaservers.name.trim('_replica')
        $correct =  if($half -like "*_TTN"){$half.trim('_TTN')}
        
        if($half -contains $prodserver.name){
           
        }
        elseif($correct -contains $prodserver.name) {
            
        }
        
        else{
        New-Object -TypeName PSObject -Property @{
        SystemName = $prodserver.name    
            }   | Select SystemName
        }
    
    } 
Write-host $half $whole = New-Object -TypeName PSObject -Property @{
        Replica = $replicaservers.name | out-string 
        Prod =   $prodservers.name | out-string 
        NotInProd = $notinprod.SystemName | out-string 
        NotInReplication = $NotInReplica.SystemName | out-string 
            }   
         $whole | Select-Object prod, replica, NotInReplication, notinprod  | Out-file -Path '.\Results\Noprod.csv'

This would be a good example of how I want it to look in a CSV

This would be a fine output. Or having them each in their own column.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
lakerskill
  • 1,019
  • 1
  • 11
  • 24
  • Please add an example of what you expect the `csv` file should look like. I suspect you want to do something like in this question: [Convert nested JSON array into separate columns in CSV file](https://stackoverflow.com/q/45829754/1701026) but that isn't clear from the question. – iRon Feb 14 '23 at 19:30
  • 1
    There's a few different issues with your code that need addressing - for example ```Trim("_replica")``` is invoking the ```Trim(Char[])``` overload which means it's removing any trailing characters that appear in ```_replica```, not just the ```_replica``` suffix - for example ```"myserver_replica".Trim("_replica")``` becomes ```myserv``` because the ```e``` and ```r``` at the end of ```myserver``` appear in the characters ```@("_", "r", "e", "p", "l", "i", "c", "a ")```. – mclayton Feb 14 '23 at 20:57
  • 2
    You might also want to fix the line breaks and indenting in your code sample :- it took me a while to realise there are *two** top-level ```foreach``` expressions -) – mclayton Feb 14 '23 at 20:58
  • Yeah I fixed, don't know how it got so out of place! Thanks though. – lakerskill Feb 14 '23 at 22:11
  • 2
    Is your intent really to create just _one_ output object? You _can_ create multi-line CSV files, but only if you use `Export-Csv`, not `Out-File`. Note that since v3 PowerShell has syntactic sugar for creating custom objects with `[pscustomobject] @{ ... }`, which - despite appearing to use hashtables - _does_ preserve definition order of the entries. Your reformatted code still has multiple statements on a single line (without separating them with `;`), and `$replicaservers` is not defined, only `$replicaserversfull`. Also, better to use ``-join "`n"`` than `Out-String` – mklement0 Feb 14 '23 at 22:23
  • 3
    Code indentation and line breaks are still kinda bad. – Joel Coehoorn Feb 14 '23 at 22:36

1 Answers1

0

It appears powershell doesn't want users to export arrays to CSV.

Unless you use Export-CSV instead of Out-File.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794