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...
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
ok well ill post the whole script then. any sensitive info will be replaced with a string of *
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
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...
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