VMware Cloud Community
athurston
Enthusiast
Enthusiast

Recurrance

Hello, 

I'm looking to set a recurrence of a task for the last Monday of every month but when i look to add a monthly recurrence i only get a date and time, is there a way i can set this up?

Thanks

Alex

Reply
0 Kudos
6 Replies
vijayrana968
Virtuoso
Virtuoso

Save this power shell script and add this into Windows Task scheduler with occurrence. Change parameters accordingly.

##VM name

$VM2change = "test-server"

##Email Settings

$emailServer = "yourSMTPServer"

$sender = "powercli@vsphere.local"

$recipients = "admin@vsphere.local"

##Load VMware PS plugin

Add-PSSnapin VMware.VimAutomation.Core

##Connect to vCenter

connect-viserver -server vcenter.vsphere.local -User administrator@vsphere.local -Password Password123

###########################Start- Custom Task #########################

$beforechange = (GET-VM -Name $VM2change|FT -auto MemoryGB|out-string)

##Stop VM

GET-VM -Name $VM2change| Stop-VMGuest -Confirm:$False

start-sleep -s 180

##Change Memory

GET-VM -Name $VM2change| set-vm -MemoryGB 28 -Confirm:$False

##Start VM

GET-VM -Name $VM2change| Start-VM -Confirm:$False

$afterchange = (GET-VM -Name $VM2change|FT -auto MemoryGB|Out-String)

##ping VM

start-sleep -s 120

$isalive= (Test-Connection -ComputerName $VM2change -count 1|Out-String)

###########################End- Custom task #########################

##Compose eMail and send

$body = @"

Memory Before,$beforechange.

Memory After, $afterchange.

Is VM up??, $isalive

"@

send-mailmessage -from $sender -to $recipients -subject "VM Memory Change $VM2change" -Bodyashtml "$body" -smtpserver $EmailServer

Reply
0 Kudos
athurston
Enthusiast
Enthusiast

Hi Thanks, 

im not looking to schedule a windows task im looking to schedule a task within vRealize orchestrator, i have a workflow i need to run on the last monday of every month. 

Thanks

Alex

Reply
0 Kudos
vijayrana968
Virtuoso
Virtuoso

Ok, I thought its vCenter. Sorry for that Smiley Happy

Reply
0 Kudos
athurston
Enthusiast
Enthusiast

no problem
Reply
0 Kudos
iiliev
VMware Employee
VMware Employee

I don't think this can be expressed directly in the scheduler UI.

Not sure if it is applicable for your use case but you can try to set the recurrence to be 'every week', Monday. This way, your scheduled workflow will be executed every Monday. Then, inside the workflow, you can add some logic to check if the current date is the last Monday of the month, and immediately return without doing anything if the current date is not the last Monday of the month. Of course, you need some code to check if the given date/Monday is the last Monday in the month, but this should be trivial - either google for some existing Javascript code, or just get the current date, extract the day number within the month, and then check if the day + 7 is still within the same month or within the next month.

Reply
0 Kudos
athurston
Enthusiast
Enthusiast

Thanks iiliev, so i have found this but im unsure on how i then get the workflow to run based on the answer of this script, i presume its a custom descision i add in to the workflow with this code but then how does this know to then run the next step if the answer from the script is a yes?

Thanks

Alex

 

function Get-LastFridayOfMonth([DateTime] $d) {   
    $lastDay = new-object DateTime($d.Year, $d.Month, [DateTime]::DaysInMonth($d.Year, $d.Month))
    $diff = ([int] [DayOfWeek]::Friday) - ([int] $lastDay.DayOfWeek)
 
    if ($diff -ge 0) {
        return $lastDay.AddDays(- (7-$diff))
    }
    else {
        return $lastDay.AddDays($diff)
    }   
}
Reply
0 Kudos