简体   繁体   English

Powershell - 如何将多个对象添加到自定义 object 的属性中?

[英]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. namevariable等通用名称经常与现有属性发生冲突。 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.

相关问题 Powershell - 如何编辑自定义对象中的现有属性 - Powershell - how do I edit an existing property in a custom object 如何向现有的自定义 object 添加新属性并向 Powershell 中新添加的属性添加值? - How do I add new properties to an existing custom object and add values to the newly added properties in Powershell? 如何在PowerShell中将多个json对象添加到一个json对象 - How to add multiple json objects to one json object in PowerShell 在Powershell中,如何将对象数组的一个属性连接成一个字符串? - In Powershell, how do I concatenate one property of an array of objects into a string? 如何按 Powershell 中的一个属性值对对象数组进行排序? - How do I sort an array of objects by one of their property values in Powershell? 如何在PowerShell PSDrive“函数:”上向函数对象添加属性? - How do you add a property to a function object on the PowerShell PSDrive “function:”? 如何将System.Collections.ArrayList添加到PowerShell自定义对象? - How do I add a System.Collections.ArrayList to a PowerShell custom object? 如何在 powershell (Azure) 中返回对象的一个​​确切属性 - How do I return one exact property of an object in powershell (Azure) PowerShell - 如何将两个自定义对象的属性添加在一起以使用 - PowerShell - How to add the properties of two custom objects together to create a single merged custom object using 如何在 Powershell 中添加自定义数据作为计算属性 - How to add custom data as calculated property in Powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM