VMware Cloud Community
cryton2707
Enthusiast
Enthusiast
Jump to solution

need help with new-folder powercli5

Ok Im trying to use the new-folder function to create a folder structure

somthing like

tier1

     team a

          project a

          project b

     team b

     team c

tier2

     team a

          project a

          project c

     team b

     team c

Dev

     team a

          project a

          project b

          project c

     team b

     team c

The problem is I create tier 1 and sub folders ok

On creating "tier 2" and "team a".  When going down to create "project a" under "tier 2/team a"  then I hit problems because the paramater -location just says "team a" (i.e the folder above) and folder "project a" already exists under "tier 1/team a"

So I get an error.. What I want to know is how can I specify the location context.

Thanks

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You use the hidden 'vm' folder.

Like this

$DC2="DataCenter1.MYVC.domain.com" 
connect-viserver
-Server $DC2
$vmFolder
= Get-Folder -Name vm -Location (Get-Datacenter -Name "DataCenter 2")
$t1 = New-Folder -Name "Tier 1" -Location $vmFolder -Server $DC2 -confirm:$false
$t1a
= New-Folder -Name "Team a" -Location $t1 -Server $DC2 -Confirm:$false
New-Folder
-Name "project a" -Location $t1a -Server $DC2 -Confirm:$false
New-Folder -Name "project b" -Location $t1a -Server $DC2 -Confirm:$false
New-Folder
-Name "Team b" -Location $t1 -Server $DC2 -Confirm:$false
New-Folder
-Name "Team c" -Location $t1 -Server $DC2 -Confirm:$false
$t2 = New-Folder -Name "Tier 2" -Location $vmFolder -Server $DC2 -Confirm:$false
$t2a
= New-Folder -Name "Team a" -Location $t2 -Server $DC2 -Confirm:$false
New-Folder -Name "project a" -Location $t2a -Server $DC2 -Confirm:$false
New-Folder -Name "project b" -Location $t2a -Server $DC2 -Confirm:$false


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

View solution in original post

Reply
0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

Could you perhaps include the script your are using ?


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

Reply
0 Kudos
cryton2707
Enthusiast
Enthusiast
Jump to solution

Example Code Im using..

$DC2="DataCenter1.MYVC.domain.com"

connect-viserver

-Server $DC2

New-Folder

-Name "Tier 1" -Location $DC2 -Server $DC2 -confirm:$false

New-Folder -

Name "Tier 1" -Location "Team a" -Server $DC2 -Confirm:$false

New-Folder

-Name "project a" -Location "Team a" -Server $DC2 -Confirm:$false

New-Folder

-Name "project b" -Location "Team a" -Server $DC2 -Confirm:$false

New-Folder -

Name "Team b" -Location "Tier 1" -Server $DC2 -Confirm:$false

New-Folder

-Name "Team c" -Location "Tier 1" -Server $DC2 -Confirm:$false

New-Folder -

Name "Tier 2" -Location $DC2 -Server $DC2 -Confirm:$false

New-Folder

-Name "Team a" -Location "Tier 2" -Server $DC2 -Confirm:$false

New-Folder

-Name "project a" -Location "Team a" -Server $DC2 -Confirm:$false

New-Folder -Name "project b" -Location "Team a" -Server $DC2 -Confirm:$false

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

When you reach Project a under Tier 2, the name Project a is not unique anymore.

A Get-Folder -Name "Project a" will return 2 folders.

The solution is to store the parent folders in a variable where needed.

Something like this for example

$DC2="DataCenter1.MYVC.domain.com" 
connect-viserver
-Server $DC2
$t1
= New-Folder -Name "Tier 1" -Location $DC2 -Server $DC2 -confirm:$false
$t1a = New-Folder -Name "Team a" -Location $t1 -Server $DC2 -Confirm:$false
New-Folder
-Name "project a" -Location $t1a -Server $DC2 -Confirm:$false
New-Folder -Name "project b" -Location $t1a -Server $DC2 -Confirm:$false
New-Folder -Name "Team b" -Location $t1 -Server $DC2 -Confirm:$false
New-Folder -Name "Team c" -Location $t1 -Server $DC2 -Confirm:$false
$t2
= New-Folder -Name "Tier 2" -Location $DC2 -Server $DC2 -Confirm:$false
$t2a = New-Folder -Name "Team a" -Location $t2 -Server $DC2 -Confirm:$false
New-Folder -Name "project a" -Location $t2a -Server $DC2 -Confirm:$false
New-Folder -Name "project b" -Location $t2a -Server $DC2 -Confirm:$false

The New-Folder cmdlet returns the unique folder object you created.


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

cryton2707
Enthusiast
Enthusiast
Jump to solution

Hi that worked great in that the folder structure is now creating..

however.. Its creating yellow folders under (Hosts adn Clusters) rather than the Blue Folders under VM's and Templates.

Im presuming its somthing to do with setting a top level before with somthing like

$t1top = Get-Datacenter "DataCenter 2"
$t1 = new-Folder "Tier 3" -Location $t1top -Server $DC2 -confirm:$false

But the DataCeter 2 name shows the same under inventory and vms and templates.. So how do I distinguish were to create the folders.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You use the hidden 'vm' folder.

Like this

$DC2="DataCenter1.MYVC.domain.com" 
connect-viserver
-Server $DC2
$vmFolder
= Get-Folder -Name vm -Location (Get-Datacenter -Name "DataCenter 2")
$t1 = New-Folder -Name "Tier 1" -Location $vmFolder -Server $DC2 -confirm:$false
$t1a
= New-Folder -Name "Team a" -Location $t1 -Server $DC2 -Confirm:$false
New-Folder
-Name "project a" -Location $t1a -Server $DC2 -Confirm:$false
New-Folder -Name "project b" -Location $t1a -Server $DC2 -Confirm:$false
New-Folder
-Name "Team b" -Location $t1 -Server $DC2 -Confirm:$false
New-Folder
-Name "Team c" -Location $t1 -Server $DC2 -Confirm:$false
$t2 = New-Folder -Name "Tier 2" -Location $vmFolder -Server $DC2 -Confirm:$false
$t2a
= New-Folder -Name "Team a" -Location $t2 -Server $DC2 -Confirm:$false
New-Folder -Name "project a" -Location $t2a -Server $DC2 -Confirm:$false
New-Folder -Name "project b" -Location $t2a -Server $DC2 -Confirm:$false


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

Reply
0 Kudos
cryton2707
Enthusiast
Enthusiast
Jump to solution

Thanks for your help :smileycool:

Reply
0 Kudos