VMware Cloud Community
Vijay1996
Enthusiast
Enthusiast

How to schedule the powercli script in task scheduler

Hello, Could any one suggest me for scheduling datastore script in task scheduler and Is there modification do i need to do in below script. And the script is getting credential and it works, We need to defined the credentials in that script but password should be encrypted. $head = @""@ $body = Get-Datastore |     Select @{N='Datastore';E={$_.Name}},     @{N='CapacityGB';E={[math]::Round($_.CapacityGB,1)}},     @{N='FreeSpaceGB';E={[math]::Round($_.FreeSpaceGB,1)}},     @{N='UsedPercent';E={[math]::Round(($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB*100,1)}},     @{N='FreePercent';E={         $p = [math]::Round((1 - ($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB)*100,1)         if($p -lt 10){"#br#$($p)"}         elseif($p -lt 30){"#by#$($p)"}         else{"#bg#$($p)"}     }} |     Sort-Object -Property UsedPercent -Descending |     ConvertTo-Html -Head $head $body = $body.Replace('>#br#',' bgcolor="red">').Replace('>#by#',' bgcolor="yellow">').Replace('>#bg#',' bgcolor="green">') $sMail = @{     To = 'vijay.venkatachalam@test.com'     From = 'vijay.venkatachalam@test.com'     Subject = 'Datastore Report'     BodyAsHtml = $true     Body = $body | Out-String     SmtpServer = 'owa.test.com' } Send-MailMessage @sMail Thanks in advanse!! Vijay

7 Replies
LucD
Leadership
Leadership

You use the New-CredentialStoreItem to enter the credentials under which the scheduled script will connect to the vCenter.

This will allow you to connect to the vCenter without providing credentials.

Note that you need to do this with the same account under which you will run the Scheduled Task!

The script itself doesn't need a lot of changes.

This should do

Get-Module -Name VMware* -ListAvailable | Import-Module

Connect-VIServer -Server 'YourvCenter'

$head = @"

"@

$body = Get-Datastore |

    Select @{N='Datastore';E={$_.Name}},

         @{N='CapacityGB';E={[math]::Round($_.CapacityGB,1)}},

         @{N='FreeSpaceGB';E={[math]::Round($_.FreeSpaceGB,1)}},

         @{N='UsedPercent';E={[math]::Round(($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB*100,1)}},

         @{N='FreePercent';E={

             $p = [math]::Round((1 - ($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB)*100,1)

             if($p -lt 10){"#br#$($p)"}

             elseif($p -lt 30){"#by#$($p)"}

             else{"#bg#$($p)"}

         }} |

     Sort-Object -Property UsedPercent -Descending |

     ConvertTo-Html -Head $head

$body = $body.Replace('>#br#',' bgcolor="red">').Replace('>#by#',' bgcolor="yellow">').Replace('>#bg#',' bgcolor="green">')

$sMail = @{

     To = 'vijay.venkatachalam@test.com'

     From = 'vijay.venkatachalam@test.com'

     Subject = 'Datastore Report'

     BodyAsHtml = $true

     Body = $body | Out-String    

     SmtpServer = 'owa.test.com'

}

Send-MailMessage @sMail

Disconnect-VIServer -Server 'YourvCenter' -Confirm:$false

And finally you create the Scheduled Task.

You can do this through the Task Scheduler GUI, or via a script.

$script = 'C:\yourscript.ps1'

$user = 'your_account'

$pswd = 'your_password'

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

$sAction = @{

  Execute = "powershell.exe"

  Argument = "-NonInteractive -NoLogo -NoProfile -ExecutionPolicy Bypass $($script)"

}

$Action = New-ScheduledTaskAction @sAction

$interval = New-TimeSpan -Days 1

$repetetion_timespan = New-TimeSpan -Days 1

$sTrigger = @{

  Once = $true

  At = "$($date) 9:00am"

  RepetitionInterval = $interval

  RepetitionDuration = $repetetion_timespan

}

$Trigger = New-JobTrigger @sTrigger

$sTask = @{

  TaskName = 'Daily Datastore Report'

  User = $user

  Password = $pswd

  RunLevel = 'Highest'

  Action = $Action

  Trigger = $Trigger

}

Register-ScheduledTask @sTask


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Vijay1996
Enthusiast
Enthusiast

Hi LucD, I try to schedule the script in GUI mode but script was keep on running state and there is no output. The last run result is (0x1). I don't have idea about schedule task  via script. Action tab: Program - C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe Add argument - -NonInteractive -NoLogo -NoProfile -ExecutionPolicy Bypass $(E:\Scripts\Datastore report5.ps1) Could you please guide me where i did the mistake. Thank!

Reply
0 Kudos
LucD
Leadership
Leadership

Can you show a screenshot of the Task Scheduler forms?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Vijay1996
Enthusiast
Enthusiast

Hi LucD Sorry for delay response, I have already sent you screenshot personally. Now i run the script in task scheduler,  it doesn't shows me error in last run rather than It shows me "The operation is completed successfully .(0x0)" But i didn't get output to my email address. could you please guide me to complete this task. Thanks in advance!!

Reply
0 Kudos
RAJ_RAJ
Expert
Expert

Hi ,

Could you please check below link , it should work to schedule scripts

vSphere Advanced Technics: SCHEDULE A TASK IN POWERCLI

RAJESH RADHAKRISHNAN VCA -DCV/WM/Cloud,VCP 5 - DCV/DT/CLOUD, ,VCP6-DCV, EMCISA,EMCSA,MCTS,MCPS,BCFA https://ae.linkedin.com/in/rajesh-radhakrishnan-76269335 Mark my post as "helpful" or "correct" if I've helped resolve or answered your query!
Reply
0 Kudos
Vijay1996
Enthusiast
Enthusiast

Hi Raj, thank you for sharing, But it doesn't work, i already try it. FYI, I am using VMware Powercli 6.5 release 1. Before adding add-pssnapin VMware.VimAutomation.Core,  i didn't get below error while running the script manually in powershell but when i add the line i got below error, add-pssnapin : The Windows PowerShell snap-in 'VMware.VimAutomation.Core' is not installed on this computer. At E:\Scripts\Datastore report5.ps1:1 char:1 + add-pssnapin VMware.VimAutomation.Core + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     + CategoryInfo          : InvalidArgument: (VMware.VimAutomation.Core:String) [Add-PSSnapin], PSArgumentException     + FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.AddPSSnapinCommand

Reply
0 Kudos
LucD
Leadership
Leadership

Where did you send that screenshot?
In my VMTN mailbox?

In any case, can you resend it?

And if you are on PowerCLI 6.5R1 there are no more PSSnapin, hence the error.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos