VMware Cloud Community
Sureshadmin
Contributor
Contributor
Jump to solution

Need a powershell script to collect esx patch info

Hi,

Iam looking for a powershell script which collects result of "esxupdate query" for all esx host version 3.5 in the Virtual center version 2.5 and exports to CSV file.

It should take input as virtual center and collect all the ESX host info, log in to esx hosts, run esxupdate query and update the csv file with results.

I got the basic code from other script. i have pasted it below. This just collects the results for esx server given and need to alter the code for requirement as given above.

-


$servers=”server0″,”server1″

$account=”root”

$password=”password”

$servers | % { $a=plink -pw $password $account@$_ “esxupdate query”

$server=$_}

-


Thanks in advance!

0 Kudos
1 Solution

Accepted Solutions
avlieshout
VMware Employee
VMware Employee
Jump to solution

Did you try running the script in powershell console ?

Might be a problem with vesi script editor.

Sent from my iPhone

On Mar 19, 2010, at 9:14 PM, Sureshadmin <communities-

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".

View solution in original post

0 Kudos
18 Replies
harkamal
Expert
Expert
Jump to solution

This is how you can query patches on esxHosts

$h = Get-VMHost <host> | Get-View
$h.ConfigManager.PatchManager
$pm = get-view $h.ConfigManager.PatchManager
$pm | gm -MemberType Method Query*

0 Kudos
Sureshadmin
Contributor
Contributor
Jump to solution

hi Harkamal,

Thanks for your reply .

Can you pls give a brief explanation for this code. Can't quite get the logic.

0 Kudos
admin
Immortal
Immortal
Jump to solution

Could you try this code out?

function Get-VMHostPatch {
	param($vmhost)

	$hView = Get-View $vmhost.id -property ConfigManager
	$pm = Get-View $hview.ConfigManager.PatchManager
	$results = $pm.QueryHostPatch($null)
	$patchData = [xml]$results.XmlResult
	foreach ($bulletin in $patchData."esxupdate-response".bulletin) {
		$obj = new-object psobject
		foreach ($prop in @("id", "summary")) {
			$obj | Add-Member -membertype NoteProperty -Name $prop -value $bulletin.$prop
		}
		$obj | Add-Member -membertype NoteProperty -Name matchesPlatform -value ($bulletin.matchesPlatform -eq "true")
		$obj | Add-Member -membertype NoteProperty -Name newerVibs -value ($bulletin.newerVibs -eq "true")
		$obj | Add-Member -membertype NoteProperty -Name releaseDate -value ([DateTime]($bulletin.releaseDate))
		$obj | Add-Member -membertype NoteProperty -Name installDate -value ([DateTime]($bulletin.installDate))

		Write-Output $obj
	}
}

# Example:
# Get-VMHostPatch -VMHost (Get-VMHost X)

=====

Carter Shanklin

Read the PowerCLI Blog
[Follow me on Twitter|http://twitter.com/cshanklin]

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Isn't the QueryHostPatch_Task method vSphere (API 4.0) only ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Sureshadmin
Contributor
Contributor
Jump to solution

Hi Shanklin,

Thanks for your reply.

Tried your query and got this error below,

Exception calling "QueryHostPatch" with "1" argument(s): "The method is not supported in the API version 2.5u2."

I hope LucD was correct. This "QueryhostPatch" is not supported.

If possible can you please give a code to generate a list of ESX hosts(3.5) in the VC(2.5), then log into each of it via plink, then execute"esxupdate query" and keep on appending it to a CSV file.

My above given code does it for given esx hosts in the script, but i need to do collect it for all esx hosts in the VC and export it to a CSV.

Thanks in advance!

0 Kudos
admin
Immortal
Immortal
Jump to solution

Ok, how about this? You have to handle the ESX connection logic yourself if using 3.5, see the bottom for an example.

function Get-VMHostPatch {
	param($vmhost, $server)

	if ($server -eq $null) {
		$server = $global:defaultVIServer
	}
	$si = Get-View -Server $server ServiceInstance

	if (($si.Capability.MultiHostSupported -eq $false) -and ($si.Content.About.Version -eq "3.5.0")) {
		$vmhost = get-vmhost

		$components = $si.RetrieveProductComponents()
		foreach ($component in $components) {
			$obj = new-object psobject
			foreach ($prop in @("Id", "Name", "Version", "Release")) {
				$obj | Add-Member -membertype NoteProperty -Name $prop -value $component.$prop
			}
			$obj | Add-Member -membertype NoteProperty -Name VMHost -value $VMHost
			Write-Output $obj
		}

		return
	}

	# Retrieve ESX 4.0 and up patches.
	if ($vmhost -eq $null) {
		$vmhost = Get-VMHost
	}
	foreach ($h in $vmhost) {
		$hView = Get-View $h.id
		if ($hView.Config.Product.Version -eq "3.5.0") {
			Write-Warning "Skipping host $h, connect directly to this host to query its patches"
			continue
		}
		$pm = Get-View $hview.ConfigManager.PatchManager
		$results = $pm.QueryHostPatch($null)
		$patchData = [xml]$results.XmlResult
		foreach ($bulletin in $patchData."esxupdate-response".bulletin) {
			$obj = new-object psobject
			foreach ($prop in @("id", "summary")) {
				$obj | Add-Member -membertype NoteProperty -Name $prop -value $bulletin.$prop
			}
			$obj | Add-Member -membertype NoteProperty -Name matchesPlatform -value ($bulletin.matchesPlatform -eq "true")
			$obj | Add-Member -membertype NoteProperty -Name newerVibs -value ($bulletin.newerVibs -eq "true")
			$obj | Add-Member -membertype NoteProperty -Name releaseDate -value ([DateTime]($bulletin.releaseDate))
			$obj | Add-Member -membertype NoteProperty -Name installDate -value ([DateTime]($bulletin.installDate))
			$obj | Add-Member -membertype NoteProperty -Name VMHost -value $h
			Write-Output $obj
		}
	}
}

# For vCenter 4 / ESX 4+ hosts just run, or if directly connected to ESX 3.5:
# Get-VMHostPatch

# To query ESX 3.5 patches from VC:
#$hosts = Get-VMHost
#foreach ($h in $hosts) {
#	$ipAddress = ($h | Get-VMHostNetwork).VMKernelGateway
#	$server = Connect-VIServer $ipAddress -Credential $esxCredential
#	Get-VMHostPatch -Server $server
#	Disconnect-VIServer $server
#}

=====

Carter Shanklin

Read the PowerCLI Blog
[Follow me on Twitter|http://twitter.com/cshanklin]

Sureshadmin
Contributor
Contributor
Jump to solution

hi Carter,

I connected directly to ESX 3.5 host and executed the script, but got the error given below , please give any idea,

Exception calling "RetrieveProductComponents" with "0" argument(s): "The operation is not supported on the object." At :line:12 char:45 + $components = $si.RetrieveProductComponents &lt;&lt;&lt;&lt; ()

Thanks!

0 Kudos
avlieshout
VMware Employee
VMware Employee
Jump to solution

If you want to use plink you could use something like this:

$vc = Connect-VIServer "vCenter server"
$account = "root"
$password = "root_password"

$PatchReport = @()

foreach ($vmhost in get-vmhost) {
	$VmHostPatch = .\PLINK.EXE -l $account -pw $password $vmhost.name "/usr/sbin/esxupdate query"
	for($i = 2; $i -lt $VmHostPatch.length-1; $i++) {
		$Patch = "" | Select VMHost, Patch, InstallDate, Summary
		$Patch.VmHost = $vmhost.name
		$Patch.Patch = $VmHostPatch[$i].substring(1,20)
		$Patch.InstallDate = $VmHostPatch[$i].substring(22,19)
		$Patch.Summary = $VmHostPatch[$i].substring(42)
		$PatchReport += $Patch
	}
}

$PatchReport | Export-Csv "VmHostPatchreport.csv" -NoTypeInformation

-


Arnim van Lieshout

Blogging: http://www.van-lieshout.com

Twitter: http://www.twitter.com/avlieshout

If you find this information useful, please award points for "correct" or "helpful".

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
0 Kudos
Sureshadmin
Contributor
Contributor
Jump to solution

Hi Zawizz,

Thanks for ur reply.

I ran this script from VESI script editor on my virtual center. But it throwed a common error for ESX hosts which is given below,

Unable to read from standard input: The handle is invalid. At :line:8 char:27 + $VmHostPatch = .\PLINK.EXE &lt;&lt;&lt;&lt; -l $account -pw $password $vmhost.name "/usr/sbin/esxupdate query"

Can you please give any idea to overcome this error?

And also i have a list of ESX 3.5 hosts which was taken from many VC. So is there a code which can take input(ESX name) from txt file and pull out the esx patch information and update patch info from all ESX hosts in the TXT file to one csv file?

Any help in this would be of great help to me!

0 Kudos
jeveenj
Enthusiast
Enthusiast
Jump to solution

Hi,

You can try this also, I modified a bit in the script provided by Arnim.

It is easy to read csv file in power shell. You can create list of hosts in the csv file as below:

Name

Here ”Name“ would be the column and below would be list the hosts being managed by the VC.

I have used list.csv, you can change it as per your file name.

$vc = Connect-VIServer <server> -User <user> -Password <password>

#enter user name and password which has shell access 
$User = "<user>"
$Pswd = "<password>"

$plink = "c:\putty\plink.exe"  
$plinkoptions = " -v -batch -pw $Pswd"
$cmd = "esxupdate query"
$PatchReport = @()

#reading from file 
Import-Csv  "c:\list.csv"|%{
$computer = $_.name
	$msg = $plink  + " " + $plinkoptions + " " + $User + "@" + $computer + " " + $cmd
	$VmHostPatch = Invoke-Expression -command $msg	
	for($i = 2; $i -lt $VmHostPatch.length-1; $i++) {
		$Patch = "" | Select VMHost, Patch, InstallDate, Summary
		$Patch.VmHost = $computer
		$Patch.Patch = $VmHostPatch[$i].substring(1,20)
		$Patch.InstallDate = $VmHostPatch[$i].substring(22,19)
		$Patch.Summary = $VmHostPatch[$i].substring(42)
		$PatchReport += $Patch
	}
}

$PatchReport | Export-Csv "C:\VmHostPatchreport.csv" -NoTypeInformation

Hope this helps.

-If you found this information useful, please consider awarding points for Correct or Helpful.
Sureshadmin
Contributor
Contributor
Jump to solution

hi Jeveenj,

i ran this code from VESI script editor and It gives the same error as given below,

Unable to read from standard input: The handle is invalid. At :line:1 char:18 + c:\putty\plink.exe &lt;&lt;&lt;&lt; -v -batch -pw XXXXXXXXX root@XXXXXXXXXXXXXX esxupdate query

Please note i have placed plink.exe in c:\putty and placed the esx host list in c:\list.csv as you have instructed.

Any help in solving this error would be of great help to me!

0 Kudos
avlieshout
VMware Employee
VMware Employee
Jump to solution

Did you try running the script in powershell console ?

Might be a problem with vesi script editor.

Sent from my iPhone

On Mar 19, 2010, at 9:14 PM, Sureshadmin <communities-

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
0 Kudos
Sureshadmin
Contributor
Contributor
Jump to solution

Yes true. Problem with VESI script editor.

The script works

perfect. Thanks Zawizz.

Zawizz ---&gt; given correct answer 10 points.

I can live with this, but VESI script editor produces eye catching reports, dont know why the script does not work with it. If anyone know pls help me.

0 Kudos
Sureshadmin
Contributor
Contributor
Jump to solution

Problem with VESI script editor.

Jeveenj ---&gt; Given helpful answer 6 points.

0 Kudos
Sureshadmin
Contributor
Contributor
Jump to solution

hi Jeevenj,

This script works perfect but when plink cannot login to a esx box due to a authentication failure, the final report does not contain a info about it.

Can you please help me to capture the failed information as well.

Would like to have some thing like given below

VMHost Patch

-


esx1 authentication failure

0 Kudos
jeveenj
Enthusiast
Enthusiast
Jump to solution

Hi,

I have modified the script. It will generate two files. One for the patch report and other for the non-authenticated Host.

#enter user name and password which has shell access 
$User = "<user>"
$Pswd = "<password>"
$plink = "c:\putty\plink.exe"  
$plinkoptions = " -v -batch -pw $Pswd"
$cmd = "esxupdate query"
$PatchReport = @()
$AuthReport = @()
#reading from file 
Import-Csv  "c:\list.csv"|%{
$computer = $_.name
	$msg = $plink  + " " + $plinkoptions + " " + $User + "@" + $computer + " " + $cmd
	$VmHostPatch = Invoke-Expression -command $msg	-ErrorAction SilentlyContinue
	for($i = 2; $i -lt $VmHostPatch.length-1; $i++) {
		$Patch = "" | Select VMHost, Patch, InstallDate, Summary
		$Patch.VmHost = $computer
		$Patch.Patch = $VmHostPatch[$i].substring(1,20)
		$Patch.InstallDate = $VmHostPatch[$i].substring(22,19)
		$Patch.Summary = $VmHostPatch[$i].substring(42)
		$PatchReport += $Patch
	} 
	if ($VmHostPatch -eq $null )
	{
		$auth = "" | Select VMHost, Summary
		$auth.VmHost = $computer
		$auth.Summary = "Unable to authenticate"
		$AuthReport += $auth
	}
}
#File of the non-authenticated Host
$AuthReport | Export-Csv "C:\VmHostAuthReport.csv" -NoTypeInformation
#File of the Patch report
$PatchReport | Export-Csv "C:\VmHostPatchreport.csv" -NoTypeInformation

-If you found this information useful, please consider awarding points for Correct or Helpful.
0 Kudos
Sureshadmin
Contributor
Contributor
Jump to solution

Thanks a lot Jeveenj, your script was very useful and saved a lot of time for me in creating patch reports.

I have posted one more powershell code requirement please check whether you have a script for this. http://communities.vmware.com/thread/261745

0 Kudos
Sureshadmin
Contributor
Contributor
Jump to solution

Just as an update to this thread this ESX patch report can now be done without plink.exe

Checkout Luc's wonderful script on his blog script 'esxupdate -query'

0 Kudos