PowerCLI

 View Only
  • 1.  Check if folder exists Get-Folder

    Posted Mar 21, 2011 11:24 PM

    Hi there,

    I am new to powershell but I can see how it can make life a lot easier.

    I am trying to create a script that loops through each datacenter and exists whether a folder exists.
    I haven't got very far as I am stuck early on :-)
    I manage to get each folder fine and I stick the values in the $CurrentFolders array.
    The array output looks as follows:

    Name : Development
    Name : UAT

    I would like to check that $CurrentFolders contains a Production folder.
    I have tried several ways but cannot extract values from my array.

    /* Part of script ommited*/

    #Loop through all datacenters and create top folders if not exist
    ForEach ($DC in $DCs)
        {
            #Check whether top folders exists
            write-host "hello $DC"
            $CurrentFolders = @()
            ForEach ($Folder in (Get-Folder -Location $DC | Select Name | Where-Object { "datacenters","datastore","host","vm","Templates" -notcontains $_.Name } ))
                    {
                     $CurrentFolders += $Folder
                    }

             //Check for Production folder here
             $Currentfolders
             }

    Any help appreciated :-)



  • 2.  RE: Check if folder exists Get-Folder

    Posted Mar 22, 2011 06:13 AM

    The Select cmdlet doesn't produce an array of strings.

    If you have an array of strings, you can use the -contains operator to find the "Production" folder.

    #Loop through all datacenters and create top folders if not exist
    $DCs = Get-Datacenter
    foreach
    ($DC in $DCs) { #Check whether top folders exists
       
    write-host "hello $DC"
       
    $CurrentFolders = @()     foreach ($Folder in (Get-Folder -Location $DC | Where-Object { "datacenters","datastore","host","vm","Templates" -notcontains $_.Name} | %{$_.Name} ))     {         $CurrentFolders += $Folder
        }     if($Currentfolders -contains "Production"){         Write-Host "Production folder present in" $DC
        } }


  • 3.  RE: Check if folder exists Get-Folder

    Posted Mar 22, 2011 07:16 PM

    Thanks Luc,

    I will give that a try.

    Is it possible to select a subfolder for location directly (without another loop statement)?
    Something like Get-Folder -Location $DC.foldername?

    You mention the select command does not produce an array of strings.
    As I am about to order your book, does it cover information like that (the select statement info)

    or would I need to look at a more general powershell book for that kind of info?

    Cheers



  • 4.  RE: Check if folder exists Get-Folder

    Posted Mar 22, 2011 07:37 PM

    No, I'm afraid the Get-Folder cmdlet doesn't allow that.

    The Location parameter needs to be a container object.

    But you can of course nest this, something like this

    Get-Folder -Location (Get-Folder -Location $dc -Name foldername)

    In the book, thanks for ordering it btw, we concentrated on showing how to automate all types of vSphere tasks.

    And I'm afraid we already hit the 750+ page limit.

    In fact we had 2 appendices, one which gives a short PowerShell intro and another one that contains a bunch of useful links, that we had to leave out due to the page limit. These two appendices will be published on the PowerCLI book website shortly.

    One of the links in the appendix is the excellent free eBook that Tobias Weltner wrote on PowerShell.

    Have a look at that one for learning PowerShell in depth.