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
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
Perhaps my function in Folder by Path might help.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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.
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
Thanks LucD. That did it!
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".
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
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.
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
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.
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.