Anyone have ideas on the best way to use the information exported with this command in a create-cluster or set-cluster?
Get-Cluster -name $Cluster | Select Name, HAEnabled, HAFailoverLevel, DrsEnabled, DrsMode | Export-Csv -NoTypeInformation -UseCulture -Path .\$Cluster.csv
Having a tricky time with getting the variables back into a new-cluster or set-cluster command. For example, my csv is here:
"Name","HAEnabled","HAFailoverLevel","DrsEnabled","DrsMode"
"ClusterName","True","1","True","FullyAutomated"
The -HAAdmissionControlEnabled parameter for example in set-cluster or create-cluster has a $ sign that is making it tricky to get the value in my csv back in
set-cluster -cluster $Cluster -HAEnabled:$True -HAAdmissionControlEnabled:$True -confirm:$False
Comes out something like this when I put it together
set-cluster -cluster $cluster -HAEnabled:$$myvaluefromcsv
Splatting is a good way to bypass this (especially for switches)
Import-Csv yourcsv.csv | %{
$splat = @{
Cluster = $_.Name
HAEnabled = [boolean]$_.HAEnabled
HAAdmissionControlEnabled = [boolean]$_.HAAdmissionControlEnabled
Confirm = $false
}
Set-Cluster @splat
}
During the export the Boolean values will be converted to a [string], saying true or false.
When you import the CSV you will be getting a [string], but with a Boolean cast the string is converted to a Boolean.
Try like this
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Splatting is a good way to bypass this (especially for switches)
Import-Csv yourcsv.csv | %{
$splat = @{
Cluster = $_.Name
HAEnabled = [boolean]$_.HAEnabled
HAAdmissionControlEnabled = [boolean]$_.HAAdmissionControlEnabled
Confirm = $false
}
Set-Cluster @splat
}
During the export the Boolean values will be converted to a [string], saying true or false.
When you import the CSV you will be getting a [string], but with a Boolean cast the string is converted to a Boolean.
Try like this
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Awesome. Many thanks as always for the help. Would I also be able to slip in variables defined earlier in the script and not contained in the csv?
Import-Csv yourcsv.csv | %{
$splat = @{
Location = $variabledefinedearlier
Cluster = $_.Name
HAEnabled = [boolean]$_.HAEnabled
HAAdmissionControlEnabled = [boolean]$_.HAAdmissionControlEnabled
Confirm = $false
}
Set-Cluster @splat
}
Thanks!!!!!
I got it. Thanks again Luc, this is a neat little way to get around this.
Sure, you should be able to use any variable in the assignments in the splat block
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Interesting wrinkle on this one though. So, if you pickup the cluster settings and haenabled or drsenabled are set to "False", the splat process (boolean?) processes those as "True". Assuming it is stating True or False based on if there is a value there.
my band-aid:
$splat = @{
Name = $_.Name
HAEnabled = If ($_.HAEnabled -eq "False") {$False} else {[Boolean]$_.HAEnabled}
DrsEnabled = If ($_.DrsEnabled -eq "False") {$False} else {[Boolean]$_.DrsEnabled}
Yes, my bad on the Boolean casting, that is a known "issue".
An alternative is to use the System.Convert method
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference