Hi,
I am trying to run script from 'Task Scheduler' from 'Windows 2012R2' machine. I have created a credential XML file with New-VICredentialStoreItem command and added those two lines in my script:
$Credential = Get-VICredentialStoreItem -Host 'my.vcenter.as.fqdn' -File 'C:\Scripts\vicredentials.xml'
Connect-VIServer 'my.vcenter.as.fqdn' -User $Credential.User -Password $Credential.Password
If I am logged in with my own user account, the script runs perfectly when I launch it from PowerCLI box, it connects perfectly to vCenter with the credentials file.
However, when I want 'Task Scheduler' to run it, it does do anything. I do not see anything interesting in Windows events logs.
Here is the script: C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe
Here is the argument: -PSConsoleFile "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -command "&{ c:\scripts\GetVMDKsProvisionType.ps1 }"
User account used from 'Task Scheduler' has interactive logon permission to this Windows box and has files access permission to the script and the credential file.
Can you help me to understand what goes wrong?
Thanks a lot,
Regards,
Fred
Hi fsosson,
Is the task being triggered at the scheduled time or not? Do you see any logs that say that the task did start?
One thing I can think of is, add the below in the Start in field, since you are launching powershell.exe first.
C:\WINDOWS\system32\windowspowershell\v1.0\
Let me know if this works.
Cheers,
Adil Arif
Did you already try adding the -NonInteractive parameter on the powershell.exe command ?
Adn perhaps also try adding the -NoProfile parameter.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Adil,
Yes, the task starts when triggered at scheduled time. I can see many information logs from history tab:
Task Scheduler successfully finished "{b77f8f70-b323-44de-a235-6150f48448d3}" instance of the "\VMware\VMDKs provision type report" task for user "DOMAIN\user".
Task Scheduler successfully completed task "\VMware\VMDKs provision type report" , instance "{b77f8f70-b323-44de-a235-6150f48448d3}" , action "C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe" with return code 0.
Task Scheduler launched "{b77f8f70-b323-44de-a235-6150f48448d3}" instance of task "\VMware\VMDKs provision type report" for user "user" .
Task Scheduler launched action "C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe" in instance "{b77f8f70-b323-44de-a235-6150f48448d3}" of task "\VMware\VMDKs provision type report".
Task Scheduler started "{b77f8f70-b323-44de-a235-6150f48448d3}" instance of the "\VMware\VMDKs provision type report" task for user "DOMAIN\user".
Task Scheduler launch task "\VMware\VMDKs provision type report" , instance "C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe" with process ID 10336.
User "DOMAIN\user" updated Task Scheduler task "\VMware\VMDKs provision type report"
I have added C:\WINDOWS\system32\windowspowershell\v1.0\ but it does not change anything.
Fred
Hi Luc,
-NonInteractive and -NoProfile parameters do not fix the issue :smileyplain:
What kind of information can you provide you to help ?
Regards,
Fred
Guys,
I have added my own event log in the script, I can say that the script is never-ever launched by the 'Task Scheduler'.
Any idea?
Regards,
Fred
Could you include the first couple of lines of the script that you are trying to run ?
A screenshot of the Scheduled Task form might be helpful.
How did you implement that eventlog in the script ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
HI Fred,
I guess it could be a problem with the Execution Policy. Could you kindly add the argument -ExecutionPolicy Bypass in the arguments section and check once again?
Cheers,
Adil Arif
Of course, please find the first lines of my script:
Add-Content "C:\Scripts\Logs\GetVMDKsProvisionType.log" Get-Date + "Connecting to vCenter"
$Credential = Get-VICredentialStoreItem -Host 'vcenter.as.fqnd' -File 'C:\Scripts\vicredentials.xml'
Connect-VIServer 'vcenter.as.fqnd' -User $Credential.User -Password $Credential.Password
Please note that the script run well when not launched by 'Task Scheduler'
I know the consolefile should take care of that, but could you try adding the following at the beginning of the script ?
Add-PSSnapin -Name VMware*
And can you try changing that first line into
Add-Content -Path "C:\Scripts\Logs\GetVMDKsProvisionType.log" -Value "$(Get-Date) Connecting to vCenter"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
"Add-PSSnapin -Name VMware*" did not fix the problem. Do you know the requirements to run PowerCLI scripts on Windows 2012 R2 ?
I am a bit lost at the moment...
Regards,
Frederic
Depends on the PowerCLI version, but the requirements are listed in the Release Notes.
You'll find the versions and the links on here.
But the script seems to be running when you start it interactively from the PowerCLI prompt.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Fred,
Did you try checking the execution policy?
LucD, AdilArif,
I have upgraded PowerCLI 6.0 R2 to R3 then rebooted the Windows box.
I do not know why but it works a bit better, now I can see log output from my script when I run manually the scheduled task.
However, the script does not do anything except writing to my log file. I confirm the script run well when launched from PowerCLI box.
Question, is there a way to get all my script output to a file?
Fred,
This is very strange. What I can think of at the moment is to simplify the process and try running a batch file instead of calling Powershell directly.
Just add the following line as mentioned by LucD at the start of you script, which will load the necessary modules for PowerCLI.
Add-PSSnapin -Name VMware*
Next up create a batch file and call the .ps1 file by using the below command.
powershell.exe -executionpolicy remotesigned -File c:\scripts\GetVMDKsProvisionType.ps1
and run this as a batch file from Task scheduler.
Hope this resolves the mess.
Cheers,
Adil Arif
You can use redirection to capture most of the output.
In the Arguments field add something like this at the end of the line: "2>&1 > c😕temp/log.log
"
Note that this will not capture all streams!
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Guys,
Thanks a million, using a batch file fixed the issues.
Regards,
Fred
Fred,
Glad that could help resolve the issue.
It would be kind very kind of you if you could mark the question as answered.
Cheers,
Adil Arif