Automation

 View Only
  • 1.  How to add plain text row in a CSV

    Posted Jan 04, 2023 11:43 PM

    I have a CSV showing some VM stats for each dept, but I would like to add a simple plain text line between each dept for ease of reading. Any help is greatly appreciated.

    Here is some of the powershell script:

    Connect-VIServer $VC -Credential $Cred

    $date=Get-Date -format "yyyy-MMM-dd"
    $datetime=Get-Date
    $filelocation="/var/www/Reports/Overview/Overview-v2-$date.htm"
    $csvfilelocation="/var/www/Reports/Overview/CSV/Overview-v2-$date.csv"

    $HDReport = Get-VM $HDVMs | Select-Object Name,@{N="Datastore";E={$_.ExtensionData.Config.Files.VmPathName.Split(']')[0].TrimStart('[')}},VMHost,UsedSpaceGB,MemoryGB,NumCPU | Sort-Object -Property Name,Datastore

    $HDReport | Export-Csv -Append -path $csvfilelocation

    Here's the current CSV:
    https://pasteboard.co/9zJ7YB9Dd7EE.png



  • 2.  RE: How to add plain text row in a CSV

    Posted Jan 04, 2023 11:49 PM

    Then it would no longer be a CSV file.

    I would add a field for 'department' and then add the correct dept. name to each row.

    Where is the department name coming from?



  • 3.  RE: How to add plain text row in a CSV
    Best Answer

    Posted Jan 05, 2023 10:05 AM

    You could do

    Connect-VIServer $VC -Credential $Cred
    
    $date=Get-Date -format "yyyy-MMM-dd"
    $datetime=Get-Date
    $filelocation="/var/www/Reports/Overview/Overview-v2-$date.htm"
    $csvfilelocation="/var/www/Reports/Overview/CSV/Overview-v2-$date.csv"
    
    $HDReport = Get-VM $HDVMs | 
        Select-Object Name,
            @{N="Datastore";E={$_.ExtensionData.Config.Files.VmPathName.Split(']')[0].TrimStart('[')}},VMHost,UsedSpaceGB,MemoryGB,NumCPU | 
            Sort-Object -Property Name,Datastore
    $HDReport |
    ForEach-Object -Process {
        New-Object -TypeName PSObject -Property ([ordered]@{
            Datastore = $_.Datastore
            VMHost = $_.VMHost
            UsedSpaceGB = $_.UsedSpaceGB
            MemoryGB = $_.MemoryGB
            NumCPU = $_.NumCPU
        })
        New-Object -TypeName PSObject -Property ([ordered]@{
            Datastore = ''
            VMHost = ''
            UsedSpaceGB = ''
            MemoryGB = ''
            NumCPU = ''
        })
    } | Export-Csv -Append -path $csvfilelocation