Automation

 View Only
  • 1.  LucD Get-FolderPath issue

    Posted Aug 18, 2022 02:47 PM

    I am having issue with   function. not sure what I ma missing but I am getting below 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.
    At line:91 char:37
    + $fld = Get-View $fld.Parent
    + ~~~~~~~~~~~
    + CategoryInfo : InvalidData: (:) [Get-View], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetIn
    terop.GetVIView

     

     

     

    here is  what I am running


    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.

    .NOTES

    Authors: Luc Dekens

    .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 | %{

    $fld = $_.Extensiondata

    $fldType = "yellow"

    if($fld.ChildType -contains "VirtualMachine"){

    $fldType = "blue"

    }

    $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

    $row.Name = $_.Name

    $row.Path = $path

    $row.Type = $fldType

    $row

    }

    }

    }

    ## Export all folders

    $report = @()

    $report = Get-folder -type VM | where{$_.Name -ne 'vm'} | Get-Folderpath

    $report | Export-Csv '.\folders.csv' -NoTypeInformation -UseCulture



  • 2.  RE: LucD Get-FolderPath issue

    Posted Aug 18, 2022 02:56 PM

    I think I figured tghis out. I had to disconnect the  current vcenter connecion

     

    disconnect-viserver * -confirm:$false



  • 3.  RE: LucD Get-FolderPath issue

    Posted Aug 18, 2022 03:55 PM

    That sounds strange.
    Without a connection to the vCenter the script should produce errors



  • 4.  RE: LucD Get-FolderPath issue

    Posted Aug 18, 2022 06:23 PM

    right, I was connect to my destination  test vcenter which didn't have any object



  • 5.  RE: LucD Get-FolderPath issue

    Posted Aug 18, 2022 06:56 PM

    Ok, that explains