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