VMware Cloud Community
Leo7594
Contributor
Contributor
Jump to solution

RVTools report automation - script works manually but not through task scheduler

Hello everyone,

Not sure if I’m in the right forum but it cost nothing to ask.

I’m scratching my head since last Friday on this issue so I’m coming to ask for help, maybe I’m missing something.

We have a scheduled task that runs every day that generates RVTools report from all of our vcenter infrastructure, from a powershell script that uses RVTools command-line.

We were updating our VCSA last week and we stopped receiving our daily report for the upgraded vcenter.

We had to finish the upgrade so now, we don’t receive any reports at all.

I’ve worked a bit on the script since the last one had a few issues and it appears to be working, when launched directly from powershell but as soon as I put it in a scheduled task, the reporting part won’t execute normally (the archiving part does though).

When I launch the task, I can see that RVTools starts but it almost instantly stops.

The task is, for now, configured with a service account with administrative rights, to run whether the user is logged on or not and with the highest privileges and the action configured is to start a program, with “powershell” as program and “–File ‘path to script’ 1> ‘path to logs folder 2>&1”

If I’ve no old reports in the directory, I get some errors saying that the file can’t be found but it should not be an issue.

I’ve no errors on the reports part.

I’ve hid everything I had to, but here is the script I’m working with so if anyone has any leads, I’m all ears.

Thanks in advance

 

 

Function Archive{
    Param(
		[PARAMETER(Mandatory=$True, HelpMessage='Path')]
		[string]$varPath,
		[PARAMETER(Mandatory=$True, HelpMessage='Extension')]
		[string]$Extension
    )
	
	BEGIN
	{
		$Path = $varPath

		$ErrorMsg = "Files moved !"
		$Filter = $Extension
		$date = Get-Date -Format yyyyMMdd
		if(!(Test-Path -Path "$Path\Archives" -ErrorAction SilentlyContinue )){ New-Item -Path $Path -Name 'Archives' -ItemType Directory -ErrorAction SilentlyContinue }
	}
	
	PROCESS
	{
		$list = Get-ChildItem -Path $Path -Filter $Filter | where { ! $_.PSIsContainer }
		if($list -Ne $Null){
			ForEach( $var in $list ){
				Rename-Item -Path $var.FullName -NewName $Date"_"$var

				# Copy on NAS
				$share = '\\xxx\Reports\RVTools\'
				Copy-Item -Path "$Path\$(Get-Date -Format yyyyMMdd)_$var" -Destination $share -Force

				Move-Item -Path "$Path\$(Get-Date -Format yyyyMMdd)_$var" -Destination "$Path\Archives" -Force
			}
		}
	}
	
	END
	{
		if($? -Eq $True){ Write-Host $ErrorMsg -BackGroundColor 'red'; }
	}
}

$vcenters = 'xxx','xxx','xxx','xxx','xxx'
$RVTools = "C:\Program Files (x86)\Robware\RVTools\RVTools.exe"
$Path = "E:\Reports\RVTools"

# Move previous files
Archive $Path '*.xlsx'

ForEach( $vcenter in $vcenters ){

    $Arguments = "-u xxx\xxx -p xxx -s $vCenter -c ExportAll2xlsx -d $Path -f $vCenter"
    $Process = Start-Process -FilePath "C:\Program Files (x86)\Robware\RVTools\RVTools.exe" -ArgumentList $Arguments -Wait

}

While(Get-Process -Name 'RVTools' -ErrorAction SilentlyContinue){ Start-Sleep -Seconds '5' }

Get-ChildItem -Path $Path | where { ! $_.PSIsContainer } | ForEach-Object{
	$File = $_.Name.Split('.')[0]
	Send-MailMessage -From 'xxx' -To 'xxx' -Subject "RVTools : $File" -Body "Daily RVTools Export in attachment" -Attachments $_.FullName -SmtpServer 'xxx'

	# Copy on NAS
	$share = '\\xxx\Reports\RVTools\'
	Copy-Item -Path $_.FullName -Destination $share
}


# Merge variables
$MergedFile = "E:\Reports\RVTools\all_vcenters.xlsx"
$Merge_exe = "C:\Program Files (x86)\Robware\RVTools\RVToolsMergeExcelFiles.exe"

$var = ''
foreach( $i in $vcenters ){ $var = $var + (Get-Item -Path E:\Reports\RVTools\$i.xlsx).fullname }
$var = $var.Replace('xlsx','xlsx;')

# Remove the last semi colon
# $Target = $Target -replace ".$"
$var = $var -Replace ".$"

# Merge the reports
& $Merge_exe -input $var -output $MergedFile -overwrite -verbose
# Invoke-Expression -Command "D:\RVTools\RVToolsMergeExcelFiles.exe -input $target -output $MergedFile -overwrite -verbose"

#SMTP Server Credentials
$username = 'xxx'
$password = 'xxx'
$secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ($username, $secpasswd)

# Send the merge file
Send-MailMessage -From 'xxx' -To 'xxx' -Subject "Merged RVTools Report" -Body "Daily RVTools Export in attachment" -SmtpServer 'xxx'  -usessl -Credential $mycreds -Attachments $MergedFile 

# Copy on NAS
$share = 'xxx'
Copy-Item -Path $MergedFile -Destination $share

Disconnect-viserver * -Confirm:$False

 

Reply
0 Kudos
1 Solution

Accepted Solutions
Leo7594
Contributor
Contributor
Jump to solution

Hello everyone, LucD,

I manage to resolve my issue thanks to Rob, the creator of RVTools.

So if anyone does have a similar issue, here's what did the trick for me.

Just after my 3 variables ($vcenters, $RVTools & $Path), I added those 3 commands :

# Save current directory
$SaveCurrentDir = (get-location).Path

# Set RVTools path
[string] $RVToolsPath = "C:\Program Files (x86)\Robware\RVTools"

# cd to RVTools directory
set-location $RVToolsPath

This fixed my loop and allowed my task to generate reports without errors.

I also added this command at the end of the script :

# Back to starting dir
Set-Location $SaveCurrentDir

Thanks for your help LucD !

 

View solution in original post

Reply
0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

How do you schedule the task, with a Windows Task Scheduler?
Can you add a Start-Transcript to the script?
That might provide some pointers.


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

Reply
0 Kudos
Leo7594
Contributor
Contributor
Jump to solution

Hello,

Thank you for your answer.

Yes I schedule it with a Windows Task Scheduler.

I added the Start-Transcript and ran the task.
I'll join it to the post so you can check for yourself but I don't see anything wrong, the errors are there because since the files are not being generated, it cannot find the path of the different files, but the "old" file already present, did get moved to the Archive path where they should at the beginning of the script.

I did delete a few informations that didn't need to be shared but the transcript is almost as is.

Many thanks in advance for your help.

Kind regards

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

There is also an error stating that the vCenter folder can not be found.
Can you try by pre-creating those folders before the script runs, and check if that makes a difference?


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

Reply
0 Kudos
Leo7594
Contributor
Contributor
Jump to solution

Sorry I deleted a bit more information than needed, the first error is also an error because a file is missing.
Here is the line with the extension still there :

"Get-Item : Cannot find path 'E:\Reports\RVTools\vcenter.....xlsx' because it does not exist."

To add more details, I ran the script from PWS_ISE with my account where it is working, I'm joining it to this answer as well.

Many thanks for your assistance.

Reply
0 Kudos
Leo7594
Contributor
Contributor
Jump to solution

Hello everyone, LucD,

I manage to resolve my issue thanks to Rob, the creator of RVTools.

So if anyone does have a similar issue, here's what did the trick for me.

Just after my 3 variables ($vcenters, $RVTools & $Path), I added those 3 commands :

# Save current directory
$SaveCurrentDir = (get-location).Path

# Set RVTools path
[string] $RVToolsPath = "C:\Program Files (x86)\Robware\RVTools"

# cd to RVTools directory
set-location $RVToolsPath

This fixed my loop and allowed my task to generate reports without errors.

I also added this command at the end of the script :

# Back to starting dir
Set-Location $SaveCurrentDir

Thanks for your help LucD !

 

Reply
0 Kudos