## =====================================================================
## Title       : GetVI-VMList
## Description : Retrieve VMware VM List
## Author      : Idera
## Date        : 9/11/2008
## Input       : -VIserver : Virtual Infrastructure server
##                -verbose -debug   
## Output      : VMs
## Usage       : PS> .\GetVI-VMList -VIserver 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])",
   [switch]$verbose = $true,
   [switch]$debug = $false
)

function main()
{
   if ($verbose) {$VerbosePreference = "Continue"}
   if ($debug) {$DebugPreference = "Continue"}

	Write-Verbose "Retrieve VMware VM List..."
	CheckVIToolKit
   GetVI-VMList $VIserver 
}

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

	# Clear Screen
	Clear-Host
	
	# Warn user they must be logged on with an account that has connection rights to the VI server.
	$answer = Read-Host "`n`n`n`tNOTE: You must be logged on with an account that has connection rights to the VI server. Press <X> to exit, <C> to continue if you have a connection to a VI Server already or <Enter> to continue. "
	if($answer -eq "x")
	{
		break
	}
	else
	{
		if($VIserver -eq "")
		{
			Write-Host "`n`n`n`tERROR- A VI server must be specified!`n`n" -foregroundcolor red
			Read-Host "`n`t Press <Enter> to continue "
			break
		}
		else
		{
			if($answer -eq "C")
			{
				# do nothing as we have a connection we can use already
			}
			else
			{
				Connect-VIServer $VIserver
				Write-Host "`n`n`t You have connected to Server: $VIserver.Name with a SessionID of: $VIserver.SessionID ."
				Read-Host "`n`t Press <Enter> to continue "
			}
		}
		
		Clear-Host
		
		# List the servers
		$answer = Read-Host "`n`n`t Enter (S) to specify a VM or (L) to display a list of all VM's "
			if($answer -eq "S")
			{
				$answer = Read-Host "`n`n`t Enter the name of VM to display "
				Get-VM $answer | Format-Table -autosize
				Read-Host "`n`n`t Press <Enter> to continue "
			}
			elseif($answer -eq "L") 			
			{
				Get-VM | Format-Table | more
				Read-Host "`n`n`t Press <Enter> to continue "
			}
			else
			{
				Read-Host "`n`n`t Invalid option entered, Press <Enter> to continue "
				break
			}	
	}
} 

function CheckVIToolKit()
{
	# Before we do anything we must check to see if the user has the VI toolkit installed.
	# If user does not then we prompt the user and exit.
	$Error.Clear()
	Get-PSSnapin vmware*
	if($Error.Count -ne 0)
	{
		Clear-Host
		Write-Host "`n`n`t`t ERROR - To run this script, the VI Toolkit must be installed and registered with Powershell. If the VI Tollkit is installed," -foregroundcolor red -backgroundColor yellow
		Write-Host "`t`t go to the Settings menu in Powershell Plus and click on Manage Snapins." -foregroundcolor red -backgroundColor yellow
		Read-Host  "`n`n`t Press <Enter> to continue."
		Clear-Host
		break
	}
}

main

在此脚本中,第46行,Read-Host是新Cmdlets。它对应Write-host,Write是输出,Read就是读取,从PowerShell读取输入信息。

语法如下:

SYNTAX
    Read-Host [[-Prompt] <Object>] [-AsSecureString] [<CommonParameters>]


PARAMETERS
    -AsSecureString

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

    -Prompt <Object>

        Required?                    false
        Position?                    0
        Accept pipeline input?       true (FromRemainingArguments)
        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.Object


OUTPUTS
    System.String
    System.Security.SecureString


ALIASES
    None

第80行的Get-Vm是要安装Vmware模块后才具有的命令。