gokhuysen
gokhuysen

Reputation: 1

making a list off all my running vm's with powershell. want to change my output

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.

Upvotes: 0

Views: 566

Answers (2)

gokhuysen
gokhuysen

Reputation: 1

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

}

Upvotes: 0

ATek
ATek

Reputation: 825

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 

Upvotes: 1

Related Questions