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.
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) }
It looks as if the value you pass on the Location parameter is an array, not a single object.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
@LucD 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"
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.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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) }
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.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
@LucD 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?
@LucD 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.
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?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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"
@LucD 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
That means again that there is more than 1 object being returned.
The value passed on the LOcation parameter is an array of objects.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
@LucD 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"
Which export-import scripts are you using?
It doesn't look like the code I'm assuming you are using
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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) }
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?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
@LucD 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"
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'
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
@LucD the second command you had for testing, returns this:
Name Type
---- ----
network Network
Network VM
Network VM
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.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference