VMware Cloud Community
NelsonCandela
Enthusiast
Enthusiast
Jump to solution

PowerCLI DiskSpaceReport generates no HTML output when executed via Task Scheduler (otherwise it works fine)

Dear community,

I have created a PowerCLI-Script that generates an HTML file with a disk space report.

The script checks and collects disk/partition usage on specific VMs, generates a HTML file including all this information and sends it via e-mail to a specified address.

This is how the output usually looks like:

Speicherplatzreport-Ergebnis-OK.png

The script works perfectly fine when I execute it manually, meaning when I open up the PowerCLI console, type in the file path and hit Enter. The output then looks exactly like I want it to -- and that is what's shown above.

My problem is:

As soon as the script is called/executed via the Windows Task Scheduler the generated HTML output lacks the required information.

The output file then only contains the static HTML part, none of the dynamic contents are being loaded into that file and I have no idea why this happens.

Speicherplatzreport-Ergebnis-NOK.png

This is how it looks like when the dynamic content misses, so when being executed via Task Scheduler rather than being executed manually.

Let me explain how the system and task is set up:

Server: Windows Server 2012 R2 (x64)

User: I've created a local admin user account to run that script (User TaskAdmin)

Task: Please see below the Task Scheduler settings for this specific task information

Speicherplatzreport-Ergebnis-Task_01.png

Speicherplatzreport-Ergebnis-Task_02.png

Speicherplatzreport-Ergebnis-Task_03.png

Speicherplatzreport-Ergebnis-Task_04.png

Action / script call:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file "D:\PowerCLI-Scripts\Speicherplatzreport.ps1"

Speicherplatzreport-Ergebnis-Task_05.png

Speicherplatzreport-Ergebnis-Task_06.png

My idea is that the script somehow fails to remotely connect to the vCenter instance; when no connection can be established no information can be pulled from the running VMs. But how can I accomplish this so that the script works both ways? Is there a way to get a detailed error message out of this to be able to see where it fails -- or solve it directly? 🙂

Any help with this would be greatly appreciated, thanks to you in advance.

BR

NC

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

One way is to run the PowerCLI initialization script (Initialize-PowerCLIEnvironment.ps1) in your script.

Another option is to load the PowerCLI PSSnapin & modules yourself.

I wrote a function for that, see my Universal PowerCLI Loader post.


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

View solution in original post

Reply
0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

Which Role did you give user TaskAdmin in the vCenter?

Does the account have sufficient permissions to connect and collect the data?

Did you logon with user TaskAdmin, and run the script under that account?

Does that generate the report?

You could try to use the Start-Transcript cmdlet to capture the output.


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

NelsonCandela
Enthusiast
Enthusiast
Jump to solution

Dear Luc,

thanks a lot for your quick reply.

I figured I could forget something (important) 😕

The user TaskAdmin is there only to execute the script and doesn't have access to the vCenter.

The connection in the script is established with the SSO Admin:

# vCenter Login

# ===============================================================

$vCenterServer             = 'vc-blabla-001'

$vCenterUser               = 'Administrator@vsphere.local'

$vCenterPass               = 'vCenterSSOAdminPassword'

The connection string looks like this:

Connect-VIServer -Server $vCenterServer -User $vCenterUser -Password $vCenterPass -ErrorAction Continue

I should then probably track the connection with the Start-Transcript cmdlet as you mentioned.

Thanks in advance

NC

Reply
0 Kudos
NelsonCandela
Enthusiast
Enthusiast
Jump to solution

Okay, I think the following errors explains everything ...

Host Application: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file D:\PowerCLI-Scripts\Speicherplatzreport.ps1

Process ID: 7256

**********************

Transcript started, output file is D:\Export\transcript.txt

Connect-VIServer : The term 'Connect-VIServer' is not recognized as the name of a cmdlet, function, script file, or

operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try

again.

At D:\PowerCLI-Scripts\Speicherplatzreport.ps1:69 char:1

+ Connect-VIServer -Server $vCenterServer -User $vCenterUser -Password $vCenterPas ...

+ ~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (Connect-VIServer:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

Get-VM : The term 'Get-VM' is not recognized as the name of a cmdlet, function, script file, or operable program.

Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

At D:\PowerCLI-Scripts\Speicherplatzreport.ps1:75 char:17

[...]

Any idea how to in fact execute the PowerCLI environment rather than the regular PowerShell?

How would I load the module before the script in order to be able to run PowerCLI commands and actually connect to the vCenter server first?

Thanks again Luc for the hint with the transcript, feels like I'm getting closer 😉

BR

NC

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

One way is to run the PowerCLI initialization script (Initialize-PowerCLIEnvironment.ps1) in your script.

Another option is to load the PowerCLI PSSnapin & modules yourself.

I wrote a function for that, see my Universal PowerCLI Loader post.


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

Reply
0 Kudos
NelsonCandela
Enthusiast
Enthusiast
Jump to solution

Dear Luc,

unfortunately I wasn't able to solve this.

I have tried both approaches with the automated loading of the Initialize-PowerCLIEnvironment.ps1 file and the Universal PowerCLI Loader you created.

In the first case the error message looks like this:

**********************

Windows PowerShell transcript start

Start time: 20160921154509

Username: VCHLP-CGN-001\TaskAdmin

RunAs User: VCHLP-CGN-001\TaskAdmin

Machine: VCHLP-CGN-001 (Microsoft Windows NT 6.3.9600.0)

Host Application: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file D:\PowerCLI-Scripts\Speicherplatzreport.ps1

Process ID: 12900

**********************

Transcript started, output file is D:\Export\transcript.txt

x86 : The term 'x86' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the

spelling of the name, or if a path was included, verify that the path is correct and try again.

At D:\PowerCLI-Scripts\Speicherplatzreport.ps1:16 char:90

+ ... Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-Po ...

+                    ~~~

    + CategoryInfo          : ObjectNotFound: (x86:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

I've called the Initialize-PowerCLIEnvironment.ps1 file like this:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noe -c ". \"C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1\""

(this is the same link as used to start the PowerCLI shortcut on the Desktop - that also works from (elevated) command windows)

In the second case with your function (function added to the script and called it) the error message was:

**********************

Windows PowerShell transcript start

Start time: 20160921153955

Username: VCHLP-CGN-001\TaskAdmin

RunAs User: VCHLP-CGN-001\TaskAdmin

Machine: VCHLP-CGN-001 (Microsoft Windows NT 6.3.9600.0)

Host Application: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file D:\PowerCLI-Scripts\Speicherplatzreport.ps1

Process ID: 5124

**********************

Transcript started, output file is D:\Export\transcript.txt

Connect-VIServer : The term 'Connect-VIServer' is not recognized as the name of a cmdlet, function, script file, or

operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try

again.

At D:\PowerCLI-Scripts\Speicherplatzreport.ps1:205 char:1

+ Connect-VIServer -Server $vCenterServer -User $vCenterUser -Password $vCenterPas ...

+ ~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (Connect-VIServer:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

The task now took a little longer to run but as you can see from the above message it didn't really change the situation.

Did I make a mistake with how I loaded the file? I don't get it 😞

Thanks for your continuous support!

BR

NC

Reply
0 Kudos
NelsonCandela
Enthusiast
Enthusiast
Jump to solution

Dear everyone,

it has been solved.

I added two lines of code to the file:

Add-PSSnapin VMware.VimAutomation.Core

. 'C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1'

With these two lines the script is now being executed correctly.

I don't know why all my other approaches didn't work but now it's fine - at least for this scenario.

Thanks LucD for your help in finding out why it occured in the first place.

The Start-Transcript Cmdlet came in quite handy and I will now use it more often, when needed.

BR

NC

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Great that the issue if solved.

Still curious why my Enable-PowerCLI didn't do the trick.

Would be interested in seeing how you implemented that in your script, if possible


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

Reply
0 Kudos
NelsonCandela
Enthusiast
Enthusiast
Jump to solution

Dear Luc,

sorry but I don't have access to the sources anymore (working at a customer's site). I'll provide you with the details when I'm back on site next week.

As a short description and to let you know:

I have included your raw script in the header part of my script, so basically before anything else. Directly after that I called the function to load the PowerCLI components, because one of the first things that happen is that the script tries to connect to the vCenter. And that already caused the issues.

When I'm back I can check the version information etc. and let you know the outcome.

Thanks again for your great hint with the 'run protocol', that indeed helped a lot.

BR

NC

Reply
0 Kudos