VMware Cloud Community
breakstuf50
Enthusiast
Enthusiast
Jump to solution

how to import list of VM Names and ran a script against the imported list of VMs

Hi Guys,

Can someone help me and integrate an import csv script (which contains a list of VMs) and ran the below script against the imported VMs to create an output csv file? Thanks alot in advance.

Connect-VIServer xxxxxxxx

$date = Get-Date -format M-d-yyyy

. 'c:\Users\xxxx\test\test2\scripts\xxx scripts\Get-FolderPath.ps1'

$folders = Get-Folder -type vm | Get-FolderPath | Sort-Object Id

function Set-Path{

param($Object)

foreach ($folder IN $folders){

if( $folder.Id -eq $Object.Id){

$result = $folder.Path

break

}

}

$result

}

Get-VM vditest-1.xxx.internal | Select-Object -Property Name,@{N='Datacenter';E={$_|Get-Datacenter}},@{N='Cluster';E={$_.VMHost.Parent}},

NumCPU,MemoryGB,ProvisionedSpaceGB,@{N='Path';E={($_.Folder|Get-FolderPath).Path}},@{N='FolderID';E={$_.folder.Id}} | Export-Csv “C:\test\vm-xxx-$date.csv” -NoTypeInformation -UseCulture

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like below script

It assumes your CSV file looks like this

"Name"

"VM1"

"VM2"

The script

Import-Csv vm.csv -UseCulture | %{

    Get-VM -Name $_.Name |

    Select Name,

        @{N='Datacenter';E={Get-Datacenter -VM $_ | Select -ExpandProperty Name}},

        @{N='Cluster';E={Get-Cluster -VM $_ | Select -ExpandProperty Name}},

        NumCpu, MemoryGB,

        ProvisionedSpaceGB,

        @{N='Path';E={

            $current = Get-View $_.ExtensionData.Parent

            $path = $_.Name

            do {

                $parent = $current

                if($parent.Name -ne "vm"){$path =  $parent.Name + "\" + $path}

                $current = Get-View $current.Parent

            } while ($current.Parent -ne $null)

            [string]::Join('\',($path.Split('\')[0..($path.Split('\').Count-2)]))           

        }},

        FolderId

}


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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Not sure what you are trying to accomplish here.

This looks like a collection of random copy/paste from other scripts.

Perhaps you could describe your input, and what you want to see in the report ?


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

0 Kudos
breakstuf50
Enthusiast
Enthusiast
Jump to solution

Hi Luc,

I have a list of VMs from an excel/csv sheet. I'm trying to collect the some info + folderpath + folder Id's using the script i mentioned above.

The input list of VMs should come from the the excel sheet then it should collect something like this.

        

NameDatacenterClusterNumCpuMemoryGBProvisionedSpaceGBPathFolderID
vditest-1.xxx.internalUSservers2420.00145031US\serversFolder-group-v905

vditest-2.xxx.internal

vditest-3.xxx.internal

hope this helps clarify what i want to achieve.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try something like below script

It assumes your CSV file looks like this

"Name"

"VM1"

"VM2"

The script

Import-Csv vm.csv -UseCulture | %{

    Get-VM -Name $_.Name |

    Select Name,

        @{N='Datacenter';E={Get-Datacenter -VM $_ | Select -ExpandProperty Name}},

        @{N='Cluster';E={Get-Cluster -VM $_ | Select -ExpandProperty Name}},

        NumCpu, MemoryGB,

        ProvisionedSpaceGB,

        @{N='Path';E={

            $current = Get-View $_.ExtensionData.Parent

            $path = $_.Name

            do {

                $parent = $current

                if($parent.Name -ne "vm"){$path =  $parent.Name + "\" + $path}

                $current = Get-View $current.Parent

            } while ($current.Parent -ne $null)

            [string]::Join('\',($path.Split('\')[0..($path.Split('\').Count-2)]))           

        }},

        FolderId

}


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

0 Kudos
breakstuf50
Enthusiast
Enthusiast
Jump to solution

Thanks LucD works like a charm.

0 Kudos