Wednesday, November 30, 2016

How to run Powershell script as a scheduled task

Normally, we can execute a PowerShell script by typing ./ followed by the name of the script and if want to run a script on a scheduled time then we can schedule it with Windows Task Scheduler.

Lately, I had issues while scheduling an script as scheduled task so thought of making this note for future reference.

As we all would be aware, before calling any PowerCLI cmdlet we should load PoweCLI Snapin to powershell first without which PowerShell will not recognize any of the PowerCLI cmdlets. There are two ways to do this,

#1. Add the following line to the top of each PowerCLI script you will be running as a scheduled task,

add-pssnapin VMware.VimAutomation.Core

Now while creating the scheduled task, under Action option, pass the script execution command as follows,

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe "& 'C:\Script.ps1'"

you can also use this,  C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -file "C:\Script.ps1"

#2. This method runs the PowerCLI Console file before it runs your script. This will enable the PowerCLI snapin for you and there is nothing to add to the scripts.

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1  "& 'C:\Script.ps1'"

Before using any of this method first we need to ensure it will work by running them from a cmd prompt. During testing we may aslo use the –noexit switch to see any errors arising from the operation.

For detailed info about runing Powershell script as a scheduled task, you can checkout Dmitry's related blog post, HERE, Alan Renouf's post, HERE and this blog post.

That's it... :)

Monday, November 28, 2016

Finding the host where your VM resides

It seems funny but possible, where using virtual vCenter and didn't created any VM-host affinity rule to limit its movement on hosts and in case if vCenter goes down then this script could be useful in locating it....
#Script to find a VM from a pool of individual hosts having differ passwords

Add-PSSnapin VMware.VimAutomation.Core

$connectvmhost = "Connect-VIServer host-A -user root -password xyz"
$connectvmhost += "Connect-VIServer Host-B -user root -password 123xyz"


Get-VM vm-name -server Host-A, Host-B | select Name, VMhost

Disconnect-VIServer -Confirm:$false

If all hosts are having the same password, then,
$usr = 'user_name'
$pwd = 'password'

$connectvmhost = "Connect-VIServer host-A -user $usr -password $pwd"
$connectvmhost += "Connect-VIServer Host-B -user $usr -password $pwd"


Get-VM vm-name -server Host-A, Host-B | select Name, VMhost

Disconnect-VIServer -Confirm:$false

I believe, there would be a better way to do this but this also serves the purpose.

That's it... :)

Saturday, November 19, 2016

PowerCLI 6.5 R1 is available now

VMware today announced the general availability of VMware PowerCLI 6.5 Release 1.

Here I'm just copying the details from original VMware blog post,

Cmdlet Updates

The Core vSphere module has received a number of updates as well as stability and performance improvements. The most notable updates include cmdlets based around VM creation and management. The ‘Move-VM’ cmdlet now supports the ability to vMotion VMs between vCenters which is known as Cross vCenter vMotion. Performing a Cross vCenter vMotion with PowerCLI also allows the VM to traverse between separate SSO domains too! This is something not available with the GUI. The ‘New-VM cmdlet now supports configuring a VM to have a specific number of CPU cores. Then, the ‘Open-VMConsoleWindow’ has been updated to provide access to the latest and greatest version of the VMware Remote Console (VMRC). Lastly, various cmdlets have received updated views to allow access to the new vSphere 6.5 APIs!

Storage Module Updates

The PowerCLI Storage module has been a big focus on this release. A lot of functionality has been added around vSAN, VVOLs, and the handling of virtual disks. The vSAN cmdlets have been bolstered to more than a dozen cmdlets which are focused on the entire lifecycle of a vSAN cluster. The entire vSAN cluster creation process can be automated with PowerCLI as well as running tests, updating the HCL database, and much more!
  • Get-VsanClusterConfiguration
  • Get-VsanDisk
  • Get-VsanDiskGroup
  • Get-VsanFaultDomain
  • Get-VsanResyncingComponent
  • Get-VsanSpaceUsage
  • New-VsanDisk
  • New-VsanDiskGroup
  • New-VsanFaultDomain
  • Remove-VsanDisk
  • Remove-VsanDiskGroup
  • Remove-VsanFaultDomain
  • Set-VsanClusterConfiguration
  • Set-VsanFaultDomain
  • Test-VsanClusterHealth
  • Test-VsanNetworkPerformance
  • Test-VsanStoragePerformance
  • Test-VsanVMCreation
  • Update-VsanHclDatabase
vSphere 6.5 introduces a new way to handle the management of virtual disks. Instead of managing a VM’s hard disks through the VM, they can now be managed independently with new PowerCLI cmdlets. This allows the handling of a virtual disk’s lifecycle to be decoupled from the lifecycle of a VM. This adds a ton of flexibility!
  • Copy-VDisk
  • Get-VDisk
  • Move-VDisk
  • New-VDisk
  • Remove-VDisk
  • Set-VDisk
PowerCLI has been updated to include new cmdlets that allow for the automated management of the VVOL replication features which are new to vSphere 6.5. Some of the new features include finding fault domains and replication groups, syncing replication groups, as well as preparing for and starting the failover process to the target site.
  • Get-SpbmFaultDomain
  • Get-SpbmReplicationGroup
  • Get-SpbmReplicationPair
  • Start-SpbmReplicationFailover
  • Start-SpbmReplicationPrepareFailover
  • Sync-SpbmReplicationGroup
Horizon Module

A brand-new module has been included to work with VMware Horizon! This has been a long time coming and has finally been released. The module can be installed anywhere and then remotely connect to the Horizon Connection server, a huge improvement over the last offering. Speaking of improvements, the module provides access to 100% of the public API through the Connect-HVServer and Disconnect-HVServer cmdlets. Keep an eye on the PowerCLI-Examples Github repo for functions allowing you to easily work with this module and provide sample code!

As with all versions of PowerCLI, it’s also backwards compatible going back to vSphere 5.5!
PowerCLI Core
As if this release wasn’t enough, PowerCLI Core also has support for the new 6.5 vSphere APIs! This ensures those core vSphere cmdlets and Views are available to other operating systems PowerCLI Core operates on as well!

The PowerCLI 6.5 R1 can be downloaded from HERE.

That's it... :)

Tuesday, November 8, 2016

Some useful PowerCLI one liner scripts

In this post I would list some of the useful PowerCLI one liner scripts that I use time to time... some scripts are taken from other blogs so the credit of those scripts goes to their respective authors...

Open the PowerCLI and connect to vCenter server using Connect-VIServer cmdlet, then

Get VMs with connected VLAN and other details,

Get-Cluster "clustername" | get-vmhost "hostname" | get-vm | Get-NetworkAdapter | Format-Table parent, networkname, type, MACaddress

To create table with renamed property name, use @{Name="New_Name";Expression={$_.Old_Name}}
Like in above script,

Get-Cluster "clustername" | get-vmhost "hostname" | get-vm | Get-NetworkAdapter | Format-Table @{Name="VirtualMachine";Expression={$_.parent}}, @{Name="VLAN";Expression={$_.networkname}}

To list all harddisks of a VM with size,

Get-VM VM01 | Get-HardDisk | ft Parent, Name, Filename, CapacityKB –AutoSize

And if you want to export this to a CSV file then,

Get-VM VM01 | Get-HardDisk | Select Parent, Name, Filename, CapacityKB | Export-csv C:\file.csv

We can't export output of Format-Table cmdlet to CSV file so we need to use Select-Object instead.

Add PortGroup to all hosts in cluster with PowerCLI

Get-Cluster “clustername” | Get-VMHost | Get-VirtualSwitch -Name “vSwitch_name″ | New-VirtualPortGroup -Name “VLAN-xx” -VLanId xx

Check Multi-path policy/configuration of connected storage,

Get-VMHost | Get-ScsiLun | Select VMHost, ConsoleDeviceName, Vendor, MultipathPolicy, LunType

To get list of VMs with snapshot,

Get-VM | Get-Snapshot | Select VM,Name,Created,sizemb

List of VMs having snapshot older than 3 days,

Get-VM | Get-Snapshot | Select VM,Name,Created,sizemb | Where {$_.Created -lt ((Get-Date).AddDays(-2))}

To remove snapshot older than 3 days,

Get-Snapshot | Select VM,Name,Created,sizemb | Where {$_.Created -lt ((Get-Date).AddDays(-2))} | Remove-Snapshot

To list time on all VMhosts,

Get-VMHost | sort Name | select Name,@{Name="Current VMHost Time";Expression={(Get-View 

To find a list of VMs having sync time with host option selected,

Get-View -viewtype virtualmachine -Filter @{'Config.Tools.SyncTimeWithHost'='True'} | select name 

That's it for now... Over time I would add more scripts here... :)