## =====================================================================
## Title       : GetVI-VM
## Description : Retrieve VMware VMs
## Author      : Idera
## Date        : 9/11/2008
## Input       : -VIserver : Virtual Infrastructure server
##               -vm : VM server to get server information, blank for all
##                -verbose -debug   
## Output      : VMs
## Usage       : PS> .\GetVI-VM -VIsevrer myVIserver -verbose -debug
## Notes       : 
## Tag         : PowerShell, VMware
## Change log  : 
##  4/1/2009 - Added input parameters, param block, main and trap
##  4/1/2009 - Added Read-Host input prompts and Write-Verbose and Write-Debug statements
## ===================================================================== 
 
param
(
   [string]$VIserver = "$(Read-Host 'VI Server' [e.g. vs01])",
   [string]$vm = "$(Read-Host 'Virtual Machine' [e.g. vm01])", 
   [switch]$verbose = $true,
   [switch]$debug = $false
)

function main()
{
   if ($verbose) {$VerbosePreference = "Continue"}
   if ($debug) {$DebugPreference = "Continue"}
   
	Write-Verbose "Retrieve VMware VMs..."
	GetVI-VM $VIserver $vm 
}

function GetVI-VM($VIserver, $vm)
{
	trap [Exception] 
	{
		write-error $("TRAPPED: " + $_.Exception.Message);
		continue;
	}

   $Error.Clear()
   Get-PSSnapin vmware*
   if($Error.Count -ne 0)
   {
      $Err = "ERROR" +
               "`nTo run this script, the VI Toolkit must be installed and registered with PowerShell." +	
               "`nIf the VI Toolkit is installed, go to the Settings menu in PowerShell Plus console, " +
               "`n   click on Manage Snapins and check the VMware snapin to load it."
   
      Write-Host $Err -foregroundcolor red -backgroundColor yellow
      break
   }
   else
   {
      if ($VIserver -ne $null)
      {
         $viSrv = Connect-VIServer $VIserver
         Write-Debug "You have connected to Server: $viSrv.Name with a SessionID of: $viSrv.SessionID ."
         if ($vm -eq "")
         {
	         Get-VM | Sort-Object -property Name 
         }
         else
         {
	         Get-VM $vm | Sort-Object -property Name 
         }
		 Disconnect-VIServer -Server $DefaultVIServer
      }
      else
      {
         Write-Host "You must supply a valid host name"
      }
   }
}

main

这个脚本是获取VMWare的虚拟机。

脚本中涉及的新Cmdlet是Sort-Object,这个是标准的PowerShell的Cmdlet。通过man查看它的标准用法:

SYNTAX
    Sort-Object [[-Property] <Object[]>] [-Stable] [-Descending] [-Unique] [-InputObject <psobject>] [-Culture <string>]
     [-CaseSensitive] [<CommonParameters>]

    Sort-Object [[-Property] <Object[]>] -Top <int> [-Descending] [-Unique] [-InputObject <psobject>] [-Culture <string>
    ] [-CaseSensitive] [<CommonParameters>]

    Sort-Object [[-Property] <Object[]>] -Bottom <int> [-Descending] [-Unique] [-InputObject <psobject>] [-Culture <stri
    ng>] [-CaseSensitive] [<CommonParameters>]


PARAMETERS
    -Bottom <int>

        Required?                    true
        Position?                    Named
        Accept pipeline input?       false
        Parameter set name           Bottom
        Aliases                      None
        Dynamic?                     false

    -CaseSensitive

        Required?                    false
        Position?                    Named
        Accept pipeline input?       false
        Parameter set name           (All)
        Aliases                      None
        Dynamic?                     false

    -Culture <string>

        Required?                    false
        Position?                    Named
        Accept pipeline input?       false
        Parameter set name           (All)
        Aliases                      None
        Dynamic?                     false

    -Descending

        Required?                    false
        Position?                    Named
        Accept pipeline input?       false
        Parameter set name           (All)
        Aliases                      None
        Dynamic?                     false

    -InputObject <psobject>

        Required?                    false
        Position?                    Named
        Accept pipeline input?       true (ByValue)
        Parameter set name           (All)
        Aliases                      None
        Dynamic?                     false

    -Property <Object[]>

        Required?                    false
        Position?                    0
        Accept pipeline input?       false
        Parameter set name           (All)
        Aliases                      None
        Dynamic?                     false

    -Stable

        Required?                    false
        Position?                    Named
        Accept pipeline input?       false
        Parameter set name           Default
        Aliases                      None
        Dynamic?                     false

    -Top <int>

        Required?                    true
        Position?                    Named
        Accept pipeline input?       false
        Parameter set name           Top
        Aliases                      None
        Dynamic?                     false

    -Unique

        Required?                    false
        Position?                    Named
        Accept pipeline input?       false
        Parameter set name           (All)
        Aliases                      None
        Dynamic?                     false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer, PipelineVariable, and OutVariable. For more information, see
        about_CommonParameters (https://go.microsoft.com/fwlink/?LinkID=113216).


INPUTS
    System.Management.Automation.PSObject


OUTPUTS
    System.Object

ALIASES
    sort

我们经常使用的是-Property参数,通过管道传递对象的某个属性值进行的排序。它有一个别名是sort,当我们在管道后使用sort其实就是在使用Sort-Object