VMware Cloud Community
nareshunik
Enthusiast
Enthusiast

Import and export DRS rules

Need a script to import DRS rules and export DRS rules...

the output in .csv format

0 Kudos
6 Replies
LucD
Leadership
Leadership

See Arne's post called PowerCLI: Export, Import and Create DRS Rules v2


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

0 Kudos
nareshunik
Enthusiast
Enthusiast

before posting in communities i checked it.

Export of DRS rules works fine..

The same tried to import it, but it did not work.

Get-VM : Cannot validate argument on parameter 'Id'. The argument is null, empt
y, or an element of the argument collection contains a null value. Supply a col
lection that does not contain any null values and then try the command again.
At C:\users\cz3m7g\desktop\scripts\DRS Rules\import drs rules.ps1:6 char:20
+     -VM (Get-VM -Id <<<<  $rule.VmIds)}
    + CategoryInfo          : InvalidData: (:) [Get-VM], ParameterBindingValid
   ationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
   ation.ViCore.Cmdlets.Commands.GetVM
Get-Cluster : 3/25/2012 11:17:59 AM    Get-Cluster        Cluster with name 'CP
THI05' was not found, using the specified filter(s).
At C:\users\cz3m7g\desktop\scripts\DRS Rules\import drs rules.ps1:3 char:38
+     New-DrsRule -Cluster (Get-Cluster <<<<  -Name "CPTHI05") `
    + CategoryInfo          : ObjectNotFound: (:) [Get-Cluster], VimException
    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimA
   utomation.ViCore.Cmdlets.Commands.GetCluster
Get-VM : 3/25/2012 11:18:01 AM    Get-VM        VM with id 'VirtualMachine-vm-1
290' was not found, using the specified filter(s).
At C:\users\cz3m7g\desktop\scripts\DRS Rules\import drs rules.ps1:6 char:16
+     -VM (Get-VM <<<<  -Id $rule.VmIds)}
    + CategoryInfo          : ObjectNotFound: (:) [Get-VM], VimException
    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimA
   utomation.ViCore.Cmdlets.Commands.GetVM
Get-VM : 3/25/2012 11:18:01 AM    Get-VM        VM with id 'VirtualMachine-vm-1
291' was not found, using the specified filter(s).
At C:\users\cz3m7g\desktop\scripts\DRS Rules\import drs rules.ps1:6 char:16
+     -VM (Get-VM <<<<  -Id $rule.VmIds)}
    + CategoryInfo          : ObjectNotFound: (:) [Get-VM], VimException
    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimA
   utomation.ViCore.Cmdlets.Commands.GetVM
New-DrsRule : Cannot bind parameter 'Cluster'. Cannot convert the "" value of t
ype "System.Management.Automation.PSCustomObject" to type "VMware.VimAutomation
.ViCore.Types.V1.Inventory.Cluster".
At C:\users\cz3m7g\desktop\scripts\DRS Rules\import drs rules.ps1:3 char:25
+     New-DrsRule -Cluster <<<<  (Get-Cluster -Name "CPTHI05") `
    + CategoryInfo          : InvalidArgument: (:) [New-DrsRule], ParameterBin
   dingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomat
   ion.ViCore.Cmdlets.Commands.NewDrsRule
Get-Cluster : 3/25/2012 11:18:01 AM    Get-Cluster        Cluster with name 'CP
THI05' was not found, using the specified filter(s).
At C:\users\cz3m7g\desktop\scripts\DRS Rules\import drs rules.ps1:3 char:38
+     New-DrsRule -Cluster (Get-Cluster <<<<  -Name "CPTHI05") `
    + CategoryInfo          : ObjectNotFound: (:) [Get-Cluster], VimException
    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimA
   utomation.ViCore.Cmdlets.Commands.GetCluster
Get-VM : Cannot validate argument on parameter 'Id'. The argument is null, empt
y, or an element of the argument collection contains a null value. Supply a col
lection that does not contain any null values and then try the command again.
At C:\users\cz3m7g\desktop\scripts\DRS Rules\import drs rules.ps1:6 char:20
+     -VM (Get-VM -Id <<<<  $rule.VmIds)}
    + CategoryInfo          : InvalidData: (:) [Get-VM], ParameterBindingValid
   ationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
   ation.ViCore.Cmdlets.Commands.GetVM
0 Kudos
LucD
Leadership
Leadership

Is the export/import done on the same vCenter ?

It also looks as if the cluster doesn't exist where you are doing the import.


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

0 Kudos
nareshunik
Enthusiast
Enthusiast

Now i export/imported the DRS rules , same vcenter,same cluster.. Still get error.

Get-VM : Cannot validate argument on parameter 'Id'. The argument is null, empt
y, or an element of the argument collection contains a null value. Supply a col
lection that does not contain any null values and then try the command again.
At C:\users\xxxxx\desktop\scripts\DRS Rules\import drs rules.ps1:6 char:20
+     -VM (Get-VM -Id <<<<  $rule.VmIds)}
    + CategoryInfo          : InvalidData: (:) [Get-VM], ParameterBindingValid
   ationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
   ation.ViCore.Cmdlets.Commands.GetVM

===========

To import i am using the below script.

# Import DRS
ForEach ($rule in (Import-CliXml 'C:\scripts\drs.xml')){
    New-DrsRule -Cluster (Get-Cluster -Name "Cluster_01") `
    -Name $rule.Name -Enabled $rule.Enabled `
    -KeepTogether $rule.KeepTogether `
    -VM (Get-VM -Id $rule.VmIds)}
0 Kudos
LucD
Leadership
Leadership

That seems to work for me.

Did you check the contents of the XML file and verified if those IDs are for existing VMs on the cluster ?

There appear to be some issues with New-DrsRule cmdlet when you try to add rules to a cluster that has already rules (created in vSphere 4.x).

I got the following error in such a situation.

New-DrsRule : 27-03-2012 18:20:29    New-DrsRule        Unable to cast object of type 'VMware.Vim.ClusterVmHostRuleInfo'
to type 'VMware.Vim.ClusterAntiAffinityRuleSpec'.
At line:1 char:12
+ New-DrsRule <<<<  -Cluster MyCluster -name test -enabled $true -vm (Get-VM VM1,VM2) -KeepTogether $false
    + CategoryInfo          : NotSpecified: (:) [New-DrsRule], ViError
    + FullyQualifiedErrorId : Client20_ClientSideTaskImpl_ThreadProc_UnhandledException,VMware.VimAutomation.ViCore.Cmdl
   ets.Commands.NewDrsRule


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

0 Kudos
nareshunik
Enthusiast
Enthusiast

I will verify the VM id from xml file..

0 Kudos