Iteration to check if content contains string does not work (PowerShell)

Copper Contributor

I am trying to get names of PS scripts in ci folder on GitHub, pass it out as strings and then check if that script name exists in a list of workflow yaml files.

 

 

 # Get contents of ci folder [Done]
    # pass names of ps1 files to a single array [Done]
    # get contents of .gtihub/workflow and .github/actions folders 
    # Pass File contents to an object/variable 
    # Check if array items are contained in each workflow files and then action files. 
    # if file is found in workflow/action file, output true else output false. 



    param (

    [string]$PAT = '[securestring]', # This token MUST have admin rights
    [string]$OrgName = '[string}'
    )


    $headers = @{ Accept = "application/vnd.github.v3+json"; Authorization = "token $PAT"; sha = ""} 
    


    $Yamls = @()
    $Cis = @()

    #Get contents of workflows files in .github/workflows folder

    $WorkflowFiles = Invoke-RestMethod "https://api.github.com/repos/$($OrgName)/bct-bbti-kpi/contents/.github/workflows" -Method Get -Headers $headers -ErrorAction SilentlyContinue

    foreach($WorkflowFile in $WorkflowFiles) {
    $Yamlfile = Invoke-RestMethod $WorkflowFile.url -Method Get -Headers $headers

    $Yamlfilecontents = [System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String($Yamlfile.content))

    $Yamls+=$Yamlfilecontents

    }

    #Get names of PowerShell Scripts in CI folder 

    $Cicontents = Invoke-RestMethod "https://api.github.com/repos/$($OrgName)/bct-bbti-kpi/contents/ci" -Method Get -Headers $headers -ErrorAction SilentlyContinue

    $cis+=$Cicontents
    Write-Host CI folder contents
    Write-Host ""
    #$ci.name

    foreach($item in $cis.name){

    foreach($WorkflowFile in $WorkflowFiles.name){

    if($Yaml.ToString() -contains $item1.ToString()){

    Write-Host 'True'

    } else {
    Write-Host 'False'
    }
       }
      }

 

 

Please I need help on how to successfully check if each item in $cis.name(as a string) exists in the workflows files inside .github/workflows folder.

0 Replies