GetVI-VM


posted by Richard Giles
10-06-2008

Downloads: 393
File size: 1.9kB
Views: 1,506

Embed
GetVI-VM
  1. ## ===================================================================== 
  2. ## Title       : GetVI-VM 
  3. ## Description : Retrieve VMware VMs 
  4. ## Author      : Idera 
  5. ## Date        : 9/11/2008 
  6. ## Input       : -VIserver : Virtual Infrastructure server 
  7. ##               -vm : VM server to get server information, blank for all 
  8. ##                -verbose -debug    
  9. ## Output      : VMs 
  10. ## Usage       : PS> . GetVI-VM myVIserver -v -d 
  11. ## Notes       :  
  12. ## Tag         : PowerShell, VMware 
  13. ## ===================================================================== 
  14.  
  15. param 
  16.    [string]$VIserver = "myVIserver"
  17.    [string]$vm
  18.    [switch]$verbose
  19.    [switch]$debug 
  20.  
  21. function main() 
  22.    if ($verbose) {$VerbosePreference = "Continue"
  23.    if ($debug) {$DebugPreference = "Continue"
  24.    GetVI-VM $VIserver $vm  
  25.  
  26. function GetVI-VM([string]$VIserver, [string]$vm
  27.  
  28.    $Error.Clear() 
  29.    Get-PSSnapin vmware* 
  30.    if($Error.Count -ne 0) 
  31.    
  32.       $Err = "ERROR" + 
  33.                "`nTo run this script, the VI Toolkity must be installed and registered with Powershell." +     
  34.                "`nIf the VI Tollkit is installed, go to the Settings menu in PowershellPlus console, " + 
  35.                "`n   click on Manage Snapins and check the VMware snapin to load it." 
  36.     
  37.       Write-Host $Err -foregroundcolor red -backgroundColor yellow 
  38.       break 
  39.    
  40.    else 
  41.    
  42.       if ($VIserver -ne $null
  43.       
  44.          $viSrv = Connect-VIServer $VIserver 
  45.          Write-Debug "You have connected to Server: $viSrv.Name with a SessionID of: $viSrv.SessionID ." 
  46.          if ($vm -eq ""
  47.          
  48.              Get-VM | Sort-Object -property Name  
  49.          
  50.          else 
  51.          
  52.              Get-VM $vm | Sort-Object -property Name  
  53.          
  54.          Disconnect-VIServer -Server $DefaultVIServer 
  55.       
  56.       else 
  57.       
  58.          Write-Host "You must supply a valid host name" 
  59.       
  60.    
  61.  
  62. main 
Filed under: , ,

Retrieve VMware VMs from a Virtual Instructure Server using VI Toolkit PowerShell snapin.

Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.