首页 > 系统相关 >通过PowerShellPlus示例脚本学习PowerShell-读取VMware主机信息

通过PowerShellPlus示例脚本学习PowerShell-读取VMware主机信息

时间:2023-11-14 10:32:06浏览次数:51  
标签:示例 ## VI Write Host continue PowerShellPlus VIserver PowerShell

## =====================================================================
## Title       : GetVI-HostList
## Description : Retrieve VMware Hosts
## Author      : Idera
## Date        : 9/11/2008
## Input       : -VIserver : Virtual Infrastructure server
##                -verbose -debug   
## Output      : VMs
## Usage       : PS> .\GetVI-HostList -VIserver myVIserver -verbose -debug
## Notes       : 
## Tag         : PowerShell, VMware
## Change log  : 
##  4/1/2009 - Added input parameters 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 Hosts..."
	CheckVIToolKit
	GetVI-HostList $VIserver 
}

function GetVI-HostList($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. `n`n`n`n`tPress <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
			
		# Display the list of Hosts found on the network.
		Get-VMHost
		Read-Host "`n`n`t Press <Enter> to continue "
	}
} # EOF: GetVI-HostList()

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
	}
}# EOF: CheckVIToolKit()

main

这个脚本检索VMWare主机,首先看到执行时会执行第81行的CheckVIToolKit,该方法是检索是否安装模拟,使用的一个新Cmdlets是第86行的Get-PSSnapin。

Get-PSSnapin cmdlet 获取 Windows PowerShell 管理单元,这些单元已添加到当前会话中或已在系统上注册它们。 按照检测到的顺序列出这些管理单元。

Get-PSSnapin仅获取已注册的管理单元。

这里主要检测是否安装Vmware模块。(如果没安装可以使用命令

Install-Module VMWare.PowerCLI

来安装)。

第67和76行的Cmdlet都是需要安装VMware的PowerShell模块后才会有的命令。


标签:示例,##,VI,Write,Host,continue,PowerShellPlus,VIserver,PowerShell
From: https://blog.51cto.com/u_3353175/8362976

相关文章

  • 微服务系列-Spring Boot使用Open Feign 微服务通信示例
    公众号「架构成长指南」,专注于生产实践、云原生、分布式系统、大数据技术分享。前言在前几个教程中我们已经看到:使用RestTemplate的SpringBoot微服务通信示例使用WebClient的SpringBoot微服务通信示例在本教程中,我们将学习如何使用SpringCloudOpenFeign库在......
  • 在`tomlkit`库中,`parse`函数用于将TOML格式的字符串解析为一个`Document`对象¹。以下
    #parse与节点(.)importtomlkit#TOML格式的字符串toml_str="""[tool.poetry]name="my-package"version="0.1.0"description="Mypackagedescription"authors=["Me<[email protected]>"]"&qu......
  • VCR库代码示例
    首先,我们需要在代码中添加对VCR库的引用:require'vcr'require'rest-client'然后,我们需要创建一个VCR录制器,以便我们可以记录实际的HTTP请求和响应:VCR.use_cassette('download_video',record::new_episodes)do#下载代码将在这里end接下来,我们需要设置:proxy={然后,我们可......
  • TypeScript 和 needle 库代码示例
    TypeScript和needle库编写的爬虫程序。import*asneedlefrom'needle';import{Request}from'http';constproxyHost='www.duoip.cn';constproxyPort=8000;//创建一个HTTP请求对象constreq=newRequest(',{headers:{&......
  • httrOAuth库代码示例
    #导入httr和httrOAuth库library(httr)library(httrOAuth)#设置服务器,主机为proxy_host<-proxy_port<-proxy_url<-"#设置httrOAuth的客户端ID和客户端密钥consumer_key<-"your_consumer_key"consumer_secret<-"your_consumer_secret"......
  • isomorphic-fetch库代码示例
    isomorphic-fetch库的爬虫程序。//引入isomorphic-fetch库importfetchfrom'isomorphic-fetch';//设置constproxy=;//定义视频URLconsturl=';//使用fetch获取视频数据fetch(url,{method:'GET',headers:{'Accept':'appl......
  • Fetch库代码示例
    importFetch._importFetch.{FetchException,FetchProxyException,FetchTimeoutException}//创建对象valproxy=newFetchProxy)//创建Fetch对象,并传入对象valfetch=newFetch(proxy)//创建请求对象,并传入视频的URLvalrequest=newFetchRequest("")//进......
  • FTDI的MPSSE使用示例(SPI协议背景知识简介及其FT4232H配成USB to SPI使用实例)
    MPSSEApplicationExample:http://ftdichip.cn/Support/SoftwareExamples/MPSSE.htmMPSSE:AN_129FTDIUSBToJTAGTAPExampleMPSS:AN_114FTDIUSBtoSPIExampleMPSSE:AN_113FTDIUSBtoI2CExampleMPSS:AN_114FTDIUSBtoSPIExampleFTDIMPSSE(Multi-Pr......
  • Rust语言做数据抓取代码示例
    这个任务需要使用到Rust语言和网络爬虫相关的库,以下是一个简单的示例代码。请注意,由于涉及到的具体问题和数据的复杂性,这个示例可能并不能直接满足你的需求,需要根据你的具体情况进行修改和扩展。usereqwest;useserde::{Deserialize,Serialize};useserde_json::{json,Value};......
  • bat批处理常用系统信息、命令、语法、示例
    Windows下使用批处理设置东西,是很方便的事。一、获取系统信息1.获取CPU信息echo%PROCESSOR_ARCHITECTURE:~%系统CPU是AMD牌子,64位的1.1)获取CPU品牌echo%PROCESSOR_ARCHITECTURE:~0,3%1.2)获取CPU位数echo%PROCESSOR_ARCHITECTURE:~3%或echo%PROCESSOR_ARCHITEC......