Get-VMHostPnicCDP for check CDP/LLDP information for ESXi hostsystem physical nic.

Today i had to check some data in CDP, hence i wrote this script. I think it’s well documented, so it does not need description from my side. One thing to note, is that i have not checked if it prints out properly lldp information if you are using -lldp switch, i just assumed it’s going to be ok, looking at the vsphere documentation for this lldpinfo object.

<#
	.SYNOPSIS
		Gets CDP/LLDP information for ESXi physical nic.

	.DESCRIPTION
		Gets CDP/LLDP information for ESXi physical nic.

	.PARAMETER  VMHost
		VMHost Can be piped to this function, it can VMHost object from get-vmhost or it get be HostSystme object from Get-View.

	.PARAMETER  pnic
		This is the physical nic adapter name, for example: vmnic0 , it supports array as well: vmnic0,vmnic2,vmnicN.

	.PARAMETER  lldp
		If paramenter -lldp was give, it will output content of lldpinfo from QueryNetworkHint method.

	.EXAMPLE
		PS C:\> Get-VMHostPnicCDP   -pnic vmnic0 -VMHost 'esxi1.local.lan'
		Without passing through pipeline it acceppts 1 vmhost only.

	.EXAMPLE
		PS C:\> Get-VMHostPnicCDP  -VMHost 'esxi1.local.lan'
		When -pnic parameter is skipped, CDP will be retrieved for all pnics in ESXi.

	.EXAMPLE
		PS C:\> 'esxi1.local.lan','esxi2' | Get-VMHostPnicCDP   -pnic vmnic0
		You can pass multiple VMHost names through pipeline

	.EXAMPLE
		PS C:\> get-vmhost -name 'esxi1.local.lan' | Get-VMHostPnicCDP -pnic vmnic1
		You can pass object directly from get-vmhost

	.EXAMPLE
		PS C:\>  get-view -viewtype hostsystem -filter @{'name'='esxi1.local.lan'} | Get-VMHostPnicCDP -pnic vmnic1
		You can pass object directly from get-view

	.NOTES
		NAME: Get-VMHostPnicCDP
		AUTHOR: Grzegorz Kulikowski
		LASTEDIT : 09/07/2015

	.LINK
		https://psvmware.wordpress.com

#>
function Get-VMHostPnicCDP {
	[CmdletBinding()]
	param(
		[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
		[ValidateNotNullOrEmpty()]
		[System.Object]
		$VMHost,
		[Parameter(Position=1)]
		[System.String[]]
		$pnic,
		[switch]$lldp
	)
	begin {
		try
		{
			if (!$global:defaultVIserver) { 'Not connected to VirtualCenter';break }
		}
		catch
		{
		}
	}
	process {
		try {
			switch (($VMHost.GetType()).FullName)
			{
				'VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl'
				{
					$NetworkSystemConfigManager = get-view -id $vmhost.ExtensionData.ConfigManager.NetworkSystem
				}
				'VMware.Vim.HostSystem'
				{
					$NetworkSystemConfigManager = Get-View -id $vmhost.ConfigManager.NetworkSystem
				}
				'System.String'
				{
					$VMHost = Get-View -viewtype HostSystem -Property Name, ConfigManager.NetworkSystem -Filter @{ 'name' = $VMHost }
					$NetworkSystemConfigManager = Get-View -id $vmhost.ConfigManager.NetworkSystem
				}
			}
			if (!$pnic) { [string[]]$pnic = $NetworkSystemConfigManager.NetworkConfig.Pnic | %{ $_.device } }
			foreach ($EsxiPnic in $pnic)
			{
				if ($lldp) { $NetworkSystemConfigManager.QueryNetworkHint($esxipnic) | %{ $_.lldpinfo } | Select-Object *, @{ n = 'Esxi'; e = { $VMHost.name } }, @{ n = 'ESXiPnic'; e = { $EsxiPnic } } }
				else
				{
					$NetworkSystemConfigManager.QueryNetworkHint($esxipnic) | %{ $_.ConnectedSwitchPort } | Select-Object *, @{ n = 'Esxi'; e = { $VMHost.name } }, @{ n = 'ESXiPnic'; e = { $EsxiPnic } }
				}
			}
		}
		catch [VMware.Vim.VimException]
		{
			$_.Exception
			'Maybe the pnic you are reffering to, does not exist ?'
		}
	}
	end {
		try {
		}
		catch {
		}
	}
}

Copy n paste, and enjoy 😉

cdp

vLanIdentificator – for checking subnets for Virtual Machines and their vlan info.

I have created this tiny program that will help me sometimes look for other vms that are supposed to sit inside some particular vlan/subnet. The idea is really simple. You are given an ip and somebody is asking you which vlan is that , for example. If you don’t have a hobby of memorizing vlans in your networks might try to use this program. If given a particular IP, and netmask, it will scan all ips within it and check if you have Virtual Machines in Virtual Center that are utilizing ips from that range. In addition it will tell you where the VM is being hosted and which portgroup on esxi hostsystem it is utilizing and of course its vlan id. I hope some of you may find it useful. Executable is prepared for powershell version 3. In general i guess it’s compatible only with >PowerShell 3 , since how i wrote it. I have not tested it yet with Distributed Switches. I have not tested it on other version than vsphere 5.0.If you test it on different version , let me know in the comment. I will make sure it runs ok with vDS as well soon. VMware tools need to be installed in VM in order to report the IP back. This tiny program basically just runs a loop for ips utilizing SearchIndex and its FindAllByIp function. There isn’t much magic happening here 😉 In order to write it i used Get-IPrange by Barry Chum – https://goo.gl/TBYujC IP regex – http://goo.gl/Ow7PFf On the computer on which you want to run it , you will need to have PowerCLI installed. When you are asked for credentials, read-only permission will be enough to retrieve information. Screenshot of how it looks is below. vlanidentificator Executable can be found HERE PSF (Powershell Studio) source code can be found HERE No this is not finished 😉 I am still learning windows forms, so that’s why it looks like it looks. Most important for now , is that it does its job. –Update 21/07/2015 Added support for VMware Distributed Switches Fixed minor bugs

Change vnic type from / to e1000, Flexible, Vmxnet, EnhancedVmxnet, and Vmxnet3, and Unknown with Set-NetworkAdapter. UPDATED!

What if you noticed that your vms are running on wrong network card type ? Let’s say you want e1000 instead of vmxnet3 for some reason.There is a great cmdlet set-networkadapter. This will help us out in that kind of situation(Thx Rafael!).
Right, so what set-networkadapter can do for us ? (by this time you should read the get-help set-networkadapter -full). So now, like you already know 😉 it can do a lot for us! But what i want from it right now, is to help me with changing the adapter type. I want to change it from vmxnet 3 to e1000, would be cool if i will not need to shutdown vm too 😉 Unfortunately it will not be so cool at all !!! VM has to be powered off to make this change properly. Please do not do this while vm is up and running.
Continue reading

Searching for cdrom devices with Emulate Ide or Passthrough IDE mode in powercli

Right,
so we want to find all vms that have their cdrom device set to Emulate Ide mode.

$vms=get-view -viewtype virtualmachine
foreach($vm in $vms){
 if ($vm.Config.Hardware.Device |? {$_.backing -is [vmware.vim.VirtualCdromRemoteAtapiBackingInfo]}) {$vm.name}
}

If we want to look for vms with cdrom devices mode set to Passthrough:

$vms=get-view -viewtype virtualmachine
foreach($vm in $vms){
 if ($vm.Config.Hardware.Device |? {$_.backing -is [vmware.vim.VirtualCdromRemotePassthroughBackingInfo]}) {$vm.name}
}

No fancy output 😉 Just sending to screen affected vm names.

New-Folder in datacenter of vm type

I was trying to create a vm folder in Datacenter. This can not be achieved using new-folder directly without specifying proper location(as per MicaH comment) , because as per get-help from new-folder, this will result in creating a “yellow” host and cluster folder. In order to create a vm folder i had to use method CreateFolder(string)
Lets say i have a datacenter with name: “myDC” in which i want to create a folder “myTopLevelFolder”

(get-view (get-view -ViewType datacenter -Filter @{"name"="myDC"}).VmFolder).CreateFolder("myTopLevelFolder")

get-view -ViewType datacenter -Filter @{“name”=”myDC”}
We want to select only datacenter with name ‘myDC’
Then, we take the vmfolder from this datacenter
(get-view -ViewType datacenter -Filter @{“name”=”myDC”}).VmFolder
Then we create view from this:
get-view (get-view -ViewType datacenter -Filter @{“name”=”myDC”}).VmFolder
After that we can invoke method from this object using CreateFolder

It’s done 😉