VMware Cloud Community
carter205
Contributor
Contributor
Jump to solution

Obtain list of VM names and IP addresses from a specific folder

I'm trying to obtain a list of VM names and IP addresses from a specific folder within vCenter. The problem is there are multiple folders with the same name under different folders. I need to know how to specify the top level folder in which the folder customer10 resides. Here is an example of my current script which is pulling all VM info from all folders named customer10:

get-vm -location customer10 | select Name,@{N="IP Address";E={@($_.guest.IPAddress[0])}} |        
out-file c:\VM_IP_Addresses.csv

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The PowerSHell engine has to "know" the function before you can call it.

Store the function in a .ps1 file.

Then dot-source that .ps1 file. Make sure you are positioned at the directory where the .ps1 is stored, or use the full path

PowerCLI C:\> . ./GetFolderByPath.ps1

Note that there is a blank between the 2 dots !

Now you can call the function

PowerCLI C:\> Get-FolderByPath -Path MYDC/MyFolder | Get-VM | Select Name,....


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

View solution in original post

0 Kudos
10 Replies
LucD
Leadership
Leadership
Jump to solution

Perhaps my function in Folder by Path might help.


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

0 Kudos
carter205
Contributor
Contributor
Jump to solution

Thanks LucD for your response. When I run the command here is what I get:

The term 'Get-FolderByPath' is not recognized as the name of a cmdlet, function, script file, or operable program.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The PowerSHell engine has to "know" the function before you can call it.

Store the function in a .ps1 file.

Then dot-source that .ps1 file. Make sure you are positioned at the directory where the .ps1 is stored, or use the full path

PowerCLI C:\> . ./GetFolderByPath.ps1

Note that there is a blank between the 2 dots !

Now you can call the function

PowerCLI C:\> Get-FolderByPath -Path MYDC/MyFolder | Get-VM | Select Name,....


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

0 Kudos
carter205
Contributor
Contributor
Jump to solution

Thanks LucD. That did it!

0 Kudos
carter205
Contributor
Contributor
Jump to solution

LucD,

I'd like to schedule this script now. When I dot source and run the script it works fine. However when I schedule it as a task it fails with the "The term 'Get-FolderByPath' is not recognized as the name of a cmdlet" message.

Here is what's in the task Program path: C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe. The additional arguments are -PSConsoleFile "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" "& "C:\vmwarescripts\ipreport\ipreport.ps1".

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That's because the function is not know at the moment you call it in the scheduled task.

There are several ways to solve this, the easiest would be to just add the lines for the function at the top of your ipreport.ps1 file.

An alternative is to load the function through the profile that belongs to the user under which you execute the scheduled task.


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

0 Kudos
carter205
Contributor
Contributor
Jump to solution

Had a feeling you'd say that so I had tried adding it to the .ps1 file. Here is my script:

. ./GetFolderByPath.ps1
Connect-VIServer viservername -User corp\username -Password password
Get-FolderByPath -Path "c:\path\path" | Get-VM | select Name,@{N="IP Address";E={@($_.guest.IPAddress[0])}} | export-csv c:\vmwarescripts\ipreport\Support_IP_Addresses.csv

It says the . ./GetFolderByPath.ps1 is not recognized as the name of a cmdlet.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I meant the content of the file GetFolderByPath.ps1.

Or else try giving an absolute path, something like C:\Scripts\GetFolderByPath.ps1.


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

saitoz
Contributor
Contributor
Jump to solution

go to "command prompt" and run:

Start C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe  -command "C:\Program Files\VMware\Infrastructure\vSphere  PowerCLI\vim.psc1" -noe -c ". \"C:\Program  Files\VMware\Infrastructure\vSphere  PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1\";"C:\scripts\GetFolderByPath.ps1"

you should change the paths above according to where they are in your system.

carter205
Contributor
Contributor
Jump to solution

I finally got it working by adding . c:\vmwarescripts\GetFolderByPath.ps1 to the ipreport.ps1 file. Thanks guys for your help, I really appreciate it.

0 Kudos