Powershell和一对多关系/对象 [英] Powershell and one to many relationships / objects
问题描述
我创建了一个函数,该函数先读取目录列表,然后读取CNC程序名称/编号的文件内容.每个文件都有多个程序号,我用,"分隔,并作为obj传递(请参见下面的代码).我想将其导出为XML,以上传到具有每个程序名称的单独条目的数据库.什么是实现这一目标的最佳/最简便方法?
I have created a function that reads a directory listing and then the files contents for CNC program names/numbers. Each file have multiple program numbers which i have separated by "," and are passed on as a obj (see code below). I want to export this to XML to upload to a database having each of the program names in a seperate entry. Whats the best/easiest method to achieve this?
$obj = New-Object -TypeName PSObject
$obj | Add-Member -MemberType NoteProperty -Name FullFileName -Value $CNCProgFileName
$obj | Add-Member -MemberType NoteProperty -Name ProgramName -Value $ProgramName
推荐答案
像您一样创建自定义对象是很好的.我建议创建一个数组属性,以便将数组中的许多内容关联到一个对象.
Creating a custom object like you are doing is good. I suggest creating a property that is an array so you can associate the many things in the array to one object.
$myListOfStuff = @()
$myListOfStuff += "Stuff"
$myListOfStuff += "More stuff"
$obj = New-Object PsObject @{
FullFileName = $CNCProgFileName
ProgramName = $ProgramName
ListOfStuff = $myListOfStuff
}
您可以使用-ExpandProperty
看到具有Select-Object
的内容列表,如下所示:$obj | select -expand ListOfStuff
.
You can see your list of stuff with Select-Object
with -ExpandProperty
like so: $obj | select -expand ListOfStuff
.
这篇关于Powershell和一对多关系/对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!