Automation

 View Only
  • 1.  Obtain list of VM names and IP addresses from a specific folder

    Posted Apr 09, 2013 05:19 PM

    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



  • 2.  RE: Obtain list of VM names and IP addresses from a specific folder

    Posted Apr 09, 2013 05:26 PM

    Perhaps my function in Folder by Path might help.



  • 3.  RE: Obtain list of VM names and IP addresses from a specific folder

    Posted Apr 09, 2013 05:39 PM

    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.



  • 4.  RE: Obtain list of VM names and IP addresses from a specific folder
    Best Answer

    Posted Apr 09, 2013 06:01 PM

    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,....


  • 5.  RE: Obtain list of VM names and IP addresses from a specific folder

    Posted Apr 10, 2013 08:15 PM

    Thanks LucD. That did it!



  • 6.  RE: Obtain list of VM names and IP addresses from a specific folder

    Posted Apr 15, 2013 06:44 PM

    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".



  • 7.  RE: Obtain list of VM names and IP addresses from a specific folder

    Posted Apr 15, 2013 06:48 PM

    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.



  • 8.  RE: Obtain list of VM names and IP addresses from a specific folder

    Posted Apr 15, 2013 07:20 PM

    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.



  • 9.  RE: Obtain list of VM names and IP addresses from a specific folder

    Posted Apr 15, 2013 07:35 PM

    I meant the content of the file GetFolderByPath.ps1.

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



  • 10.  RE: Obtain list of VM names and IP addresses from a specific folder

    Posted Apr 15, 2013 08:16 PM

    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.



  • 11.  RE: Obtain list of VM names and IP addresses from a specific folder

    Posted Apr 15, 2013 07:37 PM

    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.