Automation

 View Only
  • 1.  Differentiating between identically named folders

    Posted May 06, 2010 02:32 PM

    Hi,

    I am trying to modify a script in this thread to suit my needs. The script was using the Datacenter as the input to automatically update the VMs. In our case, all of our VMs are under one Datacenter, and separated into Development and Production by folders. I was able to modify the script to use a folder as input. However, underneath the root dev and prod folders, we have identically named subfolders. For example:

    Datacenter

    Dev

    -->FolderA

    Prod

    -->FolderA

    I have attached the script I have so far.

    I would like to update all of the VMs in FolderA underneath Dev without updating any in FolderA underneath Prod. Would there be any way to provide input of either dev or prod, and then provide a secondary input for a subfolder?

    Thanks.



  • 2.  RE: Differentiating between identically named folders

    Posted May 06, 2010 03:25 PM

    You can use the -Location parameter on the Get-Folder cmdlet.

    You will have to pass a 2nd parameter indicating if you want the OPS or the DEV branch.

    The start of your script could look something like this

    param ($folderName=$(throw "Please specify the folder name where the VMs are located."),$branch)
    
    ##############################
    # Get collection of VMs, create log file
    ##############################
    
    if($branch -eq "DEV") {$loc = "dev} else {$loc = "prod"}
    $colVMs = Get-Folder $folderName -Location (Get-Folder $loc) | Get-VM
    $logFile = New-Item -ItemType File -Path "C:\tmp\VMware-Tools-Update_$((get-date).toString('MM-dd-yyyy_hh.mmtt')).log"
    ...
    

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 3.  RE: Differentiating between identically named folders
    Best Answer

    Posted May 06, 2010 03:26 PM

    Update the param line,

    param( $folderName = $(throw "Please specify the folder name where the VMs are located.") ,

    $folderName2 = $(throw "Please specify the folder name 2 where the VMs are located.") )

    Then modify the $colVMs = Get-Folder $folderName | Get-VM --> $colVMs = Get-Folder $folderName| Get-Folder $folderName2 | Get-VM

    -KjB

    Message was edited by: kjb007 : Fixed formatting



  • 4.  RE: Differentiating between identically named folders

    Posted May 06, 2010 03:56 PM

    Thanks for the input. Both of the code examples helped, and I implemented the one from kjb007.



  • 5.  RE: Differentiating between identically named folders

    Posted May 06, 2010 04:05 PM

    Fair enough, but be aware that the solution from kjb007 will only work if your folders don't go any deeper than 2 levels.

    ____________

    Blog: LucD notes

    Twitter: lucd22