VMware Cloud Community
parsley
Contributor
Contributor
Jump to solution

Differentiating between identically named folders

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.

0 Kudos
1 Solution

Accepted Solutions
kjb007
Immortal
Immortal
Jump to solution

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

vExpert/VCP/VCAP vmwise.com / @vmwise -KjB

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

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


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

kjb007
Immortal
Immortal
Jump to solution

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

vExpert/VCP/VCAP vmwise.com / @vmwise -KjB
0 Kudos
parsley
Contributor
Contributor
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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