VMware Cloud Community
Bragisimison
Contributor
Contributor

Get-View : Cannot validate argument on parameter

Hey all, 

So I have been given the task of finishing off a project of someone that is no longer with us. The basic gist is that we are collecting a list of all our servers, hardware or VM and the previous guy has a PShell script to do all that data collection for us and we will transform it later. I started testing the script and I'm funning into an issue with this part.... 

 

Begin{
#Functions (load the required functions)
function Get-FolderPath{
<#
.SYNOPSIS
Returns the folderpath for a folder
.DESCRIPTION
The function will return the complete folderpath for
a given folder, optionally with the "hidden" folders
included. The function also indicats if it is a "blue"
or "yellow" folder.
.PARAMETER Folder
On or more folders
.PARAMETER ShowHidden
Switch to specify if "hidden" folders should be included
in the returned path. The default is $false.
.EXAMPLE
PS> Get-FolderPath -Folder (Get-Folder -Name "MyFolder")
.EXAMPLE
PS> Get-Folder | Get-FolderPath -ShowHidden:$true
#>

param(
[parameter(valuefrompipeline = $true,
position = 0,
HelpMessage = "Enter a folder")]
[VMware.VimAutomation.ViCore.Impl.V1.Inventory.FolderImpl[]]$Folder,
[switch]$ShowHidden = $false
)

begin{
$excludedNames = "Datacenters","vm","host"
}

process{
$Folder | %{
$folderid = $_.Id
$fld = $_.Extensiondata
$fldType = $_.Type
$path = $fld.Name
while($fld.Parent){
$fld = Get-View $fld.Parent
if((!$ShowHidden -and $excludedNames -notcontains $fld.Name) -or $ShowHidden){
$path = $fld.Name + "\" + $path
}
}
$row = "" | Select Name,Path,Type,FolderId
$row.Name = $_.Name
$row.Path = $path
$row.Type = $fldType
$row.FolderId = $folderid
$row
}
}
}

#Populate the Vcenters array with the FQDNs of all vCenters.
$Vcenters = @(`

 

the rest of the script runs, I connect and such but then it returns this error. 

Get-View : Cannot validate argument on parameter 'VIObject'. The argument is null, empty, or an element of the argument
collection contains a null value. Supply a collection that does not contain any null values and then try the command again.

I do have older data files that were generated, so I'm trying to understand here what's up. I freely admit I'm very freshmen level in debugging Pshell scripts so yes I am an idiot. I need some help here... 

 

0 Kudos
5 Replies
LucD
Leadership
Leadership

The Get-View error seems to indicate that the parameter is $null.
Since $fld is derived from the parameter $Folder that is passed to the function, it looks as if $Folder was already $null.

Long story short, without showing how the Get-FolderPath function is called and without knowing what parameter was passed, it's impossible to determine where the error comes from.


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

0 Kudos
Bragisimison
Contributor
Contributor

ok well ill post the whole script then. any sensitive info will be replaced with a string of *

<#
.SYNOPSIS
    This script will query different data sources and provide applicable metadata for VMs 
 
.DESCRIPTION
    This script queries VMWare, Active Directory, & BMC Discovery to join applicable metadata from each dataset to provide a unified view of the systems
 
.PARAMETER Location
    The parameter location is used to refine the location of where geographicaly applicable system metadata is queried from
    e.g. AK
 
.EXAMPLE
    The example below will query all defined vCenter instances and join applicable metadata for each VM from AD & Discovery
    PS C:\> Get-VmInventory 
 
.EXAMPLE
    The example below will query the AK vCenter instance and join applicable metadata for each VM from AD & Discovery
    PS C:\> Get-VmInventory -location AK
 
.NOTES
    *******************************
    
    Script: Vmware_Get-VmInventory
    Current Version: 1.0
    Published: 23 Oct 2023
    Revision Date: 23 Oct 2023
    
    Change Log:
    v1.0 - Script creation
 
Module Dependencies:
Join-Object
PowerCLI
#>
Begin{
#Functions (load the required functions)
function Get-FolderPath{
<#
.SYNOPSIS
Returns the folderpath for a folder
.DESCRIPTION
The function will return the complete folderpath for
a given folder, optionally with the "hidden" folders
included. The function also indicats if it is a "blue"
or "yellow" folder.
.PARAMETER Folder
On or more folders
.PARAMETER ShowHidden
Switch to specify if "hidden" folders should be included
in the returned path. The default is $false.
.EXAMPLE
PS> Get-FolderPath -Folder (Get-Folder -Name "MyFolder")
.EXAMPLE
PS> Get-Folder | Get-FolderPath -ShowHidden:$true
#>
 
param(
[parameter(valuefrompipeline = $true,
position = 0,
HelpMessage = "Enter a folder")]
[VMware.VimAutomation.ViCore.Impl.V1.Inventory.FolderImpl[]]$Folder,
[switch]$ShowHidden = $false
)
 
begin{
$excludedNames = "Datacenters","vm","host"
}
 
process{
$Folder | %{
$folderid = $_.Id
$fld = $_.Extensiondata
$fldType = $_.Type
$path = $fld.Name
while($fld.Parent){
$fld = Get-View $fld.Parent
if((!$ShowHidden -and $excludedNames -notcontains $fld.Name) -or $ShowHidden){
$path = $fld.Name + "\" + $path
}
}
$row = "" | Select Name,Path,Type,FolderId
$row.Name = $_.Name
$row.Path = $path
$row.Type = $fldType
$row.FolderId = $folderid 
$row
}
}
 
#Populate the Vcenters array with the FQDNs of all vCenters. 
$Vcenters = @(`
*******************************
 
#define BMC Discovery API endpoint & auth info
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
#exact query context can be generated directly within the UI 
$apiURL = "**************************************************"
$apiHeaders = @{"Authorization" = "Bearer $apikey"}
$apiKey = ****************************************************
$apiQuery = "search Host show name, #InferredElement:Inference:Associate:DiscoveryAccess.endpoint as 'Scannedvia', #Host:HostedSoftware:AggregateSoftware:BusinessApplicationInstance.name as 'BusinessApp'"
$apiParams = @{"query" = $apiQuery; "format" = "object"; "limit" = 0}
}
process{
#Query BMC Discovery via Rest API
$BmcDiscoResponse = Invoke-RestMethod -uri $apiurl -Headers $apiHeaders -method Get -Body $apiparams
 
$adhosts = get-adcomputer -filter * -properties name,enabled,description,CanonicalName,operatingSystem,lastlogondate,managedby | select @{Name='name';Expression={$_.name.ToLower()}},enabled,description,CanonicalName,operatingSystem,lastlogondate,managedby
#$adhosts = get-adcomputer -filter {(operatingsystem -notlike "Windows 10*") -and (operatingsystem -notlike "Windows 7*") -and (operatingsystem -notlike "Windows 8*")} -properties name,enabled,description,CanonicalName,operatingSystem,lastlogondate,managedby
 
# Loop thru all vCenters
Foreach ($vcenter in $Vcenters) {
connect-viserver $vcenter
 
#get a list of all vm folders, fullpath, & associated IDs
$VMFolders= get-folder | get-folderPath
#get a list of all vms & associated containing folders
$vms = get-vm | select  @{Name='name';Expression={$_.name.ToLower()}}, powerstate,folderid
 
#Join VMs output with VMFolders output to get full folder path of VMs
$VMJoinObjParams = @{
Left              = $vms
Right             = $VMfolders
LeftJoinProperty  = 'FolderId'
RightJoinProperty = 'FolderId'
LeftProperties    = 'Name', 'Powerstate'
RightProperties   = 'Path'
Type              = 'AllInLeft'
}
$VmsWFolders = Join-Object @VMJoinObjParams
 
#Join VMsWFolders output with Active Directory data
$ADJoinObjParams = @{
Left              = $VmsWFolders
Right             = $ADhosts
LeftJoinProperty  = 'Name'
RightJoinProperty = 'Name'.ToLower()
LeftProperties    = 'Name','Powerstate','Path'
RightProperties   = 'Description','Operatingsystem','managedby','CanonicalName'
Type              = 'AllInLeft'
Prefix            = 'AD_'
}
$VmsWAd = Join-Object @ADJoinObjParams
 
#Join VMsWFolders output with Active Directory data
$BmcJoinObjParams = @{
Left              = $VmsWAd
Right             = $BmcDiscoResponse.results
LeftJoinProperty  = 'Name'
RightJoinProperty = 'Name'
LeftProperties    = 'Name','Powerstate','Path','AD_Description','AD_Operatingsystem','AD_managedby','AD_CanonicalName'
RightProperties   = 'Name','ScannedVia','BusinessApp'
Type              = 'OnlyIfInBoth'
Prefix            = 'BMC_'
}
$VmsWAdWBmc = Join-Object @BmcJoinObjParams
 
#Output to CSV
$VmsWAdWBmc | Select name, powerstate, path, AD_Description, AD_Operatingsystem, AD_managedby, AD_CanonicalName, BMC_ScannedVia, @{Name='BusinessApp';Expression={$_.BMC_BusinessApp}}| Export-Csv -path c:\tmp\VMInventory_$(Get-Date -Format "MM-dd-yyyy").csv -Append -NoTypeInformation
}
}
end {
# Disconnect from each vCenter
Foreach ($vcenter in $Vcenters) {
Disconnect-viserver $vcenter -Confirm:$false
}
}

 

0 Kudos
LucD
Leadership
Leadership

You will have to check if Get-Folder returns any Folder objects.
Just place a breakpoint on the following line

$VMFolders = Get-Folder | Get-FolderPath

When the breakpoint is reached, perform a Get-Folder from the terminal prompt.

You can also add some debugging lines (Write-Host) to the function.
That way you should be able to pinpoint which folder causes the issue.

        process {
            $Folder | % {
                Write-Host "Folder: $($_.Name)"
                $folderid = $_.Id
                $fld = $_.Extensiondata
                $fldType = $_.Type
                $path = $fld.Name
                Write-Host "`tParent: $($fld.Name)"
                while ($fld.Parent) {
                    $fld = Get-View $fld.Parent
                    Write-Host "`tParent: $($fld.Name)"
                    if ((!$ShowHidden -and $excludedNames -notcontains $fld.Name) -or $ShowHidden) {
                        $path = $fld.Name + "\" + $path
                    }
                }
                $row = "" | select Name, Path, Type, FolderId
                $row.Name = $_.Name
                $row.Path = $path
                $row.Type = $fldType
                $row.FolderId = $folderid 
                $row
            }
        }


It could be that the Connect-VIServer cmdlet fails.


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

0 Kudos
Bragisimison
Contributor
Contributor

I'm still digging but doing as suggested with break point. I'm getting this as the response when i get the get-folder command. 

Name Type
---- ----
Datacenters Datacenter
vm VM
network Network
host HostAndCluster
datastore Datastore
VRM VM
Discovered virtual machine VM
OC VM
Templates VM
OC Network
OC HostAndCluster
OC Datastore
Steelfusion-Staging VM
DT VM
EL VM
OP VM
Steelfusion VM
OC VM
OP Network
OP HostAndCluster
OP Datastore
Steelfusion Datastore
Windows_Server VM
Desktop VM
Unix_Servers VM
Windows_Servers VM
Desktop VM
Unix_Servers VM
Windows_Servers VM
Desktop VM
Linux VM
Windows VM
Steelfusion HostAndCluster
HostsOnHold HostAndCluster
Operations VM
Operations VM
VMWare VM
Steelfusion VM
VMWare VM

I suck at the formatting tools but, I don't see anything Null there... 

0 Kudos
LucD
Leadership
Leadership

That's why I suggested adding those Write-Host lines, that way you should be able to see from which Folder the Get-View error comes.


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

0 Kudos