VMware Cloud Community
FrankG203
Enthusiast
Enthusiast
Jump to solution

Get-VM working anymore?

I have a few scripts that seem to just not be working anymore.  I basically just want to feed a list of VMs using get-content and I want the output to be the VM name and the Folder name. I am not great at scripting but this has been typically easy and something I have done in the past.

clear

# vCenters to connect to

    $hosts = @(

    "xxxxxxxxxxxxxx",

    "xxxxxxxxxxxxxx"

    );

# Connect to both vCenters

# Connect-VIServer -Server $hosts

$list = Get-Content -Path C:\xxxx\xxxxx.txt

Get-VM -Name $list -Server $hosts |

Select Name , Folder | sort Folder | ft -AutoSize

or

Clear

#Connect-VIServer xxxxxxxxxxxxx

$list = Get-Content -Path C:\xxxxxxx\xxxxxxx.txt

    foreach($vm in $list){

    Get-VM -Name $vm |

    Select Name,

    @{N=”Folder”;E={$_.Folder.Name}} | export-csv c:\xxxxxxx\vmname.csv

   }

If i do just get-vm it works no problem its ignoring the list, these two scripts have worked no issue for a long time now any help is appreciated.

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

What is returned when you just do Get-Content -Path C:\xxxxxxx\xxxxxxx.txt?

Any blank lines in that .txt file?


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

View solution in original post

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

What is returned when you just do Get-Content -Path C:\xxxxxxx\xxxxxxx.txt?

Any blank lines in that .txt file?


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

Reply
0 Kudos
FrankG203
Enthusiast
Enthusiast
Jump to solution

Unbelievable, I guess I had hit enter after the last VM in the list and that was causing it to not work.  Usually if there are white spaces after the VM names in the list it obviously will not work, I have never seen it not work with a enter after the last entry

But what was strange is $list was returning the full list of VMs.

Anyways sorry to bother you with something so rudimentary, I am all good now!

Its not pretty but i turned it into this.

clear

# Setup Array With Hosts

    $hosts = @(

    "xxxxxxxxxxxx.com",

    "xxxxxxxxxxxx.com"

    );

# Connect to both vCenters

# Connect-VIServer -Server $hosts

$Date = Get-Date -Format "MMddyyyyHHmm"

$list = Get-Content -Path C:\yourpath

$on = 0

$listcount = $list.count

$Results = @()

   

    foreach($vm in $list){

   

        # Adding the VM counter for the output window    

        $on += 1

        write-host "$on of $listcount" -BackgroundColor Green -ForegroundColor Black

        Write-Host "$vm"

   

        $vFolder = Get-VM  -Name $vm -Server $hosts | Select Name, Folder | Sort Folder

   

        # Creating Object and exporting to Excel

        $report = New-Object psobject

        $report | Add-Member -MemberType NoteProperty -Name FolderName -Value $vfolder.Name

        $report | Add-Member -MemberType NoteProperty -Name VMName -Value $vfolder.Folder

        $Results += $report  

        $report | Export-Csv -Path C:\yourpath\FolderList_$Date.csv -NoTypeInformation -Append

    

    }

$Results

Reply
0 Kudos