I would like to set multiple properties for data collector set object in powershell. How can I do that,
Here is the code I was trying that fails.
$hash_table=@{
DisplayName = "Test"
RootPath = "C:\Test"
Segment = -1
SegmentMaxDuration = 600
SegmentMaxSize = 0
SubdirectoryFormat = 1
}
$dcs = New-Object -COM Pla.DataCollectorSet @hash_table
Error Details:
New-Object : A parameter cannot be found that matches parameter name 'DisplayName'.
At line:9 char:45
+ $dcs = New-Object -COM Pla.DataCollectorSet @hash_table
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-Object], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
Any better ways of setting all required properties at once instead of setting them one by one?
You can't splat object properties like this, what your current code does is try to pass the hashtable parameters as parameters to New-Object
, not to the COM object you are trying to instantiate. What you want to do is use the -Property
parameter to set the properties you've named in the hashtable on the object. This should work for you:
$hash_table=@{
DisplayName = "Test"
RootPath = "C:\Test"
Segment = -1
SegmentMaxDuration = 600
SegmentMaxSize = 0
SubdirectoryFormat = 1
}
$dcs = New-Object -COM Pla.DataCollectorSet -Property $hash_table
If you wanted to use splatting for the New-Object
parameters, your code would need to look more like this:
$hash_table=@{
COMObject = 'Pla.DataCollectorSet'
Property = @{
DisplayName = "Test"
RootPath = "C:\Test"
Segment = -1
SegmentMaxDuration = 600
SegmentMaxSize = 0
SubdirectoryFormat = 1
}
}
$dcs = New-Object @hash_table
See this example from Microsoft's documentation on New-Object
for more information.