PowerCLI

 View Only
Expand all | Collapse all

need help with scripting export of datacenter and clusters to import into brand new vcenter

  • 1.  need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Sep 30, 2022 03:39 PM

    I've successfully created scripts on exporting files/folder, etc. and importing, but I'm having a hard time figuring out how to script the hosts and clusters view.  I have a ROBO vcenter with tons of datacenters and clusters and obviously don't want to create by hand. I don't need all the cluster settings, I simply want the datacenter and clusters "shell".  Any powercli script help for exporting all the datacenters and clusters and then re-importing to populate new vcenter would be greatly appreciated. I went through these boards but no one really seems to be doing exactly this.



  • 2.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter



  • 3.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Sep 30, 2022 04:48 PM

    I've looked through these and most are out of date. I've tried to reverse engineer but not much luck. FYI, I don't have any resource pools so I really only need the datacenters and clusters. I got separate scripts to export the folders, roles, etc. So I want something independent.



  • 4.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Sep 30, 2022 05:07 PM

    Datacenters shouldn't be a big deal, they are rather flat objects and easy to create.
    Just export the name and the path.

    For clusters you will have to decide what exactly you want to export/import.

    Btw, I'm not sure why you categorise those scripts are oudated.
    A Datacenter and a Cluster are still the same.
    A Cluster might have new features, but a basic cluster is still the same.



  • 5.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Sep 30, 2022 05:53 PM

    So I figured out how to export the datacenter list to .csv which basically looks like:

    "Name"
    "test.dc.lab"
    "prod.dc.general"
    "location5.dc.general"

    I'm having problem importing so it will look at each line and create a DC from that line. 

    ##IMPORT DATACENTER
    $datacenter = Import-Csv "c:\temp\datacenters.csv"

    foreach($line in $datacenter){
    New-Datacenter -location (Get-Folder -NoRecursion)
    }



  • 6.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Sep 30, 2022 06:35 PM

    If some of your Datacenters are not located in the root of the inventory, you will also have to export the Path.

    If all Datacenters are in the root of the inventory you can just do

    Import-Csv "c:\temp\datacenters.csv" -PipelineVariable row |
    Foreach-Object -Process {
       New-Datacenter -Name $row.Name -Confirm:$false
    }
    


  • 7.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Sep 30, 2022 06:43 PM

    Thanks  I'll try that. Luckily all the DCs are in the root. However you got me thinking about the cluster portion. I will have to get the path since each cluster resides under its respective DC. Do you think its easier to write a separate cluster export/import script or try to incorporate it into the same script with the DC export/import?  I'm thinking separate right; because then I have the DC's created in the new vcenter and it has a path to look at it?



  • 8.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Sep 30, 2022 06:49 PM

    I would definitely go for separate scripts, just make sure to keep the order in mind, i.e. Datacenters before Clusters.

    You might want to have a look at my Get-InventoryPlus function (Get-InventoryPlus – Inventory of all vSphere objects), it will give you the path of all inventory objects.
     



  • 9.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Sep 30, 2022 07:05 PM

    its yelling at  me that I don't have the mandatory -location on New-Datacenter. if each one is created at the root what do I put for that?



  • 10.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Sep 30, 2022 07:25 PM

    The hidden folder Datacenters



    New-Datacenter -Name $row.Name -Location (Get-Folder -Name Datacenters)





  • 11.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Sep 30, 2022 10:31 PM

     Okay, I figured out how to export the clusters list in the format I wanted. but i need some assistance on the cluster creation part where I import the .csv.

    The format of my output.csv looks like this, but I'm not sure how to loop it thru the file so it creates the cluster under that datacenter path (also not sure why my path has 'host' in it; wondering if that is a hidden folder too)

    "Name","Path"
    "robo.cl.1","robo.dc.1\host\robo.cl.1"
    "robo.cl.2","robo.dc.2\host\robo.cl.2"
    "robo.cl.3","robo.dc.3\host\robo.cl.3"
    "robo.cl.4","robo.dc.4\host\robo.cl.4"
    "robo.cl.4","robo.dc.5\host\robo.cl.5"

     

     



  • 12.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 01, 2022 03:48 AM

    Yes, that 'host' is another hidden folder.

    With that CSV (I assume it is named cluster-export.csv) you could do something like this

    Import-Csv -Path .\cluster-export.csv -UseCulture -PipelineVariable row |
    ForEach-Object -Process {
        $dcName,$dummy,$clusterName = $row.Path.Split('\')
        New-Cluster -Name $clusterName -Location $dcName
    }

      



  • 13.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 03, 2022 07:39 PM

     Thank you, this worked. The last piece im working on is the VM folder structure. I pretty much used the previous scripts you gave me and tried to alter for New-folder. But my import folder keeps creating them in th Hosts and Clusters View but I want in VM and folders View. How would I designate which view to create in?

    ##IMPORT FOLDERS
    Import-Csv -Path c:\temp\clusters.csv -UseCulture -PipelineVariable row |
    ForEach-Object -Process {
    $dcName,$dummy,$folderName = $row.Path.Split('\')
    New-Folder -Name $folderName -Location $dcName }

    My input file is structured like:

    "Name","Path"
    "Linux","testing1vm\vmvm\Linux"
    "Windows","testing1vm\vmvm\Windows"

     



  • 14.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 03, 2022 07:42 PM

    You will have to use another hidden folder for the VM&Template folders.
    Under the Datacenter there is a hidden folder named 'vm'.
    Use that one as the Location on the New-Folder cmdlet.



  • 15.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 13, 2022 05:19 PM

    So I was able to get Hosts and Clusters view and VM and folders view, but struggling with our folders in the Network and Datastores view. For example, using similar scripts, I was able to export our datastores view and the .csv format is something like this:

    "Name","Path","Type"
    "System","testing\datastore\System","blue"
    "Virtual Machine","testing\datastore\Virtual Machine","blue"

     

    However, when I use this to import, it creates it on the Hosts and Clusters view; i have a feeling i can't use the 'New-Folder' command, right to create on Datastores view(and eventually network)

     

    ##IMPORT FOLDERS
    Import-Csv -Path c:\temp\datastoresfolders.csv -UseCulture -PipelineVariable row |
    ForEach-Object -Process {
    $dcName,$dummy,$folderName = $row.Path.Split('\')
    New-Folder -Name $folderName -Location $dcName }



  • 16.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 13, 2022 06:32 PM

    You have to follow the full path, datacenter and hidden folder (in this case 'datastore') to find the Location for the New-FOlder cmdlet.

    Something like this

    $dcName = 'DC'
    $hiddenFolder = 'datastore'
    $newFolder = 'TestFolder'
    
    $location = Get-Datacenter -Name $dcName | Get-Folder -Name $hiddenFolder
    New-Folder -Name $newFolder -Location $location
    


  • 17.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 13, 2022 07:10 PM

     Ah, I was missing the full path; you are awesome. thank you. I got the loop working and its now rolling through all my vcenters and populating correctly. I can use this for my network ones too; just a different csv file. I think I finally understand the 'hiddenfolder' concept with each view.

    ##IMPORT FOLDERS
    Import-Csv -Path c:\temp\datastorefolders.csv -UseCulture -PipelineVariable row |
    ForEach-Object -Process {
    $dcName,$dummy,$folderName = $row.Path.Split('\')
    New-Folder -Name $folderName -Location (Get-Datacenter -Name $dcName | Get-Folder $dummy) }

    So, here is my last conundrum....I've been looking at everyone's permissions scripts and it seems that all of them can only handle one datacenter as input.  How can I get the permissions from old vcenter exported with datacenter path for VM and Folder view(and eventually network and datastore) but also be able to loop through multiple datacenters? The ones i've come across can only create the folder and subfolder for one datacenter but then error out because they think its a duplicate name; not knowing how to iterate to new datacenter and so on.



  • 18.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 13, 2022 08:02 PM

    In which format did you export the permissions?
    What is included in that file?



  • 19.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 13, 2022 08:34 PM

    The one I used exports it in 2 formats; one is .csv and the other is .xml

    #TYPE Selected.VMware.VimAutomation.ViCore.Impl.V1.PermissionManagement.PermissionImpl
    "vCenter","Principal","Role","Propagate","Entity","Entity Type"

     

    The problem with the export though, is it only declares the datacenter for the "cluster compute resource" and not "Virtual Machine". for virtual machine it looks like:

    "testvcenter","DOMAIN\Role-Linux","Role.Vm.VmSupport","True","Linux","Folder"

     

    The problem is that every datacenter has a folder called "Linux" under it, so this script can distinguish the next DC; just applies on the first one and then complains about folder already created.

     

    full export script i'm using. FYI, I commented out role stuff because I already created those in new vcenter

    $PermissionXML = Get-VIPermission
    $PermissionXML | Export-Clixml -Path "C:\Temp\$vCenterHost-Permission.xml"


    #Roles
    #$Role = Get-VIRole | Select-Object @{N='vCenter';E={$_.Uid.Split('@:')[1]}},
    #Name,
    #@{N='PrivilegeList';E={[string]::Join([char]10,$_.PrivilegeList)}}


    #Export to CSV
    #$Role | Export-Csv -Path "C:\Temp\$vCenterHost-Roles.csv"

    #Export to XML
    #$RoleXML = Get-VIRole
    #$RoleXML | Export-Clixml -Path "C:\Temp\$vCenterHost-Roles.xml"

    Write-Verbose "`tRole & Permission Data Exported Successfully from $vCenterHost" -Verbose

    Write-Verbose "Disconnecting from $vCenterHost" -Verbose
    Disconnect-VIServer -Server

    }

    catch {

    Write-Verbose "`tError Encountered! Error:$_" -Verbose
    $ErrorObject = New-Object -TypeName PSObject -Property @{
    vCenterName = $vCenterHost
    Error = $_

    }

    }



  • 20.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 14, 2022 09:28 AM

    I tested the following to export/import permissions.
    And it seems to work for me.

    One note, the vCLS VMs and Folders use a builtin account (like vsphere.local/vCLSAdmin) that can apparently not be used as a Principal.
    Which shouldn't be a problem since these Folders, VMs and Permissions are generated automatically by the vCenter.

    To export I used this

    Get-VIPermission |
    where{$_.Principal -notmatch 'vCLSAdmin$'} |
    Select-Object Entity,
    @{N='Path';E={
        $hidden = @('Datacenters')
        $script:object = Get-View -Id $_.EntityId
        $path = @($script:object.Name)
        $parent = $script:object.Parent
    
        while ($parent) {
          $pObject = Get-View -Id $Parent -Property Name, Parent
          $parent = $pObject.Parent
          if ($pObject -and $hidden -notcontains $pObject.Name) {
            $path += $pObject.Name
          }
        }
        [array]::Reverse($path)
        "$($path -join '/')"
    }},
    Principal, Role, Propagate |
    Export-Csv -Path .\permissions.csv -NoTypeInformation -UseCulture
    


    To import I used this

    $root = Get-Folder -Name Datacenters
    
    Import-Csv -Path .\permissions.csv -UseCulture -PipelineVariable row |
    ForEach-Object -Process {
      if($row.Path -ne 'Datacenters'){
        $dcName, $nodes = $row.Path.Split('/')
        $entity = Get-Datacenter -Name $dcName
        $nodes[0..($nodes.Count - 1)] | ForEach-Object -Process {
          $entity = Get-Inventory -Name $_ -Location $entity
        }
      }
      else{
        $entity = $root
      }
      $sPerm = @{
        Entity = $entity
        Role = $row.Role
        Principal = $row.Principal
        Propagate = [Boolean]$row.Propagate
        WhatIf = $true
      }
      New-VIPermission 
    }

     

    Note that I used to WhatIf switch for testing.
    Once you are sure the correct permissions are applied, remove that WhatIf entry from the hash table, or set it to $false



  • 21.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 14, 2022 03:39 PM

     

    Thanks, I'll try this out today.

    On a side note, I'm troubleshooting the Network folder import; it worked fine for datastore folder input. do you know why I'm getting these errors?

    New-Folder : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.ViCore.Types.V1.Inventory.VIContainer' required by parameter 'Location'. Specified
    method is not supported.
    At D:\Scripts\vCenter Scripts\ImportNetworkFolders.ps1:15 char:44
    + ... erName -Location (Get-Datacenter -Name $dcName | Get-Folder $dummy) }
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [New-Folder], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewFolder

    New-Folder : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.ViCore.Types.V1.Inventory.VIContainer' required by parameter 'Location'. Specified
    method is not supported.
    At D:\Scripts\vCenter Scripts\ImportNetworkFolders.ps1:15 char:44
    + ... erName -Location (Get-Datacenter -Name $dcName | Get-Folder $dummy) }
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [New-Folder], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewFolder

    New-Folder : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Name'. Specified method is not supported.
    At D:\Scripts\vCenter Scripts\ImportNetworkFolders.ps1:15 char:22
    + New-Folder -Name $folderName -Location (Get-Datacenter -Name $dcN ...
    + ~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [New-Folder], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewFolder

     

    the script

    ##IMPORT Network FOLDERS
    Import-Csv -Path c:\temp\networkfolders-test.csv -UseCulture -PipelineVariable row |
    ForEach-Object -Process {
    $dcName,$dummy,$folderName = $row.Path.Split('\')
    New-Folder -Name $folderName -Location (Get-Datacenter -Name $dcName | Get-Folder $dummy) }



  • 22.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 14, 2022 04:07 PM

    It looks as if the value you pass on the Location parameter is an array, not a single object.



  • 23.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 14, 2022 07:51 PM

     I think i know the issue but not sure how to fix.  my .csv file for datacenter field has periods in it. It works without them:

    "Name","Path","Type"
    "Virtual Machine","testing\network\Virtual Machine","blue"
    "VMkernel","testing\network\VMkernel","blue"
    "Virtual Machine","testing2\network\Virtual Machine","blue"
    "VMkernel","testing2\network\VMkernel","blue"

    but my datacenters names are more like:

    "Name","Path","Type"
    "Virtual Machine","testing.dc.01\network\Virtual Machine","blue"
    "VMkernel","testing\network.dc.01\VMkernel","blue"
    "Virtual Machine","testing.dc.02\network\Virtual Machine","blue"
    "VMkernel","testing.dc.02\network\VMkernel","blue"

     



  • 24.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 14, 2022 08:46 PM

    The Path is split on the '\' character, the '.' shouldn't be of any consequence.
    It looks as if the Get-Datacenter or Get-Folder returns more than 1 object.



  • 25.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 14, 2022 10:07 PM

    I'm not sure what you mean? 

    ##IMPORT FOLDERS
    Import-Csv -Path c:\temp\networkfolders.csv -UseCulture -PipelineVariable row |
    ForEach-Object -Process {
    $dcName,$dummy,$folderName = $row.Path.Split('\')
    New-Folder -Name $folderName -Location (Get-Datacenter -Name $dcName | Get-Folder $dummy) }



  • 26.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 15, 2022 06:28 AM

    Either Get-Datacenter or Get-Folder in the line

    New-Folder -Name $folderName -Location (Get-Datacenter -Name $dcName | Get-Folder $dummy) }

    is returning more than 1 object.
    You would have to have a look at the Path value in the CSV, do the split and check if any of $dcName or $dummy returns more than 1 object.



  • 27.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 17, 2022 02:49 PM

     Okay, just to keep testing simple, I stripped down the .csv to two lines and it still returns the errors.

    "Name","Path","Type"
    "Virtual Machine","robo.dc.01\network\Virtual Machine","blue"
    "VMkernel","robo.dc.01\network\VMkernel","blue"

    if I follow the split it should be:  $dcName = robo.dc.01,  $dummy = network, and $FolderName  = Virtual Machine

    I'm stumped as why it would think multiple values are being returned?

     

     



  • 28.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 17, 2022 03:02 PM

     So, check this out. just for testing I created a test datacenter called "robotesting1". ran the script and it created those two folders successfully. So, it has to be the two periods in my datacenter name? right?

    On a side note, there is a subfolder under one of the main folders and that failed; probably because the split can't handle that.



  • 29.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 17, 2022 04:14 PM

    I don't immediately see why the period would be an issue, the split is done on '/'.
    Btw, you can change that character to whatever you like, as long as there is no conflict with characters used in your names.

    What was the name of that folder?
    And what was the error message?



  • 30.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 17, 2022 06:21 PM

    So, here is the .csv that worked.

    "Name","Path","Type"
    "Virtual Machine","robotesting1\network\Virtual Machine","blue"
    "VMkernel","robotesting1\network\VMkernel","blue"

     

    and the one that didn't work

    "Name","Path","Type"
    "Virtual Machine","robo.dc.00001\network\Virtual Machine","blue"
    "VMkernel","robo.dc.00001\network\VMkernel","blue"

     



  • 31.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 17, 2022 06:22 PM

     forgot to post the error.

    New-Folder : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.ViCore.Types.V1.Inventory.VIContainer' required by parameter 'Location'. Specified
    method is not supported.
    At D:\Scripts\vCenter Scripts\ImportNetworkFolders.ps1:15 char:44
    + ... erName -Location (Get-Datacenter -Name $dcName | Get-Folder $dummy) }
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [New-Folder], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewFolder

    New-Folder : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.ViCore.Types.V1.Inventory.VIContainer' required by parameter 'Location'. Specified
    method is not supported.
    At D:\Scripts\vCenter Scripts\ImportNetworkFolders.ps1:15 char:44
    + ... erName -Location (Get-Datacenter -Name $dcName | Get-Folder $dummy) }
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [New-Folder], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewFolder

    New-Folder : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Name'. Specified method is not supported.
    At D:\Scripts\vCenter Scripts\ImportNetworkFolders.ps1:15 char:22
    + New-Folder -Name $folderName -Location (Get-Datacenter -Name $dcN ...
    + ~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [New-Folder], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewFolder

     



  • 32.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 17, 2022 06:28 PM

    That means again that there is more than 1 object being returned.
    The value passed on the LOcation parameter is an array of objects.



  • 33.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 17, 2022 06:30 PM

      But how?

    So, here is the .csv that worked.

    "Name","Path","Type"
    "Virtual Machine","robotesting1\network\Virtual Machine","blue"
    "VMkernel","robotesting1\network\VMkernel","blue"

     

    and the one that didn't work

    "Name","Path","Type"
    "Virtual Machine","robo.dc.00001\network\Virtual Machine","blue"
    "VMkernel","robo.dc.00001\network\VMkernel","blue"



  • 34.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 17, 2022 06:32 PM

    Which export-import scripts are you using?
    It doesn't look like the code I'm assuming you are using



  • 35.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 17, 2022 06:36 PM

     

    Export script

    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 = $true

    )

     

    begin{

    $excludedNames = "Datacenters","vm","host"

    }

     

    process{

    $Folder | %{

    $fld = $_.Extensiondata

    $fldType = "yellow"

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

    $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 network| where{$_.Name -ne 'network'} | Get-Folderpath

    $report | Export-Csv 'c:\temp\networkstuff.csv' -NoTypeInformation -UseCulture

    Import Scripts

    ##IMPORT Network FOLDERS
    Import-Csv -Path c:\temp\networkfolders-test2.csv -UseCulture -PipelineVariable row |
    ForEach-Object -Process {
    $dcName,$dummy,$Name = $row.Path.Split('\')
    New-Folder -Name $Name -Location (Get-Datacenter -Name $dcName | Get-Folder -Name $dummy) }



  • 36.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 17, 2022 06:45 PM

    Ok, the split character is '\' in both.
    And I see no reason why the '.' should interfere with the split.
    And I assume that Get-Datacenter -Name 'robo.dc.00001' only returns 1 Datacenter object?



  • 37.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 17, 2022 06:55 PM

     yeah, basically robo.dc.00001 in the name of the datacenter and I want to create two parent folders at the root of the dc object. one called "virtual machine" and the other called "vmkernel". Which makes for two separate lines in the .csv file

    "Name","Path","Type"
    "Virtual Machine","robo.dc.00001\network\Virtual Machine","blue"
    "VMkernel","robo.dc.00001\network\VMkernel","blue"



  • 38.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 17, 2022 07:16 PM

    I just tested in my lab.
    With a Datacentername with dots in there, the creation of a Folder under Network worked without any issue.
    I'm not sure what is happening in your environment, but the error message seems to indicate that more than 1 object is returned.

    You could test if each of the following only returns 1 object

    Get-Datacenter -Name 'robo.dc.00001'
    Get-Datacenter -Name 'robo.dc.00001' | Get-Folder -Name 'Network'
    


  • 39.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 17, 2022 07:58 PM

     the second command you had for testing, returns this:

    Name             Type
    ----                  ----
    network        Network
    Network         VM
    Network         VM



  • 40.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 17, 2022 08:09 PM

    And that explains why it returns more than 1 object.

    In this case (Network folder) you could use

    New-Folder -Name $Name -Location (Get-Datacenter -Name $dcName | Get-Folder -Name $dummy -Type $row.Type)  

    But we will probably have to adapt the export script to use the correct name for the Type parameter.



  • 41.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 17, 2022 08:42 PM

     yeah, now I get this.

    Get-Folder : Cannot bind parameter 'Type'. Cannot convert value "blue" to type "VMware.VimAutomation.ViCore.Types.V1.Inventory.FolderType". Error: "Unable to match the identifier name blue
    to a valid enumerator name. Specify one of the following enumerator names and try again:
    VM, HostAndCluster, Datacenter, Datastore, Network"
    At D:\Scripts\vCenter Scripts\ImportNetworkFolders.ps1:15 char:96
    + ... atacenter -Name $dcName | Get-Folder -Name $dummy -Type $row.Type) }
    + ~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [Get-Folder], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetFolder



  • 42.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 17, 2022 09:06 PM

    That's normal, I thought the type would say network.

    The export needs to adapted



  • 43.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 17, 2022 09:25 PM

     A different export script entirely? I remember reading about this one you did, but I couldn't figure out how to do it for just datastore or network folders? 

    New-VIProperty -Name 'BlueFolderPath' -ObjectType 'VirtualMachine' -Value {
        param($vm)
    
        function Get-ParentName{
            param($object)
    
            if($object.Folder){
                $blue = Get-ParentName $object.Folder
                $name = $object.Folder.Name
            }
            elseif($object.Parent -and $object.Parent.GetType().Name -like "Folder*"){
                $blue = Get-ParentName $object.Parent
                $name = $object.Parent.Name
            }
            elseif($object.ParentFolder){
                $blue = Get-ParentName $object.ParentFolder
                $name = $object.ParentFolder.Name
            }
            if("vm","Datacenters" -notcontains $name){
                $blue + "/" + $name
            }        
    else{             $blue
            }     }     (
    Get-ParentName $vm).Remove(0,1) } -Force | Out-Null
    $dcName
    = "MyDC"

    Get-VM
    -Location (Get-Datacenter -Name $dcName) | Select Name,BlueFolderPath |Export-Csv "C:\vm-folder.csv" -NoTypeInformation -UseCulture


  • 44.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 18, 2022 09:37 AM

    Try these

    For export

     

    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 = $true
      )
    
      begin {
        $excludedNames = "Datacenters", "vm", "host"
      }
    
      process {
        $Folder | ForEach-Object {
          $fld = $_.Extensiondata
          $fldType = "yellow"
          if ($fld.ChildType -contains "Network") {
            $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-Object Name, Path, Type
          $row.Name = $_.Name
          $row.Path = $path
          $row.Type = $fldType
          $row
        }
      }
    }
    
    $hidden = 'Datacenters','host','vm','network','datastore','vCLS'
    Get-Folder | where{$_.Name -notin $hidden} |
    Select @{N='Path';E={(Get-FolderPath -Folder $_).Path}} |
    Export-Csv -Path .\export-folder.csv -NoTypeInformation -UseCulture
    

     

    For import

     

    Import-Csv -Path .\export-folder.csv -UseCulture -PipelineVariable row |
    ForEach-Object -Process {
      $root,$dcName,$path = $row.Path.Split('\',3)
      $location = Get-Datacenter -Name $dcName
      $path.Split('\') | ForEach-Object -Process {
        $name = $_
        try{
          $location = Get-Folder -Name $_ -Location $location -ErrorAction Stop
        }
        catch{
          New-Folder -Name $name -Location $location
        }
      }
    }

     





  • 45.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter
    Best Answer

    Posted Oct 18, 2022 02:38 PM

     

    Getting some new errors on the import with these scripts....

     

    New-Folder : 10/18/2022 7:35:09 AM New-Folder '10/18/2022 7:35:09 AM Get-Folder Folder with name 'Virtual Machine' was not found using the specified filter(s). ' is invalid or exceeds the maximum number of characters
    permitted.
    At line:10 char:7
    + New-Folder -Name $_ -Location $location
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [New-Folder], InvalidName
    + FullyQualifiedErrorId : Client20_InventoryServiceImpl_NewFolder_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewFolder

    New-Folder : 10/18/2022 7:35:09 AM New-Folder '10/18/2022 7:35:09 AM Get-Folder Folder with name 'VMkernel' was not found using the specified filter(s). ' is invalid or exceeds the maximum number of characters permitted.
    At line:10 char:7
    + New-Folder -Name $_ -Location $location
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [New-Folder], InvalidName
    + FullyQualifiedErrorId : Client20_InventoryServiceImpl_NewFolder_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewFolder

     

    my .csv output

    "Path"
    "Datacenters\testing2\network\Virtual Machine"
    "Datacenters\testing2\network\VMkernel"
    "Datacenters\testing2\datastore\System"
    "Datacenters\testing2\datastore\Virtual Machine"



  • 46.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 18, 2022 04:08 PM

    My bad, I forgot that the $_ variable inside a catch-block contains the error, not the value from the loop.
    I corrected the code above



  • 47.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 18, 2022 04:25 PM

    Thanks for all the help. These scripts worked perfectly in my Lab but got the same errors in my other system. I think something is wrong with the PowerCLI install. It does the very first line in the import script and then simply errors out the rest with the same error. I do notice the $PSTableVersion shows 5.1.19041.1682 for the lab one and the non-working one is 5.1.14393.2125.

     



  • 48.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 18, 2022 04:36 PM

    Those PS version differences shouldn't cause those errors.

    You are sure that $name variable I added is there?



  • 49.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 18, 2022 05:08 PM

    Yeah, i got your updated script and it worked fine in my lab. I have to use the other powershell instance because of security; basically I'm running it from a jumpbox that has access to those particular vcenters. 

    i run it and it just errors.


    New-Folder : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.ViCore.Types.V1.Inventory.VIContainer' required by parameter 'Location'. Specified
    method is not supported.
    At line:11 char:40
    + New-Folder -Name $name -Location $location
    + ~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [New-Folder], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewFolder



  • 50.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 18, 2022 05:11 PM

    But that is again that original error which seems to indicate an array instead of single object is passed on the Location parameter.
    You could do those 2 tests I mentioned yesterday



  • 51.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 18, 2022 08:03 PM

     Get-Datacenter on any of the DC's just return one value.

    The 2nd command returns 3 values Get-Datacenter -Name 'robo.dc.00001' | Get-Folder -Name 'Network'

    Name Type
    ---- ----
    network Network
    Network VM
    Network VM



  • 52.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 18, 2022 08:18 PM

    So you have 2 VM folders that are named Network, and on Network folder that is also named Network.
    Not sure how to code around that (yet)



  • 53.  RE: need help with scripting export of datacenter and clusters to import into brand new vcenter

    Posted Oct 18, 2022 08:28 PM

    yeah, i was thinking the same thing. so I tried adding -type network to your script. It worked...kind of. created my network folders, but also created a bunch of folders at the hosts and clusters view at the Datacenter root. I'm thinking I could just edit my .csv and trim out everything except the network folders

     

    Import-Csv -Path c:\temp\export-networkfolder-test.csv -UseCulture -PipelineVariable row |
    ForEach-Object -Process {
    $root,$dcName,$path = $row.Path.Split('\',3)
    $location = Get-Datacenter -Name $dcName
    $path.Split('\') | ForEach-Object -Process {
    $name = $_
    try{
    $location = Get-Folder -Type Network -Name $_ -Location $location -ErrorAction Stop
    }
    catch{
    New-Folder -Name $name -Location $location
    }
    }
    }