VMware Cloud Community
jeep2004
Enthusiast
Enthusiast

need export to csv all vm in txt file

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 

Reply
0 Kudos
23 Replies
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jeep2004
Enthusiast
Enthusiast

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 

 

 

Reply
0 Kudos
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

jeep2004
Enthusiast
Enthusiast

THX 

its work 

Reply
0 Kudos
jeep2004
Enthusiast
Enthusiast

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 ?

Reply
0 Kudos
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jeep2004
Enthusiast
Enthusiast

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
-------------------------------------

Reply
0 Kudos
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jeep2004
Enthusiast
Enthusiast

Ok 

I got it 

many thanks 

Tags (1)
Reply
0 Kudos
jeep2004
Enthusiast
Enthusiast

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

how do it  add in the same script?

 

THX 

Tags (1)
Reply
0 Kudos
LucD
Leadership
Leadership

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?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jeep2004
Enthusiast
Enthusiast

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 

 

 

Reply
0 Kudos
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jeep2004
Enthusiast
Enthusiast

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 

 

Tags (1)
Reply
0 Kudos
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

jeep2004
Enthusiast
Enthusiast

thank you 

Tags (1)
Reply
0 Kudos
jeep2004
Enthusiast
Enthusiast

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

Tags (1)
Reply
0 Kudos
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jeep2004
Enthusiast
Enthusiast

thanks you ,

but I need a ToolsVersionStatus  not ToolsVersion

Reply
0 Kudos