0

For another script im making i need a list of my VM's. I use the following script for that:

Add-PSSnapin VMware.VimAutomation.Core Connect-VIServer vmXX Connect-VIServer vmXI $vms = get-vm | where { ($_.powerstate -eq "poweredon") } $rows = @() Foreach ($VM in $vms) { $View = $VM | get-view $Config = $View.config if ($Config.Template) { continue } $row = New-Object -TypeName PSObject $row | Add-Member -MemberType NoteProperty -Name $Config.Name $rows += $row } 

Now this outputs the names as VM : VMXX - Build Server I've tried -replace and -trim but i cant find a way to just get a clean list with only VM names in it and nothing else.

1
  • what are you trying to achieve? What properites do you need? to only get vm names you dont need all that script, just use Get-VM | Select -Expand Name or in your case $Rows = $vms | Select -Expand Name Commented Aug 19, 2015 at 8:45

2 Answers 2

1

I think the main thing you were missing is the -Name "" -Value "" format in your Add-Member line.

Something like this.

Add-PSSnapin VMware.VimAutomation.Core Connect-VIServer @("svc01","svc02") -WarningAction 0 -Force $vms = Get-VM | Where-Object { ($_.powerstate -eq "PoweredOn") } $rows = @() ForEach ($vm in $vms) { if ($($vm | Get-View).config.template) { continue } $row = New-Object -TypeName PSObject $row | Add-Member -MemberType NoteProperty -Name "Name" -Value $vm.Name $rows += $row } $rows 
Sign up to request clarification or add additional context in comments.

1 Comment

After some more recearch i turned my script into the following which seems to do the trick:
0

i changed my code to the following which seems to do the trick:

Add-PSSnapin VMware.VimAutomation.Core Connect-VIServer vmXX $outfile1 = "d:\beheer\scripts\XX.csv" $FileExists = Test-Path $outfile1 If ($FileExists -eq $True) { del $outfile1 } foreach ($vmachine in Get-VM | where {$_.powerstate -eq "poweredon"} |where { $_.ExtensionData.Config.ManagedBy.extensionKey -notmatch "com.vmware.vcDr" }| sort ) { $list = $vmachine.Name $VM = $list -split (" ") $vm1 = $VM[0] | Out-File $outfile1 -append } Connect-VIServer vm65 # PowerShell Checks If a File Exists $outfile1 = "d:\beheer\scripts\CommVault\VMlist.csv" foreach ($vmachine in Get-VM | where {$_.powerstate -eq "poweredon"} |where { $_.ExtensionData.Config.ManagedBy.extensionKey -notmatch "com.vmware.vcDr" } | sort ) { $list = $vmachine.Name $VM = $list -split (" ") $vm1 = $VM[0] | Out-File $outfile1 -append } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.