Automation

 View Only
Expand all | Collapse all

need export to csv all vm in txt file

kuku Forever

kuku ForeverJul 19, 2022 09:57 AM

kuku Forever

kuku ForeverJul 19, 2022 12:15 PM

  • 1.  need export to csv all vm in txt file

    Posted Jul 19, 2022 08:57 AM

    hi
    need help
    I need to export.csv  from list.txt file VMs that I have issue 
    in export csv I need VM name , State (on or off) , folder name 
    thanks 



  • 2.  RE: need export to csv all vm in txt file

    Posted Jul 19, 2022 09:01 AM

    What do you already have?
    And where do you need help?



  • 3.  RE: need export to csv all vm in txt file

    Posted Jul 19, 2022 09:10 AM

    thanks for replay 

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

    $credential = Get-Credential
    $Username = $credential.GetNetworkCredential().username
    $Password = $credential.GetNetworkCredential().password
    $vcenter1 = "xxx" #VC Prod
    $VM_list = Get-Content -Path C:\temp\VMList.txt

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

    this what I have 

     

     



  • 4.  RE: need export to csv all vm in txt file

    Posted Jul 19, 2022 09:27 AM

    You could do something like this

    Get-Content -Path C:\temp\VMList.txt |
    ForEach-Object -Process {
        Get-VM -Name $_ |
        Select Name, PowerState, @{N='Folder';E={$_.Folder.Name}}
    } | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


  • 5.  RE: need export to csv all vm in txt file

    Posted Jul 19, 2022 09:57 AM

    THX 

    its work 



  • 6.  RE: need export to csv all vm in txt file

    Posted Jul 19, 2022 10:51 AM

    hi
    I run again the scripts 

    and i get error message 

    Get-VM : Cannot validate argument on parameter 'Name'. The argument is null or
    empty. Provide an argument that is not null or empty, and then try the command
    again.
    At line:2 char:18
    + Get-VM -Name $_

     

    why ?



  • 7.  RE: need export to csv all vm in txt file

    Posted Jul 19, 2022 11:25 AM

    What exactly is in your TXT file?
    How did you run the code?



  • 8.  RE: need export to csv all vm in txt file

    Posted Jul 19, 2022 11:47 AM

    in txt file only VMs list

    like


    vm01
    vm02
    vm03
    this is my scripts
    ------------------------------------
    $credential = Get-Credential
    $Username = $credential.GetNetworkCredential().username
    $Password = $credential.GetNetworkCredential().password
    $vcenter = "vSphere" 
    Connect-VIServer $vcenter -User $Username -Password $Password
    Get-Content -Path C:\temp\VMList.txt
    ForEach-Object -Process {
    Get-VM -Name $_ |
    Select Name, PowerState, @{N='Folder';E={$_.Folder.Name}}
    } | Export-Csv -Path C:\temp\report.csv -NoTypeInformation -UseCulture
    -------------------------------------



  • 9.  RE: need export to csv all vm in txt file

    Posted Jul 19, 2022 11:53 AM

    You left out the pipeline symbol at the end of the Get-Content line



  • 10.  RE: need export to csv all vm in txt file

    Posted Jul 19, 2022 12:15 PM

    Ok 

    I got it 

    many thanks 



  • 11.  RE: need export to csv all vm in txt file

    Posted Jul 25, 2022 07:58 AM

    hi
    If I want to report the PowerState and Ip address in the same scripts 

    how do it  add in the same script?

     

    THX 



  • 12.  RE: need export to csv all vm in txt file

    Posted Jul 25, 2022 08:31 AM

    The PowerState is already in the snippet I posted earlier.

    To get the IP address you need to have the VMware Tools installed.
    Is that the case?



  • 13.  RE: need export to csv all vm in txt file

    Posted Jul 25, 2022 08:46 AM

    yes , all VMs with vmTools installed

    this is my scripts...

    $reportCPU = "C:\temp\reportCPU.csv"

    Get-VM -Name $vmNames|
    Select Name,Get-NetworkAdapter,PowerState,
    @{N='CPU';E={$_.ExtensionData.Config.Hardware.NumCpu}},
    @{N='CoresPerSocket';E={$_.ExtensionData.Config.Hardware.NumCoresPerSocket}} |
    Export-Csv -Path $reportCPU -NoTypeInformation -UseCulture

     

    but the table "Get-NetworkAdapter"  is empty 

     

     



  • 14.  RE: need export to csv all vm in txt file

    Posted Jul 25, 2022 10:32 AM

    You will need a calculated property, not just a cmdlet on the Select-Object.
    And the Get-NetworkAdapter will not give you the IP address.

    $reportCPU = "C:\temp\reportCPU.csv"
    
    Get-VM -Name $vmNames|
    Select Name,
    @{N='VniC';E={(Get-NetworkAdapter -VM $_).Name -join '|'}},
    PowerState,
    @{N='CPU';E={$_.ExtensionData.Config.Hardware.NumCpu}},
    @{N='CoresPerSocket';E={$_.ExtensionData.Config.Hardware.NumCoresPerSocket}} |
    Export-Csv -Path $reportCPU -NoTypeInformation -UseCulture


  • 15.  RE: need export to csv all vm in txt file

    Posted Jul 25, 2022 11:41 AM

    you right

    I need to know how I see after I power on the VMs if the network status is enable ( i know some time we have a bug )

    so I thinking how to do export  VMs about status network connection and IP address  

    in the same script 

     

    thanks and sorry that I not very  clear 

     



  • 16.  RE: need export to csv all vm in txt file

    Posted Jul 25, 2022 11:57 AM

    You could do something like this

    Get-VM -Name $vmNames -PipelineVariable vm |
    Get-NetworkAdapter -PipelineVariable nic |
    Select @{N='VM';E={$vm.Name}},
      @{N='PowerState';E={$vm.PowerState}},
      @{N = 'CPU'; E = { $vm.ExtensionData.Config.Hardware.NumCpu } },
      @{N = 'CoresPerSocket'; E = { $vm.ExtensionData.Config.Hardware.NumCoresPerSocket} },
      @{N='NIC';E={$nic.Name}},
      @{N='Connected';E={$nic.ConnectionState.Connected}},
      @{N='IP';E={($vm.Guest.Nics | Where-Object { $_.Device.Name -eq $nic.Name }).IPAddress -join '|'}} |
    Export-Csv -Path $reportCPU -NoTypeInformation -UseCulture


  • 17.  RE: need export to csv all vm in txt file

    Posted Jul 25, 2022 01:26 PM

    thank you 



  • 18.  RE: need export to csv all vm in txt file

    Posted Aug 07, 2022 09:51 AM

    hi again

    in the same scripts , I need to verify the status ToolsVersionStatus

    I know the command is get-view but  I can't combine it together with my existing script

    Would appreciate help

    Thanks



  • 19.  RE: need export to csv all vm in txt file

    Posted Aug 07, 2022 11:47 AM

    You can add a calculated property.

    Get-VM -Name $vmNames -PipelineVariable vm |
    Get-NetworkAdapter -PipelineVariable nic |
    Select-Object @{N = 'VM'; E = { $vm.Name } },
    @{N = 'PowerState'; E = { $vm.PowerState } },
    @{N = 'CPU'; E = { $vm.ExtensionData.Config.Hardware.NumCpu } },
    @{N = 'CoresPerSocket'; E = { $vm.ExtensionData.Config.Hardware.NumCoresPerSocket } },
    @{N = 'NIC'; E = { $nic.Name } },
    @{N = 'Connected'; E = { $nic.ConnectionState.Connected } },
    @{N = 'IP'; E = { ($vm.Guest.Nics | Where-Object { $_.Device.Name -eq $nic.Name }).IPAddress -join '|' } },
    @{N='ToolsVersion';E={$vm.Guest.ToolsVersion}} |
    Export-Csv -Path $reportCPU -NoTypeInformation -UseCulture


  • 20.  RE: need export to csv all vm in txt file

    Posted Aug 07, 2022 12:06 PM

    thanks you ,

    but I need a ToolsVersionStatus  not ToolsVersion



  • 21.  RE: need export to csv all vm in txt file

    Posted Aug 07, 2022 02:29 PM

    Hi.

    You need to add:

     

    @{N='ToolsVersionStatus';E={$vm.ExtensionData.Guest.ToolsVersionStatus}}

     



  • 22.  RE: need export to csv all vm in txt file

    Posted Aug 08, 2022 12:13 PM

    Hi guys 

    I need info about ToolsVersionStatus

    like "Running, version:10305 (Upgrade available)" or "Running, version:11333 (Guest Managed)"

     can you help me please with my Script ? 

    $vmNames = get-vm
    $vmNames | Select -ExpandProperty $vmNames1

    $vmNames = get-content "C:\temp\Scripts\changeCPU\cpu.txt" # (Optional from TXT File)
    $Report_All_Info = "C:\temp\Scripts\changeCPU\Report_All_Info.csv"

    Get-VM -Name $vmNames -PipelineVariable vm |
    Get-NetworkAdapter -PipelineVariable nic |
    Select @{N='name';E={$vm.Name}},
    @{N = 'PowerState';E={$vm.PowerState}},
    @{N = 'Guest OS';E={$vm.Guest.OSFullName}},
    @{N = 'Folder';E={$vm.Folder.Name}},
    @{N = 'ToolsVersion';Expression={$vm.Guest.ToolsVersion}},
    @{N = 'Total_CPU';E={$vm.NumCpu}},
    @{N = 'CoresPerSocket';E={$vm.ExtensionData.Config.Hardware.NumCoresPerSocket}},
    @{N = 'CPU Per Sockets ';E={$vm.NumCpu/$vm.ExtensionData.Config.Hardware.NumCoresPerSocket}}, #( Calculator the vCPU /CoresPerSocket)
    @{N = 'NIC';E={$nic.Name}},
    @{N = 'Connected';E={$nic.ConnectionState.Connected}},
    @{N = 'IP';E={($vm.Guest.Nics | Where-Object { $_.Device.Name -eq $nic.Name }).IPAddress -join '|'}} |

    Export-Csv -Path $Report_All_Info -NoTypeInformation -UseCulture



  • 23.  RE: need export to csv all vm in txt file

    Posted Aug 08, 2022 12:57 PM

    That is not the ToolsVersionStatus but a concatenation of 3 separate properties.
    It would have helped if you said what you wanted to see.

     

     

    Get-VM -Name $vmNames -PipelineVariable vm |
    Get-NetworkAdapter -PipelineVariable nic |
    Select-Object @{N = 'VM'; E = { $vm.Name } },
    @{N = 'PowerState'; E = { $vm.PowerState } },
    @{N = 'CPU'; E = { $vm.ExtensionData.Config.Hardware.NumCpu } },
    @{N = 'CoresPerSocket'; E = { $vm.ExtensionData.Config.Hardware.NumCoresPerSocket } },
    @{N = 'NIC'; E = { $nic.Name } },
    @{N = 'Connected'; E = { $nic.ConnectionState.Connected } },
    @{N = 'IP'; E = { ($vm.Guest.Nics | Where-Object { $_.Device.Name -eq $nic.Name }).IPAddress -join '|' } },
    @{N='ToolsVersion';E={
            "{0}, version {1}, {2}" -f $vm.Guest.ExtensionData.GuestState,
            $vm.Guest.ExtensionData.ToolsVersion,
                    $vm.Guest.ExtensionData.ToolsVersionStatus }
    } |
    Export-Csv -Path $reportCPU -NoTypeInformation -UseCulture

     

     

     



  • 24.  RE: need export to csv all vm in txt file

    Posted Aug 07, 2022 08:50 PM

    No need to go into the ExtensionData, you can do

    Get-VM -Name $vmNames -PipelineVariable vm |
    Get-NetworkAdapter -PipelineVariable nic |
    Select-Object @{N = 'VM'; E = { $vm.Name } },
    @{N = 'PowerState'; E = { $vm.PowerState } },
    @{N = 'CPU'; E = { $vm.ExtensionData.Config.Hardware.NumCpu } },
    @{N = 'CoresPerSocket'; E = { $vm.ExtensionData.Config.Hardware.NumCoresPerSocket } },
    @{N = 'NIC'; E = { $nic.Name } },
    @{N = 'Connected'; E = { $nic.ConnectionState.Connected } },
    @{N = 'IP'; E = { ($vm.Guest.Nics | Where-Object { $_.Device.Name -eq $nic.Name }).IPAddress -join '|' } },
    @{N='ToolsStatus';E={$vm.Guest.State}} |
    Export-Csv -Path $reportCPU -NoTypeInformation -UseCulture