VMware Cloud Community
number1vspheref
Contributor
Contributor

PowerCLI Scheduled Task Won't Write To File

Hi,

I'm running Powershell 2, Powercli 5.5R2 and Win 7 32-bit.

I've got a PowerCLI task that I want to run as a scheduled task ... syntax is below (I'm running the command against a number of different datastores but just showing one get-datastore command here).

get-datastore "<data-store-name>" | get-stat -stat "datastore.numberreadaveraged.average" | export-csv C:\Users\<filepath>\$CurrentDate-readiops.csv

Works fine when I run from command-line or as a scheduled task when I'm logged in but when I run as a scheduled task when I'm disconnected (I access the VM as a hosted desktop), the output CSV file is created but it's of zero size. I've tried every possible combination (I can think of Smiley Happy ) of "Run Only When User Is Logged On" , "Run Whether User Is Logged On Or Not" etc from Task Scheduler but doesn't appear to make any difference. I've put in a simple DOS echo command in the PowerCLI script and redirected to an output file and that works fine so it seems to me like it's specifically something to do with the redirection of the get-datastore / PowerCLI specific output when my connection to the VM is disconnected. It seems like I've got the basics of running a PowerCLI scheduled task working fine ... in Task Scheduler:

Program / Script is set to "c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"

and

Add arguments (optional) is set to "-psconsolefile "c:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noexit c:\<path-to-script>\daily.ps1"

I'm guided by http://www.virtu-al.net/2009/07/10/running-a-powercli-scheduled-task/

Any ideas on what I'm missing here?

Thanks!

number1vspherefan.

0 Kudos
15 Replies
LucD
Leadership
Leadership

Just to make sure, you did add a Connect-VIServer in the script ?


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

0 Kudos
number1vspheref
Contributor
Contributor

Hi,

Yes, I have added connect-viserver ... however, I didn't pass any username / password credentials explicitly, so script gets those from the current session. Is there a possibility that this doesn't work when the session is disconnected (not logged out)?

Thanks,

number1vspherefan.

0 Kudos
LucD
Leadership
Leadership

No, that shouldn't be a problem, you can have multiple sessions with the same account.

But, since you are using SSO for the Connect-VIServer, is the scheduled task running under an account that has the required privileges ?

You could check by logging on with that account and running the script from the PowerCLI prompt.


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

0 Kudos
number1vspheref
Contributor
Contributor

Yes, the scheduled task runs under an account that has the required privileges. It runs fine from the PowerCLI prompt.

Thanks,

number1vspherefan.

0 Kudos
LucD
Leadership
Leadership

Can you attach the script your are trying to run as a scheduled task (or at least the part that doesn't work) ?


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

0 Kudos
number1vspheref
Contributor
Contributor

Relevant portion of script attached as requested.

0 Kudos
LucD
Leadership
Leadership

That looks all quite ok.

Did you try by adding the User and Password on the Connect-VIServer line.

I know that shouldn't be hardcoded, but just to test if that could be the cause of the problem.

Another thing you might try, suppress all warning messages.

You can do that by adding a line

$warningPreference = SilentlyContinue


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

0 Kudos
number1vspheref
Contributor
Contributor

Hi,

I've added the username and password and suppressed warning messages, doesn't fix the problem so not sure on next steps. Is there any enhanced logging that I can turn on to help me?

Thanks,

number1vspherefan.

0 Kudos
RvdNieuwendijk
Leadership
Leadership

Does the scheduled task run under an account that has rights to write the C:\Users\<filepath>\$CurrentDate-readiops.csv file?

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

Yes it does, the file is created fine, it's just empty...

0 Kudos
LucD
Leadership
Leadership

You could try the STart-Transcript and Stop-Transcript cmdlets, or revert to some Write-Output lines at strategic places in the script.

And split the lines in smaller parts, so you can see the intermediate results (in other words what you place in the pipeline).

For example, first do the Get-Datastore and store the result in a variable.

Then use that variable as input to the Get-Stat cmdlet.

Now you can display the contents of that variable with a Write-Output cmdlet.


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

0 Kudos
number1vspheref
Contributor
Contributor

OK, I haven't had a chance to work on this for a while but tried some things in the last day or so...

Enabling the transcript didn't really produce anything noteworthy.

.

I followed LUCD's advice to break out some of the variables, see five lines of code below:

set-variable -name datastorename -value (get-datastore "<datastore-name>")

echo $datastorename

set-variable -name iopsoutput -value (get-stat -stat "datastore.numberreadaveraged.average" $datastorename)

echo $iopsoutput

echo $iopsoutput | export-csv C:\<PATH>\test.csv

When I run above code and am logged in and connected to the virtual desktop, both $datastorename and $iopsoutput are echoed to the command-line and the contents of $iopsouput are written to test.csv correctly.

However, when I am disconnected from the desktop (but still logged in), $datastorename is written to command-line correctly but $iopsoutput is neither written to the command-line nor to the file (the file has zero size as previously).

Any ideas? Can't say I do :smileyconfused:

Thanks!

number1vspherefan.

0 Kudos
LucD
Leadership
Leadership

Try changing

set-variable -name iopsoutput -value (get-stat -stat "datastore.numberreadaveraged.average" $datastorename)

to

set-variable -name iopsoutput -value (get-stat -stat "datastore.numberreadaveraged.average" -Entity $datastorename)

I suspect the problem in that line might be related to the use of named and positional parameters incorrectly.

To avoid any issue, I would use all named parameters.

See if that brings you any further.


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

0 Kudos
number1vspheref
Contributor
Contributor

Unfortunately adding -Entity made no difference. When I run as scheduled task and am disconnected and redirect "echo $datastorename" to file, redirection works fine but exporting the $iopsoutput variable to csv gives no joy, zero file size... Looking like i'll have to resign myself to not being able to run this as scheduled task ... strange!

numbwe1vspherefan.

0 Kudos
LucD
Leadership
Leadership

Out of curiosity, does a Set-Content instead of an Export-Csv work ?


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

0 Kudos