Create Scheduled Tasks with PowerCLI to Call PowerShell Scripts.

Create Scheduled Tasks with PowerCLI to Call PowerShell Scripts.

Create Scheduled Tasks with PowerCLI to Call PowerShell Scripts.

Recently i was involved in a very tricky situation with one of the customer, There was a set of performance metrics which had to be extracted from about 200 RDSH VM’s which are part of a large Horizon View Farm.

The problem was that a lot of manual effort was involved as the admin guys in support team had to log into each and every VM and create scheduled tasks manually to run for 30 days .

The scheduled task was basically used to invoke a PowerShell Script and run in it in “non-interactive” mode every 5 minutes for 9 hours on these set of server’s every day at 9AM and dump the data generated in a csv file on an external file share.

Well if this was the case then why dont we automate it!! and then came PowerCLI to the rescue 🙂 .

Also note each of these 100 VM’s was distributed across two vcenters!!, hence i had to give a script to the helpdesk guys so that in event of a scary “RECOMPOSE” was executed on these set of servers which would make the VM’s vanilla again and delete any changes made, the help desk guys could launch a powershell console and run the script again and make sure all’s back on track.

Ok so let me go step by step on how i made it to work.

Create Scheduled Tasks with PowerCLI to Call PowerShell Scripts.

Create the Credential object for GuestOS and vCenter Server, as this would be a script just for temporary purpose and to make it easy for help desk guys!! i hard-coded the credentials 🙂 .

# Create a credential object for the guest os.

$GuestUserName = "administrator"
$GuestPassword = "VMwar3!!"

$secpasswd = ConvertTo-SecureString $GuestPassword -AsPlainText -Force
$guestcredential = New-Object System.Management.Automation.PSCredential ($GuestUserName, $secpasswd)

# Credentials remain the same for both the vcenter's

$VCUserName = "administrator@euc.prim"
$VCPassword = "IBS_bsc@2016"

$secpasswd = ConvertTo-SecureString $VCPassword -AsPlainText -Force
$vccredential = New-Object System.Management.Automation.PSCredential ($VCUserName, $secpasswd)

Next we would connect to the vcenter one by one, get a list of VM’s which match the VM prefix, then iterate across them one by one and copy the required powershell scripts to the VM’s using Copy-VMGuestfile which internally uses VMware Tools

# Connect to the PoD2 vCenter, copy the scheduled task and use a Invoke-VMSCript to call the script which would register the scheduled tasks

Connect-VIServer iprvcsa12.corp.ad.ibs -credential $vccredential

$names = get-VM IBSC1P2* | select name

$names | % { 

Write-Host "Copying to VM Name - $_.name"

Copy-VMGuestfile -Source "C:\Users\bscadmin\Desktop\psscripts\RegisterScheduledtask.ps1" -destination "c:\test" -vm $_.name -LocalToGuest -GuestCredential $guestcredential -force -ErrorAction SilentlyContinue

Copy-VMGuestfile -Source "C:\Users\bscadmin\Desktop\psscripts\procmon.ps1" -destination "c:\test" -vm $_.name -LocalToGuest -GuestCredential $guestcredential -force -ErrorAction SilentlyContinue

}

Next we would use the cool cmdlet Invoke-VMScript to call the powershell scripts across all the 200 VM’s which internally would create the scheduled tasks! 😉 and we could disconnect from the vCenter.

$names | % { 


$script = "c:\test\RegisterScheduledtask.ps1"

Invoke-VMScript -ScriptText $script -vm $_.name -GuestCredential $guestcredential -ErrorAction SilentlyContinue


}

Disconnect-VIServer * -confirm:$false

Next we would again connect to the second vcenter and go through this process again to copy the scripts and create scheduled tasks on these additional set of VM’s

# Connect to the PoD1 vCenter, copy the scheduled task and use a Invoke-VMSCript to call the script which would register the scheduled tasks

# PoD1

Connect-VIServer ipvcsa11.corp.ad.ibs -credential $vccredential

$names = get-VM IBSC1P1* | select name

$names | % { 

Write-Host "Copying to VM Name - $_.name"

Copy-VMGuestfile -Source "C:\Users\bscadmin\Desktop\psscripts\RegisterScheduledtask.ps1" -destination "c:\test" -vm $_.name -LocalToGuest -GuestCredential $guestcredential -force -ErrorAction SilentlyContinue

Copy-VMGuestfile -Source "C:\Users\bscadmin\Desktop\psscripts\procmon.ps1" -destination "c:\test" -vm $_.name -LocalToGuest -GuestCredential $guestcredential -force -ErrorAction SilentlyContinue

}

$names | % { 


$script = "c:\test\RegisterScheduledtask.ps1"

Invoke-VMScript -ScriptText $script -vm $_.name -GuestCredential $guestcredential -ErrorAction SilentlyContinue


}


Disconnect-VIServer * -confirm:$false

The end result would be something like this, where we see a number of scheduled tasks were created on the servers.

Below are the details on the RegisterScheduledtask.ps1 which created these scheduled tasks across the servers for 38 days.

1..38 | % {

$date = Get-Date (([datetime](Get-Date -Format g)).AddDays($_))-Format d

$Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NonInteractive -NoLogo -NoProfile -ExecutionPolicy Bypass C:\test\procmon.ps1" 

$interval = New-TimeSpan -Minutes 5

$repetetion_timespan = New-TimeSpan -Hours 10

$Trigger = New-JobTrigger -Once -at "$date 9:00am" -RepetitionInterval $interval -RepetitionDuration $repetetion_timespan

Register-ScheduledTask -TaskName "ProcMon-Performance-Counter-$_" -User 'administrator' -Password 'VMwar3!!' -RunLevel Highest -Action $Action -Trigger $Trigger

}

Happy Scripting!!, i hope you found the post useful and get ideas on how PowerShell and PowerCLI can come to your rescue in touch situations! 🙂

Share this post

Post Comment