[英]Powershell - how do I add multiple objects to a property on a custom object?
I'm currently working on a project to document a bunch of SSIS packages.我目前正在做一个项目来记录一堆 SSIS 包。 Since the packages are stored in XML it's pretty easy to import the package as XML and I can get to the information I want.
由于包存储在 XML 中,因此将 package 导入为 XML 非常容易,我可以得到我想要的信息。
What I think I want to do is capture key information in objects with my own defined properties so at the end I can export the object in a format of my choosing.我想我想做的是用我自己定义的属性捕获对象中的关键信息,这样最后我可以以我选择的格式导出 object。
I'm trying to get my head around properties of objects and how I would assign multiple objects to a property.我试图了解对象的属性以及如何将多个对象分配给一个属性。
So for instance I create an object $Package that represents the SSIS package.例如,我创建了一个 object $Package,它代表 SSIS package。 That package would have some top level properties for instance Name, Path, and Type all stored as strings.
package 将具有一些顶级属性,例如名称、路径和类型,所有这些属性都存储为字符串。 But then there are 5 ConnectionManagers in the package so I want to assign 5 objects representing the ConnectionManagers to $Package.
但是 package 中有 5 个 ConnectionManager,所以我想将代表 ConnectionManager 的 5 个对象分配给 $Package。 Now one of the connection managers has 2 expressions so I want to reference 2 objects for the expression property under the ConnectionManager Property.
现在其中一个连接管理器有 2 个表达式,所以我想为 ConnectionManager 属性下的表达式属性引用 2 个对象。
How do I add a collection of objects as a property of an existing custom object and do I have to add them all at once or can I add to that collection?如何将对象集合添加为现有自定义 object 的属性,我必须一次将它们全部添加还是可以添加到该集合中?
With respect to Bee_Riii's comment, you do not have to include all properties while defining an object.关于 Bee_Riii 的评论,您不必在定义 object 时包含所有属性。 You can add a new array using Add-Member .
您可以使用Add-Member添加新数组。
Add-Member -InputObject $p -NotePropertyName myVariables -NotePropertyValue @(1,2,3)
You might also choose to use an existing value.您也可以选择使用现有值。
$existingValue = @(1,2,3)
Add-Member -InputObject $p -NotePropertyName myVariables -NotePropertyValue $existingValue
Here I use the variable name myVariables
as a precaution.这里我使用变量名
myVariables
作为预防措施。 Generic names like name
and variable
often collide with existing properties. name
和variable
等通用名称经常与现有属性发生冲突。 I formed the habit of prepending 'my' to these names to avoid collisions.我养成了在这些名字前面加上“我的”以避免冲突的习惯。
You might find these resources useful:您可能会发现这些资源很有用:
$p = [PSCustomObject] @{
Name = "packageName"
Path = "packagePath"
Type = "packageType"
ConnectionManagers = @()
}
$p.ConnectionManagers += [PSCustomObject] @{
Name = "managerName"
Connection = "connectionString"
}
sets up a package with an empty connection manager list, then adds a connection manager object to it.使用空的连接管理器列表设置 package,然后向其添加连接管理器 object。
If you already have a list of connection managers in a variable, replace the [PSCustomObject] in that last assignment with the variable containing the list you want to add如果变量中已有连接管理器列表,请将最后一个赋值中的 [PSCustomObject] 替换为包含要添加的列表的变量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.