Thursday, July 14, 2016

Reboot a Host with Get-View

You can reboot a host by using its corresponding view object.
Verify that you are connected to a vCenter Server system.
1
Use the Get-VMHost cmdlet to get a host by its name, and pass the result to the Get-View cmdlet to get the corresponding view object.
$vmhostView = Get-VMHost -Name Host | Get-View 
2
Call the reboot method of the host view object to reboot the host.
$vmhostView.RebootHost()

Filter vSphere Objects with Get-View

You can use the Get-View cmdlet to filter vSphere objects before performing various actions on them.
The filter parameter is a HashTable object containing one or more pairs of filter criteria. Each of the criteria consists of a property path and a value that represents a regular expression pattern used to match the property.
Verify that you are connected to a vCenter Server system.
1
Create a filter by the power state and the guest operating system name of the virtual machines.
$filter = @{"Runtime.PowerState" ="poweredOn"; "Config.GuestFullName" = "Windows XP"}
2
Get a list of the virtual machines by using the created filter and call the ShutdownGuest method for each virtual machine in the list.
Get-View -ViewType "VirtualMachine" -Filter $filter | foreach{$_.ShutdownGuest()}
The filter gets a list of the powered-on virtual machines whose guest OS names contain the string Windows XP. The Get-View cmdlet then initiates shutdown for each guest operating system in the list.

PowerCLI Use Esxtop to Get Information on the Virtual CPUs of a Virtual Machine

ou can use the Get-EsxTop cmdlet to retrieve real-time data for troubleshooting performance problems.
Verify that you are connected to a server that runs ESX 4.0, vCenter Server 5.0 or later.
1
Get the group to which the virtual machine belongs and save it as a variable.
$group = Get-EsxTop -CounterName SchedGroup | where {$_.VMName -eq $vm.Name}
2
Get the IDs of all virtual CPUs of the virtual machine and store them in an array.
$gr = Get-EsxTop -TopologyInfo -Topology SchedGroup | %{$_.Entries} | where {$_.GroupId -eq $group.GroupID}
$cpuIds = @()
$gr.CpuClient | %{$cpuIds += $_.CPUClientID}
3
Get the CPU statistics for the virtual machine.
$cpuStats = Get-EsxTop -CounterName VCPU | where {$cpuIds -contains $_.VCPUID}
4
Calculate the used and ready for use percentage by using the UsedTimeInUsec and ReadyTimeInUsec stats.
$result = @()
$cpuStats | %{ `
$row = "" | select VCPUID, Used, Ready; `
$row.VCPUID = $_.VCPUID; `
$row.Used = [math]::Round(([double]$_.UsedTimeInUsec/[double]$_.UpTimeInUsec)*100, 2); `
$row.Ready = [math]::Round(([double]$_.ReadyTimeInUsec/[double]$_.UpTimeInUsec)*100, 2);` 
$result += $row
}
5
View the used and ready for use percentage for each virtual CPU of the virtual machine.
$result | Format-TableAutoSize

Apply a Host Profile to a Host on vCenter Server

To simplify operational management of large-scale environments, you can apply standard configurations called host profiles to hosts on vCenter Server. If you want to set up a host to use the same host profile as a reference host, you can attach the host to a profile.
Verify that you are connected to a host that runs vCenter Server 4.0 or later.
1
Get the Host2 host.
$vmhost2 = Get-VMHost Host2
2
Attach the Host2 host to the HostProfile1 host profile.
Set-VMHost -VMHost $vmhost2 -Profile HostProfile1
3
Verify that the Host2 host is compliant with the HostProfile1 profile.
Test-VMHostProfileCompliance -VMHost $vmhost2
The output of this command contains the incompliant settings of the host, if any.
4
Apply the profile to the Host2 host.
$neededVariables = Apply-VMHostProfile -Entity $vmhost2 -Profile $hp1 -Confirm:$false
The $neededVariables variable contains the names of all required variables and their default or current values, as returned by the server. Otherwise, the $neededVariables variable contains the name of the host on which the profile has been applied.

Create vSphere Inventory Objects

By using vSphere PowerCLI cmdlets, you can automate creating different inventory objects on vSphere.
Verify that you are connected to a vCenter Server system.
1
Get the inventory root folder and create a new folder named Folder in it.
$folder = Get-Folder -NoRecursion | New-Folder -Name Folder
2
Create a new datacenter named DC in the Folder folder.
New-Datacenter -Location $folder -Name DC
3
Create a folder named Folder1 under DC.
Get-Datacenter DC | New-Folder -Name Folder1
$folder1 = Get-Folder -Name Folder1
4
Create a new cluster Cluster1 in the Folder1 folder.
New-Cluster -Location $folder1 -Name Cluster1 -DrsEnabled -DrsAutomationLevel FullyAutomated
Distributed Resource Scheduler (DRS) is a feature that provides automatic allocation of cluster resources.
5
Add a host in the cluster by using the Add-VMHost command, and provide credentials when prompted.
$vmhost1 = Add-VMHost -Name 10.23.112.345 -Location (Get-Cluster Cluster1)
6
Create a resource pool in the root resource pool of the cluster.
$myClusterRootRP = Get-Cluster Cluster1 | Get-ResourcePool -Name Resources
New-ResourcePool -Location $myClusterRootRP -Name MyRP1 -CpuExpandableReservation $true -CpuReservationMhz 500 -CpuSharesLevel high -MemExpandableReservation $true -MemReservationGB 1 -MemSharesLevel high
7
Create a virtual machine asynchronously.
$vmCreationTask = New-VM -Name VM2 -VMHost $vmhost1 -ResourcePool MyRP01 -DiskGB 100 -MemoryGB 2 -RunAsync
The RunAsync parameter indicates that the command runs asynchronously. This means that in contrast to a synchronous operation, you do not have to wait for the process to complete before supplying the next command at the command line.

Activate Maintenance Mode for a Host on vCenter Server

To complete some specific administration tasks, you might need to activate maintenance mode for a host. On vCenter Server, you can activate maintenance mode by using the Set-VMHost cmdlet.
Verify that you are connected to a vCenter Server system.
1
Save the Host host object as a variable.
$vmhost = Get-VMHost -Name Host
2
Get the cluster to which Host belongs and save the cluster object as a variable.
$vmhostCluster = Get-Cluster -VMHost $vmhost
3
Start a task that activates maintenance mode for the Host host and save the task object as a variable.
$updateHostTask = Set-VMHost -VMHost $vmhost -State "Maintenance" -RunAsync
Note
If the host is not automated or is partially automated and has powered-on virtual machines running on it, you must use the RunAsync parameter and wait until all powered-on virtual machines are relocated or powered off before applying DRS recommendations.
4
Get and apply the recommendations generated by DRS.
Get-DrsRecommendation -Cluster $vmhostCluster | where {$_.Reason -eq "Host is entering maintenance mode"} | Apply-DrsRecommendation
5
Get the task output object and save it as a variable.
$myUpdatedHost = Wait-Task $updateHostTask

Add a Esxi Host to a vCenter Server System

1
View all hosts on the vCenter Server system that you have established a connection with.
Get-VMHost
2
Add the Host standalone host.
Add-VMHost -Name Host -Location (Get-Datacenter DC) -User root -Password pass

Manage Virtual Machines on vSphere

With vSphere PowerCLI, you can automate various administration tasks on virtual machines, for example retrieving information, shutting down and powering off virtual machines.
1
View all virtual machines on the target system.
Get-VM
2
Save the name and the power state properties of the virtual machines in the ResourcePool resource pool into a file named myVMProperties.txt.
$respool = Get-ResourcePool ResourcePool
Get-VM -Location $respool | Select-Object Name, PowerState > myVMProperties.txt
3
Start the VM virtual machine.
Get-VM VM | Start-VM
4
Get information of the guest OS of the VM virtual machine.
Get-VMGuest VM | fc
5
Shut down the OS of the VM virtual machine.
Shutdown-VMGuest VM
6
Power off the VM virtual machine.
Stop-VM VM
7
Move the virtual machine VM from the Host01 host to the Host02 host.
Get-VM -Name VM -Location Host01 | Move-VMDestination Host02

Content of this blog has been moved to GITHUB

Looking at current trends and to make my content more reachable to people, I am moving all the content of my blog https://tech-jockey.blogsp...