powershell脚本将csv单元格中的数据拆分为第一列到多行的行

时间:2018-01-29 01:08:36

标签: powershell

创建了一个powershell脚本,用于从xml文件中提取所需数据(excel中的实际数据中的图像到excel中的Desired输出)。希望进一步自定义第一列到第一列的每一行中的数据并复制相应的col 2,3,4的相同值。

寻找使用powershell执行以下步骤的方法

Powershell脚本,用于将数据从XML文件获取为CSV

$XmlInfo = @()

更改文件路径

[xml] $ XmlDocument = Get-Content -Path Test.xml

$ domainids = $ XmlDocument.XPS.Policy | Where-Object {$ _。class -eq“CA.SM :::Domain”}   foreach($ domainid中的$ domainid)   {

$OIDs = ($domainid.'#comment' | Select-String CA.SM::Realm | ForEach-Object {$_ -replace 'Xid="CA.SM::Realm@',''} | Out-String).Trim()
foreach($OID in $OIDs)
  { 
      $obj = New-Object PSObject
      $obj | Add-Member -MemberType NoteProperty -Name OID -Value ($Relam ID -replace '"','')
      $obj | Add-Member -MemberType NoteProperty -Name Domain OID -Value ($domainid.Xid | ForEach-Object {$_ -replace 'CA.SM::Domain@',''} )
      $obj | Add-Member -MemberType NoteProperty -Name Name -Value ($domainid.Property.get(($temp2.Property.name.IndexOf('CA.SM::Domain.Name')))).StringValue
      $obj | Add-Member -MemberType NoteProperty -Name Description -Value ($domainid.Property.get(($temp2.Property.name.IndexOf('CA.SM::Domain.Desc')))).StringValue
      $XmlInfo += $obj
  }

}

$ XmlInfo | Export-Csv output.csv -NoTypeInformation

1 个答案:

答案 0 :(得分:0)

不确定数据的来源,但这是一种可以执行此操作的方法:

# Get sample data into script
$inputdata = @'
OID,Domain ID,Name,Description
01-123456-7890AB-CDEF 02-123456-7890AB-CDEF 03-123456-7890AB-CDEF 04-123456-7890AB-CDEF,05-111111-22222-3333-4444,Singapore,* Please do not edit this domain!
06-123456-7890AB-CDEF 07-123456-7890AB-CDEF 08-123456-7890AB-CDEF,10-111111-22222-3333-4444,Test,* Please do not edit this domain!
'@ | ConvertFrom-Csv

# Show the input data in table format at the powershell prompt
$inputdata | ft -AutoSize

# Initialize $outputdata as array
$outputdata = @()

# Loop through input rows
foreach ($inputrow in $inputdata) {

    # Split line OIDs with "space" as delimiter. Change below as required.
    # For Example, if newline character then replace " " with "`n". If newline then space then "`n " etc.
    $inputOIDs = $inputrow.OID -split " "
    $inputOID = $inputOIDs[0]

    # Initialize group of output rows as array
    $outputrows = @()

    # Loop through each OID in an input line
    foreach ($inputOID in $inputOIDs) {

        # Initialize the output row with desired properties
        $outputrow = "" | select OID,'Domain ID',Name,Description

        # Set the data of the output row
        $outputrow.OID = $inputOID
        $outputrow.'Domain ID' = $inputrow.'Domain ID'
        $outputrow.Name = $inputrow.Name
        $outputrow.Description = $inputrow.Description

        # Add the output row to the group of output rows
    $outputrows += $outputrow
    }

    # Add the group of output rows to the output data
    $outputdata += $outputrows
}

# Show the output data in table format at the powershell prompt
$outputdata | ft -AutoSize

# Show the output data in CSV format at the powershell prompt
$outputdata | ConvertTo-Csv -NoTypeInformation

# Export data to a CSV file: uncomment & replace "your path here"
# $outputdata | Export-Csv -NoTypeInformation -Path "your path here"

这将返回:

OID                                                                                     Domain ID                 Name      Description                      
---                                                                                     ---------                 ----      -----------                      
01-123456-7890AB-CDEF 02-123456-7890AB-CDEF 03-123456-7890AB-CDEF 04-123456-7890AB-CDEF 05-111111-22222-3333-4444 Singapore * Please do not edit this domain!
06-123456-7890AB-CDEF 07-123456-7890AB-CDEF 08-123456-7890AB-CDEF                       10-111111-22222-3333-4444 Test      * Please do not edit this domain!

OID                   Domain ID                 Name      Description                      
---                   ---------                 ----      -----------                      
01-123456-7890AB-CDEF 05-111111-22222-3333-4444 Singapore * Please do not edit this domain!
02-123456-7890AB-CDEF 05-111111-22222-3333-4444 Singapore * Please do not edit this domain!
03-123456-7890AB-CDEF 05-111111-22222-3333-4444 Singapore * Please do not edit this domain!
04-123456-7890AB-CDEF 05-111111-22222-3333-4444 Singapore * Please do not edit this domain!
06-123456-7890AB-CDEF 10-111111-22222-3333-4444 Test      * Please do not edit this domain!
07-123456-7890AB-CDEF 10-111111-22222-3333-4444 Test      * Please do not edit this domain!
08-123456-7890AB-CDEF 10-111111-22222-3333-4444 Test      * Please do not edit this domain!

"OID","Domain ID","Name","Description"
"01-123456-7890AB-CDEF","05-111111-22222-3333-4444","Singapore","* Please do not edit this domain!"
"02-123456-7890AB-CDEF","05-111111-22222-3333-4444","Singapore","* Please do not edit this domain!"
"03-123456-7890AB-CDEF","05-111111-22222-3333-4444","Singapore","* Please do not edit this domain!"
"04-123456-7890AB-CDEF","05-111111-22222-3333-4444","Singapore","* Please do not edit this domain!"
"06-123456-7890AB-CDEF","10-111111-22222-3333-4444","Test","* Please do not edit this domain!"
"07-123456-7890AB-CDEF","10-111111-22222-3333-4444","Test","* Please do not edit this domain!"
"08-123456-7890AB-CDEF","10-111111-22222-3333-4444","Test","* Please do not edit this domain!"

这是你在找什么?

此致 理查德