VMware Cloud Community
maravillo
Contributor
Contributor

Script generates 0-sized CSV when run in Scheduled Tasks

Hi all,

I have a set of batch and powershell scripts which runs perfectly and generates a CSV file on the command line. However when it is ran as a Windows' Scheduled Task, it only creates a 0-sized CSV file.

Appreciate any info or help on this. Thanks!

c:\report\gen-vm-ds.cmd:

@echo off

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\vim.psc1" -command "&"

c:\report\gen-vm-ds.ps1:

$WarningPreference ="SilentlyContinue"

$vcs = Connect-VIServer vcenter1

Get-VM -Server $vcs | %{

$VM = $_

$DS = @()

$tmp = Get-Datastore -VM $VM

$tmp | %{

$DS += $_

}

"" | Select-Object @{N="VM";E={$VM}},@{N="DS";E={[string]::join(",",$DS)}}

} | Sort VM | Export-Csv -Path c:\report\vm-ds.csv -NoTypeInformation

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership

Try placing a Start-Transcript and Stop-Transcript cmdlet at the beginning and end of your .ps1 script.

It will show you what goes wrong.

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
maravillo
Contributor
Contributor

Hi LucD,

I commented the $WarningPreference="SilentlyContinue" line from the original code which suppressed any warning, and added the Start-Transcript/Stop-Transcript as you suggested. This is what I get from the transcript logs:

WARNING: There were one or more problems with the server certificate:

  • A certification chain processed correctly, but terminated in a root certificate which isn't trusted by the trust provider.

  • The certificate's CN name does not match the passed value.

I've been getting these warnings before from the command line, and the CSV file do get filled with data.

The problem only happens when the scripts runs from Scheduled Tasks.

Reply
0 Kudos
avlieshout
VMware Employee
VMware Employee

Does the script run in the right user context?

Since you don't specifically specify logon credentials when using connect-viserver.

If you run PowerCLI4.1 you could simplify your script.

try this:

$WarningPreference ="SilentlyContinue"
$vcs = Connect-VIServer vcenter1
New-VIProperty -ObjectType VirtualMachine -Name dsList -Value {$args[0]|Get-Datastore}
Get-VM | Select-Object Name,dsList | Export-Csv -Path c:\report\vm-ds.csv -NoTypeInformation 

-


Arnim van Lieshout

Blogging: http://www.van-lieshout.com

Twitter: http://www.twitter.com/avlieshout

If you find this information useful, please award points for "correct" or "helpful".

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
Reply
0 Kudos
LucD
Leadership
Leadership

From the directory path for the console file in your BAT file, it looks as if you are running an older VI Toolkit version.

Perhaps try upgrading to PowerCLI 4.1 and then follow the sample Arnim gave.

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos