PowerCLI

 View Only
  • 1.  export-csv data issue

    Posted Aug 27, 2025 02:29 PM
    Edited by dbutch1976 Aug 27, 2025 04:05 PM

    Hello,

    I've put together this script drawing heavily from LucD's similar scripts (thanks LucD!) which imports information from a legacy .csv file, and then looks for the VMs on the vCenter in order to provide updated status's. It also checks to see if the VM no longer exists at all:

    The script is working with one major exception, if the source .csv file has a field in which there is an extra line it's pushing that onto the next line. In other words if there is the migration strategy field contains:

    Scheduled

    Everything works, but if the migration strategy field has an extra line, EG:

    Scheduled
    Rescheduled due to change freeze

    This will end up corrupting the output. Is there a way to fix this?


    #Setup column labels in output file
    $outfilname = "VM_Decom_status_Primary"
    $outfilepath = "C:\Output\"
    "VMNAME,currentstatus,Migration_Strategy,PreviousPowerstate,CurrentPowerstate,vCenter,datacenter,Core_Project,Barcode,Migration_Prime,Senior_Manager,Director,VP,PreviousProjectStatus,PreviousProjectNotes,CurrentProjectStatus,CurrentProjectNotes" > "$outfilepath$outfilname.csv"
    
    #Import the source file
    Import-Csv "C:\input\VMS_Main_VC.csv" -PipelineVariable row |
    ForEach-Object -Process {
        $check = Get-VM -Name $row.VM -ErrorAction SilentlyContinue
        If ($check) {
        $currentstatus = "FOUND"
        } Else {
        $currentstatus = "VM NOT FOUND"
        } 
        If ($check -eq "FOUND") {
        $VMNAME     = $row.VM
        $Migration_Strategy = $row.'Migration Strategy'
        $PreviousPowerstate = $row.Powerstate
        $CurrentPowerstate = $check.PowerState
        $vCenter    = $check.Uid.Split('@')[1].Split(':')[0].Split('.')[0]
        $datacenter = (Get-Datacenter -VM $check).Name
        $Core_Project = $row.'Core Project'
        $Barcode = $row.Barcode
        $Migration_Prime = $row.'Migration prime'
        $Senior_Manager = $row.'Senior Manager'
        $Director = $row.Director
        $VP = $row.VP
        $PreviousProjectStatus = $row.'Project Status'
        $PreviousProjectNotes = $row.'Project Notes'    
        $CurrentProjectStatus = "Requires Update"
        $CurrentProjectNotes = "Requires Update"
        }
        Else {
        $VMNAME     = $row.VM
        $Migration_Strategy = $row.'Migration Strategy'
        $PreviousPowerstate = $row.Powerstate
        $CurrentPowerstate = $null
        $vCenter    = $row.vCenter
        $datacenter = $row.Datacenter
        $Core_Project = $row.'Core Project'
        $Barcode = $row.Barcode
        $Migration_Prime = $row.'Migration prime'
        $Senior_Manager = $row.'Senior Manager'
        $Director = $row.Director
        $VP = $row.VP
        $PreviousProjectStatus = $row.'Project Status'
        $PreviousProjectNotes = $row.'Project Notes'    
        $CurrentProjectStatus = "Requires Update"
        $CurrentProjectNotes = "Requires Update"
        }
    
    write-host $VMNAME $currentstatus $Migration_Strategy $PreviousPowerstate $CurrentPowerstate $vCenter $datacenter $Core_Project $Barcode $Migration_Prime $Senior_Manager $Director $VP $ProjectStatus $ProjectNotes
    $output = $VMNAME,$currentstatus,$Migration_Strategy,$PreviousPowerstate,$CurrentPowerstate,$vCenter,$datacenter,$Core_Project,$Barcode,$Migration_Prime,$Senior_Manager,$Director,$VP,$PreviousProjectStatus,$PreviousProjectNotes,$CurrentProjectStatus,$CurrentProjectNotes -join ","
    $output >> "$outfilepath$outfilname.csv"
    
    }
    
    



    -------------------------------------------



  • 2.  RE: export-csv data issue

    Posted Oct 02, 2025 09:17 PM

    1- You didnt define an Export-CSV command variable.

    2- you didnt use join-path to create a full file name and its full path

    3- You must use echo instead of Write-Host

    -------------------------------------------