AsBuiltReport / AsBuiltReport.VMware.vSphere

Repository for AsBuiltReport VMware vSphere module
https://www.asbuiltreport.com
MIT License
112 stars 39 forks source link

New-AsBuiltReport : Exception calling "IsNullOrEmpty" with "1" argument(s): "Server vsphere.local\administrator@vcenter.domain.com:443 is not connected." #77

Closed mattallford closed 4 years ago

mattallford commented 4 years ago

Describe the bug When running the vSphere As Built Report against some environments (not all), the script seems to run OK, but right at the end where the document should be generated, the script exits with the following error:

At line:14 char:1
+ New-AsBuiltReport @props
+ ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,New-AsBuiltReport

I have narrowed this down and it only occurs if the vCenter Server level is set to 3 or higher, and the specific section of code that is causing the problem is related to vSphere Tag Assignments:

$TagAssignments = Get-TagAssignment -Server $vCenter
if ($TagAssignments) {
    Section -Style Heading3 'Tag Assignments' {
        $TagAssignments | Sort-Object Tag, Entity | Table -Name 'Tag Assignments' -Columns Tag, Entity -ColumnWidths 50, 50
    }
}
#endregion vCenter Server Tag Assignments

If the above section of code is removed or commented, the script will run with no issues at all.

In the environment I am running this against currently, there are two vCenter Servers running the same build (detailed below), but the script runs fine against one vCenter Server yet not the other.

In the problematic vSphere environment there are 138 tag assignments, in the environment that works OK there are 682 tag assignments.

To Reproduce It doesn't seem to be clear on how to reproduce this issue at the moment. As described above I am running this in en environment that has two vCenter servers that, for the most part, seem to be based on the same build and are both using tag assignments, but the one with more tag assignments is OK and the one with fewer tag assignments is not ok.

Regardless, the steps to reproduce the behavior are:

  1. Ensure the Infolevel for vCenter is 3 or higher in the JSON being used for the vSphere Report
  2. Run New-AsBuiltReport specifying the VMware.vSphere Report type and the JSON containing a vCenter Infolevel of 3 or higher
  3. Observe an error when PScribo attempts to export the information to the desired document

The exact command being run (vCenter name obfuscated) is:

$props = @{
    Report = "VMware.vSphere"
    Target = "vcenter.domain.com"
    Username = 'administrator@vsphere.local'
    Password = 'SuperAwesomePassword'
    Format = "html","word"
    OutputPath = 'E:\vSphere 6.7 U3 Upgrade\Asbuilt'
    StylePath = 'E:\vSphere 6.7 U3 Upgrade\Asbuilt\Parallo.ps1'
    Timestamp = $true
    ReportConfigPath = 'E:\vSphere 6.7 U3 Upgrade\Asbuilt\AsBuiltReport.VMware.vSphere.json'
    AsBuiltConfigPath = 'E:\vSphere 6.7 U3 Upgrade\Asbuilt\AsBuiltReport.json'
}
New-AsBuiltReport @props

Expected behavior The document should be produced with no errors.

Screenshots If applicable, add screenshots to help explain your problem.

System (please provide the following information about the system from which you are trying to generate a report):

Additional context If you dig in to some of the debugging in VS Code, the specific PScribo command it is failing on is Get-HtmlTable:

GetHtmlTable : Exception calling "IsNullOrEmpty" with "1" argument(s): "Server vsphere.local\administrator@vcenter.domain.com:443 is not connected."
At line:710 char:57
+ ...     [ref] $null = $tableBuilder.Append((GetHtmlTable -Table $Table));
+                                             ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [GetHtmlTable], MethodInvocationException
    + FullyQualifiedErrorId : ViServerConnectionException,GetHtmlTable

image

The report runs ok if you output to text instead of word or HTML.

mattallford commented 4 years ago

If I run New-AsBuiltReport and export to text, as noted above it runs OK. In the Assigned Tag output, I can see there is a tag but the entity is empty for some reason: image

However if I simply run Get-TagAssignment -Server vcenter.domain.com at the command line, I get the entity in the results: image

If I browse to that VM entity in the web client, under Tags I can see the tag is assigned.

Also if I browse to the Tags & custom attributes section and select the tag, I can see that is is assigned to one object from the vCenter Server I am running As Built against. Interestingly enough the same tag is assigned to objects from another (linked) vCenter Server as well. Not sure if that's an issue, it shouldn't be by the use of the -Server parameter on Get-TagAssignment, just an observation at the moment.

mattallford commented 4 years ago

Man, this is getting weird.

I just used VC Code to put a breakpoint on the cmdlet Get-TagAssignment and then ran New-AsbuiltReport.

It broke on this line, which is what I expected:

$TagAssignments = Get-TagAssignment -Server $vCenter

So I then ran just that line, and checked the output of $TagAssignments. All good and the entity is shown here against the tag that I thought was going to be empty as per above: image

So then I let the rest of the script run, and it completed without issue and generated the HTML report!

If I remove the breakpoint and run exactly the same thing again from the current session, the report works:

VERBOSE: [ 13:51:19:220 ] [ Document ] - Processing section 'VMware Update Manager' completed.
VERBOSE: [ 13:51:19:223 ] [ Document ] - Processing section 'vcenter01.domain.com' completed.
VERBOSE: [ 13:51:19:240 ] [ Document ] - Document 'VMware vSphere As Built Report - 2020-03-12_13.50.33' processing completed.
VERBOSE: [ 13:51:19:242 ] [ Document ] - Total processing time '46.22' seconds.
VMware vSphere As Built Report - 2020-03-12_13.50.33 has been saved to E:\vSphere 6.7 U3 Upgrade\Asbuilt

I actually ran it 4 times to make sure and it worked every time.

I then killed the Powershell terminal in VS Code and started a new one and ran exactly the same code again, and now we are back to it failing as per the original post.