I'm trying to set up a powercli script as a scheduled task, but it's not connecting to vCenter before running the task. I set it up to send an email and it does, but with not with the specific VMware commands. Below is how I have the task set up.
Program/Script:
C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe
Add arguments (optional):
-PSConsoleFile "E:\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" "& 'e:\scripts\vmcount.ps1'"
and here is the script I'm running.
connect-viserver -server server.domain.com -user domain\user -password pass
$VMs=(get-vm).count
$ChiDS=(Get-Datacenter -name Chicago | get-datastore).count
$SavDS=(Get-Datacenter -name Savvis | get-datastore).count
Send-MailMessage -SmtpServer nauschieml4.domain.com -From user@domain.com -Subject "Inventory" -to
user@domain.com -body "VM Count = $VMs / Chicago Datastores = $ChiDS / SavvisDS = $SavDS"
For some reason, this is what I'm receiving in my email.
VM Count = / Chicago Datastores = / SavvisDS =
Can anyone tell me how I can fix this?
Thanks a lot,
Scott
Try capturing any messages by redirecting the output to a file.
Add for example '> script.log 2> script-errors.log' at the end of the command in the scheduled task.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Those errors just confirm that the Connect-VIServer didn't work.
Can you scroll back and perhaps show the error on the Connect-VIServer cmdlet ?
Under which account are you running the scheduled task ?
Can you try to logon with account you specified on the Connect-VIServer ? Does the script work under that account ?
Does a Start-Transcript and Stop-Transcript in the script show any error messages ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Here is the error on the screen. I also tried a connect-viserver with this account and it connects. Any ideas?
[vSphere PowerCLI] E:\scripts> .\vmcount.ps1
Get-VM : 6/19/2012 3:28:55 PM Get-VM Server server.domain.com not connected.
At E:\scripts\vmcount.ps1:1 char:13
+ $VMs=(get-vm <<<< ).count
+ CategoryInfo : InvalidArgument: (:) [Get-VM], ViServerConnectio
nException
+ FullyQualifiedErrorId : Client20_ConnectivityServiceImpl_TryVerifyIsConn
ected_NotConnected,VMware.VimAutomation.Commands.GetVM
Get-Datacenter : 6/19/2012 3:28:57 PM Get-Datacenter Server server.domain.com not connected.
At E:\scripts\vmcount.ps1:2 char:23
+ $ChiDS=(Get-Datacenter <<<< -name Chicago | get-datastore).count
+ CategoryInfo : InvalidArgument: (:) [Get-Datacenter], ViServerC
onnectionException
+ FullyQualifiedErrorId : Client20_ConnectivityServiceImpl_TryVerifyIsConn
ected_NotConnected,VMware.VimAutomation.Commands.GetDatacenter
Get-Datacenter : 6/19/2012 3:28:59 PM Get-Datacenter Server server.domain.com not connected.
At E:\scripts\vmcount.ps1:3 char:23
+ $SavDS=(Get-Datacenter <<<< -name Savvis | get-datastore).count
+ CategoryInfo : InvalidArgument: (:) [Get-Datacenter], ViServerC
onnectionException
+ FullyQualifiedErrorId : Client20_ConnectivityServiceImpl_TryVerifyIsConn
ected_NotConnected,VMware.VimAutomation.Commands.GetDatacenter
I'm afraid those errors are the result of not being connected.
You would like to see the messages coming from the Connect-VIServer cmdlet.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I am having the same problem, schduled task in Win2k3 will not run unless I login as the Run as: account.
I have piped the error and vmcount logs, no errors
Like Luc said, can you confirm both the script and the account work outside of the scheduled task>?
Yes, they do work
This is probably a silly question, but can you also confirm, that the "Run only if logged on" box is not checked?
Hmm, I haven't tried it before, but I guess another option might be just loading the cmdlets you need in your script using add-pssnapin instead of using the -psconsolefile parameter...