【问题标题】:Powershell: Arrays index acess and check if previous is substring to nextPowershell:数组索引访问并检查上一个是否是下一个的子字符串
【发布时间】:2021-09-21 15:19:09
【问题描述】:

我需要将作业链接到子作业:作业是这种格式,例如 ACGN100Q,而附加的子作业是顺序的并且格式如下:ACGN-100Q-000T;ACGN-100Q-010T;ACGN-100Q -020T;ACGN-100Q-030T

在我的 csv 文件中,此作业 ACGN100Q 的类型是“TechnologyInteraction”,而子作业的类型是“TechnologyService”。我正在开发一个脚本,例如允许我说 ACGN-100Q-000T 和 ACGN-100Q-010T 之间的链接是“TrigerringRelation”类型,而 ACGN100Q 和 ACGN-100Q-000T 之间的链接是“RealizationRelation”类型.我需要帮助,因为我无法建立链接。

这是我的起始 csv 文件: 新元素.csv

ID,"Type","Name","Documentation"
eb214110-2b6a-48b2-ba5a-7c13dc3bba39,"TechnologyInteraction","ACGN100Q","Begin Of JobStream"
a46681e7-19a8-4fc5-b747-09679c15ff26,"TechnologyService","ACGN-100Q-000T","Transfert UDM (xACGN000)"
85761a09-1145-4037-a527-66a743def45f,"TechnologyService","ACGN-100Q-010T","move fichier REF to work"
27b126fb-c708-427d-b0a6-ce4747114ac4,"TechnologyService","ACGN-100Q-020T","w_read_account"
bb0c5e42-5fad-4bd9-8ee9-f41d0b824e82,"TechnologyService","ACGN-100Q-030T","w_read_referential"
0b8b76e3-62fa-4527-9f05-2eb4dbaa8e97,"TechnologyService","ACGN-100Q-040T","w_load_CompanyGroup"
1f487986-3cac-4af8-bda2-6400a1c71f48,"TechnologyService","ACGN-100Q-050T","w_load_Company"

我想得到一个如下所示的文件:
关系.csv

"ID","Type","Name","Documentation","Source","Target"
"New ID","RealizationRelationship","","","eb214110-2b6a-48b2-ba5a-7c13dc3bba39","a46681e7-19a8-4fc5-b747-09679c15ff26"
"New ID","TriggeringRelationship","","","a46681e7-19a8-4fc5-b747-09679c15ff26","85761a09-1145-4037-a527-66a743def45f"
"New ID","TriggeringRelationship","","","85761a09-1145-4037-a527-66a743def45f","27b126fb-c708-427d-b0a6-ce4747114ac4"
"New ID","TriggeringRelationship","","","27b126fb-c708-427d-b0a6-ce4747114ac4","bb0c5e42-5fad-4bd9-8ee9-f41d0b824e82"
"New ID","TriggeringRelationship","","","bb0c5e42-5fad-4bd9-8ee9-f41d0b824e82","0b8b76e3-62fa-4527-9f05-2eb4dbaa8e97"
"New ID","TriggeringRelationship","","","0b8b76e3-62fa-4527-9f05-2eb4dbaa8e97","1f487986-3cac-4af8-bda2-6400a1c71f48"

$result= @()

function linkedRelationCsvToElementsCsv{
    #relations.csv headers are ID,"Type","Name","Documentation","Source","Target"
      $Type=@()
      $Name=@()
      $ID=@()
      $Documentation=@()
    #$pattern="^(WEBX|DWHS|COGN|CLOT|CLAI|BTRE|BISI|BDDO|ARXL|AGSO|AGPC|ACTO|FNET|ARX|AGS|INF|CLA|MEM|SWA|REX)-"
    $newElementsCsv=Import-CSV $env:USERPROFILE\Desktop\Archi\newElements.csv |sort ID,"Type","Name","Documentation"  -Unique
    # Check if type is TechnologyInteraction or TechnologyService and link :TechnologyService to TechnologyInteraction and TechnologyInteraction to TWS id's
    ForEach ($line in  $newElementsCsv){
     $Type += $line.Type
      $Name += $line.Name
      $ID +=$line.ID
      $Documentation += $_.Documentation
        #Search for element type in elements.csv
       for( $index=0; $index -le  $Name.length-1; $index++){

        if($Type[$index] -eq 'TechnologyInteraction'  -or $Type[$index] -eq 'TechnologyEvent' ){
        Write-Warning "Case type TechnologyInteraction founded, new type of RealizationRelationship created  "
    # if the job is of type "TechnologyInteraction" or "TechnologyEvent", we link it to the TWS id's(TechnologyCollaboration,ef2f510b-924b-439d-8720-0183c7294eb3) in archi.
        $newArrayResult= New-Object PsObject -Property @{ID=[guid]::NewGuid().ToString(); "Type"="RealizationRelationship"; "Name"=$Name[$index]; "Documentation"=$Documentation[$index]; "Source"="ef2f510b-924b-439d-8720-0183c7294eb3"; "Target"=$ID[$index]}
            $result = $result + $newArrayResult
    }elseif ($Type[$index][0].Type -eq 'TechnologyService'  -and$Type[$index][1].Type -eq 'TechnologyService' ){
    Write-Warning "Case type TechnologyService founded, new type of TriggeringRelationship created  "
        $newArrayResult2 = New-Object PsObject -Property @{ID=[guid]::NewGuid().ToString(); "Type"="TriggeringRelationship"; "Name"=""; "Documentation"=""; "Source"=$line[$index][0].ID; "Target"=$line[$index][1].ID}
        $result = $result + $newArrayResult2
    }
    }
    
    }
    $result |Select-Object -Property ID,"Type","Name","Documentation","Source","Target"| Export-Csv $env:USERPROFILE\Desktop\Archi\relation.csv -NoTypeInformation -Encoding UTF8 
  
}linkedRelationCsvToElementsCsv # Call the function

> the elseIf() not return value.

Thanks you in advance.

【问题讨论】:

    标签: powershell csv


    【解决方案1】:

    下面的代码完全根据您给出的输入生成您想要的输出。可能会出现意外的边缘情况,因此您应该编写一些测试(例如使用 Pester)以确认它在这些边缘情况下的行为方式符合您的要求。

    关键是任何行的输出都包括前一行的 ID 以及当前行,因此我们在 foreach 循环期间将前一行保存在一个变量中,以便在处理下一行时可以检查它, 输出中的 Type 只取决于上一行的 Type。

    请注意,我还将与 csv 的转换/从 csv 移出 main 函数,因此更容易单独对函数进行单元测试。

    function ConvertTo-ElementItems
    {
    
        param
        (
            [object[]] $Relations
        )
    
        $jobTypes = @( "TechnologyInteraction", "TechnologyEvent" );
        $subTypes = @( "TechnologyService" );
    
        $previousItem = $null;
    
        foreach( $item in $Relations )
        {
    
            if( $item.Type -in $jobTypes )
            {
                # start of a new job, but don't output anything
            }
            elseif( $item.Type -notin $subTypes  )
            {
                # not a subjob type that we recognise
                throw "unrecognised subjob type '$($item.Type)' for subjob '$($item.ID)'";
            }
            elseif( $null -eq $previousItem )
            {
                # we've got a subjob, but there was no previous job or subjob
                throw "no preceding item for subjob '$($item.ID)'";
            }
            elseif( $previousItem.Type -in $jobTypes )
            {
                # this is the first subjob after the parent job
                new-object PSCustomObject -Property ([ordered] @{
                    "ID"            = "New ID"
                    "Type"          = "RealizationRelationship"
                    "Name"          = "";
                    "Documentation" = ""
                    "Source"        = $previousItem.ID
                    "Target"        = $item.ID
                });
            }
            else
            {
                # the preceding item was a subjob as well
                new-object PSCustomObject -Property ([ordered] @{
                    "ID"            = "New ID"
                    "Type"          = "TriggeringRelationship"
                    "Name"          = ""
                    "Documentation" = ""
                    "Source"        = $previousItem.ID
                    "Target"        = $item.ID
                });
            }
    
            $previousItem = $item;
    
        }
      
    }
    

    下面是一个使用函数的例子:

    $ErrorActionPreference = "Stop";
    Set-StrictMode -Version "Latest";
    
    $inputCsv = @"
    ID,"Type","Name","Documentation"
    eb214110-2b6a-48b2-ba5a-7c13dc3bba39,"TechnologyInteraction","ACGN100Q","Begin Of JobStream"
    a46681e7-19a8-4fc5-b747-09679c15ff26,"TechnologyService","ACGN-100Q-000T","Transfert UDM (xACGN000)"
    85761a09-1145-4037-a527-66a743def45f,"TechnologyService","ACGN-100Q-010T","move fichier REF to work"
    27b126fb-c708-427d-b0a6-ce4747114ac4,"TechnologyService","ACGN-100Q-020T","w_read_account"
    bb0c5e42-5fad-4bd9-8ee9-f41d0b824e82,"TechnologyService","ACGN-100Q-030T","w_read_referential"
    0b8b76e3-62fa-4527-9f05-2eb4dbaa8e97,"TechnologyService","ACGN-100Q-040T","w_load_CompanyGroup"
    1f487986-3cac-4af8-bda2-6400a1c71f48,"TechnologyService","ACGN-100Q-050T","w_load_Company"
    "@
    
    $expectedCsv = @"
    "ID","Type","Name","Documentation","Source","Target"
    "New ID","RealizationRelationship","","","eb214110-2b6a-48b2-ba5a-7c13dc3bba39","a46681e7-19a8-4fc5-b747-09679c15ff26"
    "New ID","TriggeringRelationship","","","a46681e7-19a8-4fc5-b747-09679c15ff26","85761a09-1145-4037-a527-66a743def45f"
    "New ID","TriggeringRelationship","","","85761a09-1145-4037-a527-66a743def45f","27b126fb-c708-427d-b0a6-ce4747114ac4"
    "New ID","TriggeringRelationship","","","27b126fb-c708-427d-b0a6-ce4747114ac4","bb0c5e42-5fad-4bd9-8ee9-f41d0b824e82"
    "New ID","TriggeringRelationship","","","bb0c5e42-5fad-4bd9-8ee9-f41d0b824e82","0b8b76e3-62fa-4527-9f05-2eb4dbaa8e97"
    "New ID","TriggeringRelationship","","","0b8b76e3-62fa-4527-9f05-2eb4dbaa8e97","1f487986-3cac-4af8-bda2-6400a1c71f48"
    "@;
    
    $relations = $inputCsv | ConvertFrom-Csv;
    $elements = ConvertTo-ElementItems -Relations $relations;
    $outputCsv = ($elements | ConvertTo-Csv -NoTypeInformation) -join "`n";
    
    if( $outputCsv -ne $expectedCsv )
    {
        throw "output csv doesn't match expected csv";
    } else {
        write-host "output csv matches expected csv";
    }
    

    【讨论】:

    • 非常感谢您的帮助,我会尽快回复您。我正在学习工作 powershell。
    • 谢谢脚本与我想要的一致,但是作业和子作业不以相同的模式开始,例如,如果作业以这种模式(AGPC)开始,则附加到它的子作业必须以这种模式^(AGPC)-。请问您知道如何进行吗?
    猜你喜欢
    • 2021-03-15
    • 2019-05-11
    • 1970-01-01
    • 2022-07-01
    • 2013-05-12
    • 2017-06-17
    • 2016-05-24
    • 2011-02-07
    相关资源
    最近更新 更多