Powershell and one to many relationships / objects

谁说我不能喝 提交于 2019-12-13 04:14:05

问题


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

回答1:


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
}

You can see your list of stuff with Select-Object with -ExpandProperty like so: $obj | select -expand ListOfStuff.




回答2:


In PowreShell 2.0 you can use the ConvertTo-Xml cmdlet to produce XML from your objects.



来源:https://stackoverflow.com/questions/9198622/powershell-and-one-to-many-relationships-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!