PowerCLI

 View Only
  • 1.  Scheduling a powerCLI .ps1 script to run using Windows Task Scheduler

    Posted Mar 06, 2017 09:24 PM

    I've been trying to figure out how I can use Windows Task Scheduler to run a ps1 script I currently run manually. However when I test any of the suggestions I've found I run into at least one of the issues below.

    My current setup is a Win2012R2 or Win10 OS, using VMware vSphere PowerCLI 6.3 R1

    The issues that stop me are:

    1. Scheduler opens a PowerShell prompt, but does not open the PowerCLI  prompt
      • If PS is opened and the PS1 file opens I get a sting of errors as the PowerCLI commands are not recognized
    2. New PowerCLI session opens but I cannot send the `Connect-ViServer` command
      • I'm ok placing the server in the text of the PS1 file, but I would like to avoid saving the user and password in the same location
    3. Bonus points if I can pass parameters as I can when I call a PS1 directly
      • c:\Scripts\VMlist.ps1 -host location

    I've tried both methods in presented in the (virtual.al)[http://www.virtu-al.net/2009/07/10/running-a-powercli-scheduled-task/] without any success. I've also tried some of the comments left by LucD‌ in PowerCli Script -Scheduled Task as well as other comments, website, and chats.



  • 2.  RE: Scheduling a powerCLI .ps1 script to run using Windows Task Scheduler

    Posted Mar 06, 2017 09:28 PM

    Can you upgrade to PowerCLI 6.5R1?

    That way there are only modules, and you don't have to bother with loading PSSnapin.

    Would you mind sharing the script you are trying to run in the Windows Task scheduler?
    Does the script run in a regular PowerCLI prompt with the account you specified for the scheduled task?



  • 3.  RE: Scheduling a powerCLI .ps1 script to run using Windows Task Scheduler

    Posted Mar 06, 2017 10:05 PM

    Sorry I'm going though PowerCLI DiskSpaceReport generates no HTML output when executed via Task Scheduler (otherwise it works fine) which could have some of the solutions I'm looking for. I still consider myself as a novice when it comes to PowerCLI and Windows Task Scheduler. Reading though that trend I feel like I maybe missing something.

    I do have PowerCLI6.5R1 running on Windows10. I've been testing commands using a cmd prompt, so I don't need to wait for the scheduler. 

    At this point I only run the powerCLI script manually during maintenance period from the VMwarePowerCLI interface.

    Write-Host "The csv file includes the following headers Name, startDS, Newstore1, Newstore2";

    Write-Host "You have selected to move all drives based on a csv file. I will proceed shortly.";

    Write-Host "To stop at any time select Ctrl C.";

    Sleep -s 2

    Write-Host "";

    Write-Host "I will not move if the destination dive is over 75% full, or if the drive belongs to a production VM.";

    Write-Host "";

    Sleep -s 2

    $csv = Import-csv c:\tmp\VMfromDatastore.csv

    foreach ($line in $csv)

      {#Write-Host "$line.Name"; #debug line

      IF ($line.Name -like '*prod*')

      {Write-Host "Skipped as this is a production VM";

      continue}

      #StArT of datastore capacity check

      Write-Host "Checking DS1";

      $destinationDSfs = [math]::Round(((Get-Datastore | where {$_.Name -eq $line.Newstore1}| Get-View).Summary.FreeSpace)/1073741824)

      $destinationDScap = [math]::floor(((Get-Datastore | where {$_.Name -eq $line.Newstore1}| Get-View).Summary.Capacity)/1073741824)

      $sizeOFmovingHD = (Get-HardDisk -vm $line.Name | where {$_.filename -match $line.startDS}).CapacityGB

      $sizeOFmovingHD = ($sizeOFmovingHD|Measure-Object -sum).sum

      $precentFS = [math]::floor(100*(($destinationDSfs-$sizeOFmovingHD)/$destinationDScap));

      IF ($precentFS -ge 25) { #if >25% free space move to Newstore1

      Get-HardDisk -vm $line.Name | where {$_.filename -match $line.startDS} |Move-HardDisk -Datastore $line.Newstore1 -Confirm:$false -whatif} #heavylifting line

      Else { #checking 2nd Datastore

      Write-Host "Checking DS2";

      $destinationDSfs = [math]::Round(((Get-Datastore | where {$_.Name -eq $line.Newstore2}| Get-View).Summary.FreeSpace)/1073741824)

      $destinationDScap = [math]::floor(((Get-Datastore | where {$_.Name -eq $line.Newstore2}| Get-View).Summary.Capacity)/1073741824)

      $sizeOFmovingHD = (Get-HardDisk -vm $line.Name | where {$_.filename -match $line.startDS}).CapacityGB

      $sizeOFmovingHD = ($sizeOFmovingHD|Measure-Object -sum).sum

      $precentFS = [math]::floor(100*(($destinationDSfs-$sizeOFmovingHD)/$destinationDScap));

      IF ($precentFS -ge 25) { #if >25% free space move to Newstore2

      Get-HardDisk -vm $line.Name | where {$_.filename -match $line.startDS} |Move-HardDisk -Datastore $line.Newstore2 -Confirm:$false -whatif} #heavylifting line

      Else { Write-Host "Skipped due to space limitation."

      continue} #If datastore will be more than 75% full after move, skiped VM move

      } #end of Else checking 3rd Datastore

      } #end of Else checking 2nd Datastore

      #End of datastore capacity check

      Sleep -s 5} #end of foreach loop

    Write-Host "";

    Write-Host "All servers in c:\tmp\VMfromDatastore.csv either moved or skiped due to VM type or space restrictions on new server."

    Write-Host "Thank you, and have a nice day";

    Write-Host "End of line";



  • 4.  RE: Scheduling a powerCLI .ps1 script to run using Windows Task Scheduler
    Best Answer

    Posted Mar 07, 2017 06:16 AM

    If you schedule the script you will need to add a couple of things.

    • load the PowerCLI modules
    • connect to the vSphere server

    Add these lines at the beginning of the scheduled script (provided the host has PowerCLI 6.5R1 installed).

    And the account that runs the scheduled task has access to the vSphere server, and has a PowerShell environment set up.

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

    Connect-VIServer -Server YourVC



  • 5.  RE: Scheduling a powerCLI .ps1 script to run using Windows Task Scheduler

    Posted Mar 09, 2017 02:24 PM

    Thank you LucD‌ that is working.



  • 6.  RE: Scheduling a powerCLI .ps1 script to run using Windows Task Scheduler

    Posted Feb 24, 2018 07:55 AM

      Here is how to use task scheduler in Windows 10

    1- Launch Task Scheduler by typing Task Scheduler in the Start menu.

    2- Click on the Create Basic Task link at the right side of the Task Scheduler window. This will open up a new easy to use wizard for creating the required task. By clicking on Create Task, you can have advanced options.

    3- Give a Name and Description for the task. Click on Next.

    4- Select when you want the task to start. You can set the task to run daily, weekly monthly or only one time. You also have other options to start the task. You can trigger the task when the system turns on or when you log on or in response to an event ID in the Windows event log.

    5- If you have selected daily, weekly, monthly or one time then you will be asked to specify the time.

    6- You can start a program or send an email or display a message. The last two options are deprecated in Windows 10 and Windows 8.1.

    7- If you want to run a program, click on the Browse button and locate the program’s .exe file on your PC. Selecting the desired program will launch the program automatically at the specified time.

    8-Here I have selected Chrome. By clicking on Finish, you can create the task.

    9- For more options on the task you have created, right click on it. You have many options including delete. Now select properties.

    10- From the opened window you can customize your task.

    11- You can even set new trigger for the task from the Triggers option.

    Source:- https://merabheja.com/use-task-scheduler-windows-10/