VMware Cloud Community
scotty_p
Enthusiast
Enthusiast

Scheduling Task with PowerCLI

I'm trying to run a simple PowerCLI script to print out the number of VMs in our cluster, send it to a CSV file and email it to me.

I have 2 problems when trying to do this?

1. When I paste the script into PowerCLI, it will still prompt me for a login (even though I specify the username and password). Once I enter my credentials it will run as expected. How can I get around this?

2. I copied the text below into a .bat file and scheduled a Windows task to run the bat file every morning. It will run, but it doesn't update the .csv file or recognize the variables.

I know something is wrong. Can anyone point me in the right direction?

connect-viserver vcenterserver -user domain\username -password pass
$cnt = (get-vm).count
$sub = "VM Count = $cnt"
get-vm | where-object {$_.name -like "*wp*"} | select name,numcpu,memorymb,host | export-csv c:\temp\vms.csv

Send-MailMessage -Attachments c:\temp\vms2.csv -From email@domain.com -To email@domain.com -smtpserver smtp -Subject $sub

Thanks,

Scott

0 Kudos
13 Replies
LucD
Leadership
Leadership

1. Did you store the script in a .ps1 file and start it from the PowerCLI prompt

.\MyScript.ps1

2. Have a look at Alan's Running a PowerCLI Scheduled Task post.

Make sure to specify the correct credentials for the scheduled task, otherwise you might not have to correct permissions.


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

0 Kudos
scotty_p
Enthusiast
Enthusiast

Thanks for the response.

I can run it from the command line - .\script.ps1 but it prompts me for credentials. After I enter them, it will run fine.

I've tried the options in the blog, but those don't work. If I add the line add-pssnapin VMware.VimAutomation.Core line to the beginning of the script then try to add  C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe "& 'C:\Scripts\MyScript.ps1'" to the scheduled task, it won't send me an email so I know it's not working.

Any other ideas on how to get this to work or what I'm missing?

Thanks,

Scott

0 Kudos
LucD
Leadership
Leadership

Did you try the 2nd method from Alan's post ?

That doesn't require an Add-PSSnapin in the script.

Out of curiosity, can you paste the following lines into a .ps1 file and try to execute them

$mySRv = Connect-VIServer -Server vcenterserver -User domain\username -Password pass
$defaultVIServers
Disconnect-VIServer -Server $mySRv -Confirm:$false

Do you get a prompt ?

Does it display the server(s) ?

Almost forgot, what vCenter version are you running this against ?

And which PowerCLI version ? Do a

Get-PowerCLIVersion


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

0 Kudos
scotty_p
Enthusiast
Enthusiast

I tried the script you provided and it appeared to run fine, however the powershell window popped up quick then disappeared. Although a login box popped up and I had to enter credentials.

Our vCenter and hosts are at 4.1 U1.

Powershell is...


PowerCLI Version
----------------
   VMware vSphere PowerCLI 4.1 U1 build 332441
---------------
Snapin Versions
---------------
   VMWare vSphere PowerCLI 4.1 U1 build 332441

I'm wondering if my scheduled task is set up correctly. Here are my settings

Action - Start a Program: C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe
Add arguments: -command "&{D:\Docs\Powershell\VMware\vmcount.ps1}"

Start in: D:\Docs\Powershell\VMware\

Do you see anything wrong with these settings.

My script now looks like this. Any help is much appreciated.

add-pssnapin VMware.VimAutomation.Core

connect-viserver wpvct01 -user domain\username -password pass

$cnt = (get-vm).count

$sub = "VM Count = $cnt"

get-vm | where-object {$_.name -like "*wp*"} | select name,numcpu,memorymb,host | export-csv c:\temp\vms2.csv

Send-MailMessage

-Attachments c:\temp\vms2.csv -From email@domainemail@domain.com.com -To email@domainemail@domain.com.com -SmtpServer smtp -Subject $sub

0 Kudos
LucD
Leadership
Leadership

It looks as if you're scheduling the task on a Win7 64-bit machine.

Is that correct ?


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

0 Kudos
scotty_p
Enthusiast
Enthusiast

No. It's actually Windows 7 32-bit.

0 Kudos
LucD
Leadership
Leadership

You could perhaps try the following variation.

On the Start Program tab you enter everything, something like this

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" "& 'D:\Docs\Powershell\VMware\vmcount.ps1'"

Since you are using the PowerCLI console file, you don't have to add the Add-PSSnapin line to your script.

You could perhaps keep a transcript of the running of your script.

Like this

Start-Transcript "D:\Docs\Powershell\VMware\script.log"

connect-viserver wpvct01 -user domain\username -password pass

$cnt = (get-vm).count

$sub = "VM Count = $cnt"

get-vm | where-object {$_.name -like "*wp*"} | select name,numcpu,memorymb,host | export-csv c:\temp\vms2.csv

Send-MailMessage

-Attachments c:\temp\vms2.csv -From email@domainemail@domain.com.com -To email@domainemail@domain.com.com -SmtpServer smtp -Subject $sub

Stop-Transcript

The transcript shows perhaps what goes wrong.

Note that the transcript will not capture all output streams.


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

0 Kudos
scotty_p
Enthusiast
Enthusiast

Thanks for the suggestion. I tried adding the start and stop transaction lines to get a log, but nothing was generated.  Is there a way to see what is failing by some log or something?

Thanks.

0 Kudos
RvdNieuwendijk
Leadership
Leadership

You can change the commandline to start PowerShell into:

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" "& 'D:\Docs\Powershell\VMware\vmcount.ps1'" > vmcount.log 2> vmcount-error.log


This will write the output of the PowerShell session to vmcount.log and the error output to vmcount-error.log. Perhaps you have to specify a full path for both files.

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
scotty_p
Enthusiast
Enthusiast

Thanks for the suggestion. It did populate some info into the vmcount-error.log file. Does this make any sense to you? Thanks.

Add-PSSnapin : Cannot add Windows PowerShell snap-in VMware.VimAutomation.Core because it
is already added. Verify the name of the snap-in and try again.
At D:\Docs\Powershell\VMware\vmcount.ps1:1 char:13
+ add-pssnapin <<<<  VMware.VimAutomation.Core
    + CategoryInfo          : InvalidArgument: (VMware.VimAutomation.Core:String) [Add-PS
   Snapin], PSArgumentException
    + FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.AddPSSnapinCo
   mmand

0 Kudos
mcowger
Immortal
Immortal

You are double adding a snapin.  Remove the explicit addition of the Snapin.

--Matt VCDX #52 blog.cowger.us
0 Kudos
RvdNieuwendijk
Leadership
Leadership

The PowerCLI snapin is added in the commandline and in the script. The second one now gives the error. You can remove the line:

add-pssnapin VMware.VimAutomation.Core

from the script.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
scotty_p
Enthusiast
Enthusiast

Thanks for your help. I'm getting close now.

I've changed the Add Arguments to - -noninteractive  -noprofile -command D:\Docs\Powershell\VMware\vmcount.ps1

This now runs fine, however it doesn't appear to recognize the variables. I get an email and the .csv file is updated, but the subject is "VM Count ="

Below is my script

add-pssnapin VMware.VimAutomation.Core
connect-viserver wpvct01 -user domain\username -password pass
$cnt = (get-vm).count
$sub = "VM Count = $cnt"
get-vm | where-object {$_.name -like "*wp*"} | select name,numcpu,memorymb,host | export-csv c:\temp\vms2.csv
Send-MailMessage -Attachments c:\temp\vms2.csv -From email@domain.com -To email@domain.com -SmtpServer smtp -Subject $sub

0 Kudos