skyline-comm.ps1

function genjson {

    add-content $file2 '{ "query" : "'
            add-content $file2 '{'
            add-content $file2 ' activeFindings(limit: 200) {'
            add-content $file2 ' findings {'
            add-content $file2 ' findingId'
            add-content $file2 ' accountId'
            add-content $file2 ' findingDisplayName'
            add-content $file2 ' severity'
            add-content $file2 ' products'
            add-content $file2 ' findingDescription'
            add-content $file2 ' findingImpact'
            add-content $file2 ' recommendations'
            add-content $file2 ' kbLinkURLs'
            add-content $file2 ' recommendationsVCF'
            add-content $file2 ' kbLinkURLsVCF'
            add-content $file2 ' categoryName'
            add-content $file2 ' findingTypes'
            add-content $file2 ' firstObserved'
            add-content $file2 ' totalAffectedObjectsCount'
            add-content $file2 ' }'
            add-content $file2 ' totalRecords'
            add-content $file2 ' timeTaken'
            add-content $file2 ' }'
            add-content $file2 '}'
            add-content $file2 '"}'

}


function getaccesstoken2 {
$Header = @{
 "Accept" = "application/json"
 "Content-Type" = "application/x-www-form-urlencoded"
}

 $file = checkfile
 . $file


$Body = @{
 refresh_token = "$APITOKEN"
}


$MYTOKEN = Invoke-RestMethod -method Post -Uri "$APITOKENSERVER" -Headers $Header -Body $Body

return $MYTOKEN.access_token
}

function send2servicenow {
$SNComment = get-content $fileservicenow 

$params = @{
    Url =  "$SERVICENOWSERVER"
    Credential = $userCred
}
New-ServiceNowSession @params

New-ServiceNowIncident -caller $SERVICENOWUSER -shortdescription "Skyline Findings - List" -comment "$SNComment"

}

function send2jira {
$JIRACONTEXT = get-content $filejira
$filejira2 = "jira.json"

if (-not(Test-Path -Path $filejira2 -PathType Leaf)) {
 try {
  $null = New-Item -ItemType File -Path $filejira2 -Force -ErrorAction Stop
 }
 catch {
  throw $_.Exception.Message
 }
}
 clear-content $filejira2


add-content $filejira2 '{'
add-content $filejira2 ' "fields": {'
add-content $filejira2 ' "project":'
add-content $filejira2 ' {'
add-content $filejira2 ' "key": "SKYLINE"'
add-content $filejira2 ' },'
add-content $filejira2 ' "summary": "Skyline - Findings",'
add-content $filejira2 " `"description`": `"$JIRACONTEXT`" ,"
add-content $filejira2 ' "issuetype": {'
add-content $filejira2 ' "id": "3"'
add-content $filejira2 ' }'
add-content $filejira2 ' }'
add-content $filejira2 '}'

$credjira = get-credential
invoke-restmethod -method post -uri $JIRASERVER -authentication basic -credential $credjira -infile $filejira2 -contenttype "application/json"
}


function confirmX {
 $file = checkfile
 . $file

if (-not($CONFIRM -eq "NO")) 
{
 write-host ""
 write-host "Here are the changes you are about to do:"
 write-host ""
 write-host "Management Host: $VCENTER"
 write-host "Affected Host: $ESX"
 write-host "KB Reference: $KB"
 write-host "Detail: $DETAILX"
 write-host ""
 $confirmation = Read-Host "Do you want to continue (y/n)"
 return $confirmation
} else {
 $confirmation = "y"
 return $confirmation
}
}

function send2slack {
import-csv $fileslack | foreach-object {
 $Body = @{
    channel = $SLACKCHANNEL
    text = "$_"
    color = "#3AA3E3"
    attachment_type = "default"
 }

 $headers = @{Authorization = "Bearer $SLACKTOKEN"}
 Invoke-RestMethod -Method Post -Uri "https://slack.com/api/chat.postMessage" -Headers $headers -Body $body

}
}


function skyline-comm {
[CmdletBinding()] 
param(
 [string]$CHOICE1,
 [string]$CHOICE2,
 [string]$CHOICE3,
 [string]$CHOICE4,
 [string]$CHOICE5
)

switch ( $CHOICE1 )
{

prep {
$file = checkfile
    . $file

    if ($APITOKEN -eq $null)
    {
        $SAMPLEAPITOKEN = select-string -path $file -pattern APITOKEN
        if ( $SAMPLEAPITOKEN.Matches.Count -lt 1)
        {

         if (get-module -listavailable -name servicenow) { 
            write-host "...ServiceNow has already been installed..."    
            }
        else {
            install-module servicenow -force
            write-host "...install ServiceNow..."
            }

            $file2 = "skyline.json"

            add-content $file '#APITOKEN = "NEED-API-TOKEN"'
            add-content $file '#APITOKENSERVER = "https://console.cloud.vmware.com/csp/gateway/am/api/auth/api-tokens/authorize?grant_type=refresh_token"'
            add-content $file '#ACCESSSERVER = "https://skyline.vmware.com/public/api/data"'
            add-content $file '#SLACKTOKEN = "NEED-SLACK-TOKEN"'
            add-content $file '#SLACKCHANNEL = "NEED-SLACK-CHANNEL"'
            add-content $file '#SERVICENOWSERVER = "NEED-SERVICENOW-SERVER"'
            add-content $file '#SERVICENOWUSER = "NEED-SERVICENOW-USER"'
            add-content $file '#JIRASERVER = "NEED-JIRA-SERVER"'

            genjson 

                        write-host ''
            write-host "please update API entries in $file"
            write-host ''
        }
    }
}

get-access-token {
    $MYTOKEN2 = getaccesstoken2
    write-output $MYTOKEN2
}

get-findings {
 switch ($CHOICE2)
 {
 choice { #TESTING ONLY
  write-output "CHOICE1 = $CHOICE1"
  write-output "CHOICE2 = $CHOICE2"
  write-output "CHOICE3 = $CHOICE3"
  write-output "CHOICE4 = $CHOICE4"
  write-output "CHOICE5 = $CHOICE5"
 }

 list {
    $file2 = "skyline.json"
    if (Test-Path $file2) {
      clear-content $file2
    }

            add-content $file2 '{ "query" : "'
            add-content $file2 '{'
            add-content $file2 ' activeFindings(limit: 200) {'
            add-content $file2 ' findings {'
            add-content $file2 ' findingId'
            add-content $file2 ' products'
            add-content $file2 ' totalAffectedObjectsCount'
            add-content $file2 ' }'
            add-content $file2 ' }'
            add-content $file2 '}'
            add-content $file2 '"}'

    $file = checkfile
    . $file
    $MYTOKEN2 = getaccesstoken2
    $FINDINGS = invoke-restmethod -method post -Uri "$ACCESSSERVER" -Headers @{Authorization = "Bearer $MYTOKEN2"} -sessionvariable "SkylineAutomationToolkit" -infile skyline.json -ContentType "application/json"

 switch ($CHOICE3) {
 "send2slack" {

    write-output $FINDINGS.data.activeFindings.findings

    $fileslack = "skyline-slack.csv"
        if (-not(Test-Path -Path $fileslack -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $fileslack -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $fileslack

    $FINDINGS.data.activeFindings.findings | foreach-object {
     add-content $fileslack $_
    }
    send2slack
    
  } 
 
  "send2servicenow" {
    write-output $FINDINGS.data.activeFindings.findings

    $fileservicenow = "skyline-servicenow.csv"
    if (-not(Test-Path -Path $fileservicenow -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $fileservicenow -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $fileservicenow

    $FINDINGS.data.activeFindings.findings | foreach-object {
     add-content $fileservicenow $_
    }
    send2servicenow
   }  

  "send2jira" {
    write-output $FINDINGS.data.activeFindings.findings

    $filejira = "skyline-jira.csv"
    if (-not(Test-Path -Path $filejira -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $filejira -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $filejira

    $FINDINGS.data.activeFindings.findings | foreach-object {
     add-content $filejira $_
    }
    send2jira
   }

  "send2csv" {
    write-output $FINDINGS.data.activeFindings.findings

    $filecsv = "skyline-getfinding-list.csv"
    if (-not(Test-Path -Path $filecsv -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $filecsv -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $filecsv

    $FINDINGS.data.activeFindings.findings | select findingId, @{name="products"; expression={$_.products}}, totalAffectedObjectsCount | export-csv $filecsv -notypeinformation
    
   }

  default {
    write-output $FINDINGS.data.activeFindings.findings
  }
 }

 }

longlist {
    $file2 = "skyline.json"
    if (Test-Path $file2) {
      clear-content $file2
    }

    genjson

    $file = checkfile
    . $file
    $MYTOKEN2 = getaccesstoken2
    $FINDINGS = invoke-restmethod -method post -Uri "$ACCESSSERVER" -Headers @{Authorization = "Bearer $MYTOKEN2"} -sessionvariable "SkylineAutomationToolkit" -infile skyline.json -ContentType "application/json"

 switch ($CHOICE3) {
 "send2slack" {

    write-output $FINDINGS.data.activeFindings.findings

    $fileslack = "skyline-slack.csv"
        if (-not(Test-Path -Path $fileslack -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $fileslack -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $fileslack

    $FINDINGS.data.activeFindings.findings | foreach-object {
     add-content $fileslack $_
    }
    send2slack
    
  } 
 
  "send2servicenow" {
    write-output $FINDINGS.data.activeFindings.findings

    $fileservicenow = "skyline-servicenow.csv"
    if (-not(Test-Path -Path $fileservicenow -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $fileservicenow -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $fileservicenow

    $FINDINGS.data.activeFindings.findings | foreach-object {
     add-content $fileservicenow $_
    }
    send2servicenow
   }  

  "send2jira" {
    write-output $FINDINGS.data.activeFindings.findings

    $filejira = "skyline-jira.csv"
    if (-not(Test-Path -Path $filejira -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $filejira -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $filejira

    $FINDINGS.data.activeFindings.findings | foreach-object {
     add-content $filejira $_
    }
    send2jira
   }

  "send2csv" {
    write-output $FINDINGS.data.activeFindings.findings

    $filecsv = "skyline-getfinding-longlist.csv"
    if (-not(Test-Path -Path $filecsv -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $filecsv -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $filecsv

    $FINDINGS.data.activeFindings.findings  | select findingId, accountId, findingDisplayName, severity, @{name="products"; expression={$_.products}}, findingDescription , findingImpact, @{name="recommendations"; expression={$_.recommendations}},  @{name="kbLinkURLs"; expression={$_.kbLinkURLs}}, @{name="recommendationsVCF"; expression={$_.recommendationsVCF}}, @{name="kbLinkURLsVCF"; expression={$_.kbLinkURLsVCF}}, categoryName, @{name="findingTypes"; expression={$_.findingTypes}}, @{name="firstObserved"; expression={$_.firstObserved}}, totalAffectedObjectsCount | export-csv $filecsv -notypeinformation
    
   }

  default {
    write-output $FINDINGS.data.activeFindings.findings
  }
 }

 }

 detail {

    $file2 = "skyline.json"
    if (Test-Path $file2) {
      clear-content $file2
    }

            add-content $file2 '{ "query" : "'
            add-content $file2 '{'
            add-content $file2 ' activeFindings('
            add-content $file2 ' filter: {'
            add-content $file2 " findingId: `\`"$CHOICE3`\`","
            add-content $file2 " product: `\`"$CHOICE4`\`""
            add-content $file2 ' }'
            add-content $file2 ' limit: 200) {'
            add-content $file2 ' findings {'
            add-content $file2 ' findingId'
            add-content $file2 ' accountId'
            add-content $file2 ' findingDisplayName'
            add-content $file2 ' severity'
            add-content $file2 ' products'
            add-content $file2 ' findingDescription'
            add-content $file2 ' findingImpact'
            add-content $file2 ' recommendations'
            add-content $file2 ' kbLinkURLs'
            add-content $file2 ' recommendationsVCF'
            add-content $file2 ' kbLinkURLsVCF'
            add-content $file2 ' categoryName'
            add-content $file2 ' findingTypes'
            add-content $file2 ' firstObserved'
            add-content $file2 ' totalAffectedObjectsCount'
            add-content $file2 ' affectedObjects(start: 0, limit: 200) {'
            add-content $file2 ' sourceName'
            add-content $file2 ' objectName'
            add-content $file2 ' }'
            add-content $file2 ' }'
            add-content $file2 ' totalRecords'
            add-content $file2 ' timeTaken'
            add-content $file2 ' }'
            add-content $file2 '}'
            add-content $file2 '"}'

    $file = checkfile
    . $file
    $MYTOKEN2 = getaccesstoken2
    $FINDINGS = invoke-restmethod -method post -Uri "$ACCESSSERVER" -Headers @{Authorization = "Bearer $MYTOKEN2"} -sessionvariable "SkylineAutomationToolkit" -infile skyline.json -ContentType "application/json"

 switch ($CHOICE5) {
 "send2slack" {

    write-output write-output $FINDINGS.data.activeFindings.findings.affectedObjects

    $fileslack = "skyline-slack.csv"
        if (-not(Test-Path -Path $fileslack -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $fileslack -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $fileslack

    $FINDINGS.data.activeFindings.findings.affectedObjects | foreach-object {
     add-content $fileslack $_
    }
    send2slack
    
  } 
 
  "send2servicenow" {
    write-output $FINDINGS.data.activeFindings.findings.affectedObjects

    $fileservicenow = "skyline-servicenow.csv"
    if (-not(Test-Path -Path $fileservicenow -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $fileservicenow -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $fileservicenow

    $FINDINGS.data.activeFindings.findings.affectedObjects | foreach-object {
     add-content $fileservicenow $_
    }
    send2servicenow
   }  

  "send2jira" {
    write-output $FINDINGS.data.activeFindings.findings

    $filejira = "skyline-jira.csv"
    if (-not(Test-Path -Path $filejira -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $filejira -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $filejira

    $FINDINGS.data.activeFindings.findings.affectedObjects | foreach-object {
     add-content $filejira $_
    }
    send2jira
   }


  "send2csv" {
    write-output $FINDINGS.data.activeFindings.findings.affectedObjects

    $filecsv = "skyline-getfinding-details.csv"
    if (-not(Test-Path -Path $filecsv -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $filecsv -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $filecsv

    $FINDINGS.data.activeFindings.findings.affectedObjects | select sourceName, objectName | export-csv $filecsv -notypeinformation
    
   }


  default {
    write-output $FINDINGS.data.activeFindings.findings.affectedObjects
  }
 }

 }

longdetail {

    $file2 = "skyline.json"
    if (Test-Path $file2) {
      clear-content $file2
    }

            add-content $file2 '{ "query" : "'
            add-content $file2 '{'
            add-content $file2 ' activeFindings('
            add-content $file2 ' filter: {'
            add-content $file2 " findingId: `\`"$CHOICE3`\`","
            add-content $file2 " product: `\`"$CHOICE4`\`""
            add-content $file2 ' }'
            add-content $file2 ' limit: 200) {'
            add-content $file2 ' findings {'
            add-content $file2 ' findingId'
            add-content $file2 ' accountId'
            add-content $file2 ' findingDisplayName'
            add-content $file2 ' severity'
            add-content $file2 ' products'
            add-content $file2 ' findingDescription'
            add-content $file2 ' findingImpact'
            add-content $file2 ' recommendations'
            add-content $file2 ' kbLinkURLs'
            add-content $file2 ' recommendationsVCF'
            add-content $file2 ' kbLinkURLsVCF'
            add-content $file2 ' categoryName'
            add-content $file2 ' findingTypes'
            add-content $file2 ' firstObserved'
            add-content $file2 ' totalAffectedObjectsCount'
            add-content $file2 ' affectedObjects(start: 0, limit: 200) {'
            add-content $file2 ' sourceName'
            add-content $file2 ' objectName'
            add-content $file2 ' }'
            add-content $file2 ' }'
            add-content $file2 ' totalRecords'
            add-content $file2 ' timeTaken'
            add-content $file2 ' }'
            add-content $file2 '}'
            add-content $file2 '"}'

    $file = checkfile
    . $file
    $MYTOKEN2 = getaccesstoken2
    $FINDINGS = invoke-restmethod -method post -Uri "$ACCESSSERVER" -Headers @{Authorization = "Bearer $MYTOKEN2"} -sessionvariable "SkylineAutomationToolkit" -infile skyline.json -ContentType "application/json"

 switch ($CHOICE5) {
 "send2slack" {

    write-output write-output $FINDINGS.data.activeFindings.findings.affectedObjects

    $fileslack = "skyline-slack.csv"
        if (-not(Test-Path -Path $fileslack -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $fileslack -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $fileslack

    $FINDINGS.data.activeFindings.findings.affectedObjects | foreach-object {
     add-content $fileslack $_
    }
    send2slack
    
  } 
 
  "send2servicenow" {
    write-output $FINDINGS.data.activeFindings.findings.affectedObjects

    $fileservicenow = "skyline-servicenow.csv"
    if (-not(Test-Path -Path $fileservicenow -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $fileservicenow -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $fileservicenow

    $FINDINGS.data.activeFindings.findings.affectedObjects | foreach-object {
     add-content $fileservicenow $_
    }
    send2servicenow
   }  

  "send2jira" {
    write-output $FINDINGS.data.activeFindings.findings

    $filejira = "skyline-jira.csv"
    if (-not(Test-Path -Path $filejira -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $filejira -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $filejira

    $FINDINGS.data.activeFindings.findings.affectedObjects | foreach-object {
     add-content $filejira $_
    }
    send2jira
   }


  "send2csv" {
    write-output $FINDINGS.data.activeFindings.findings

    $filecsv = "skyline-getfinding-details.csv"
    if (-not(Test-Path -Path $filecsv -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $filecsv -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $filecsv

    $FINDINGS.data.activeFindings.findings.affectedObjects | foreach-object {
     $sourceX = $_.sourceName
     $objectX = $_.objectName
    
    $FINDINGS.data.activeFindings.findings | select findingId, accountId, findingDisplayName, severity, @{name="products"; expression={$_.products}}, findingDescription, findingImpact, @{name="recommendations"; expression={$_.recommendations}}, @{name="kbLinkURLs"; expression={$_.kbLinkURLs}}, @{name="recommendationsVCF"; expression={$_.recommendationsVCF}}, @{name="kbLinkURLsVCF"; expression={$_.kbLinkURLsVCF}}, categoryName, @{name="findingTypes"; expression={$_.findingTypes}}, @{name="firstObserved"; expression={$_.firstObserved}}, totalAffectedObjectsCount, @{name="affectedObjects.sourceName"; expression={$sourceX}}, @{name="affectedObjects.objectName"; expression={$objectX}} | export-csv $filecsv -notypeinformation -append
    
    }

    
   }


  default {
    #write-output $FINDINGS.data.activeFindings.findings.affectedObjects
    write-output $FINDINGS.data.activeFindings.findings
  }
 }

 }

  category {
       $file2 = "skyline.json"
    if (Test-Path $file2) {
      clear-content $file2
    }

            add-content $file2 '{ "query" : "'
            add-content $file2 '{'
            add-content $file2 ' activeFindings('
            add-content $file2 ' filter: {'
            add-content $file2 " categoryName: [$CHOICE3]" 
            add-content $file2 ' }'
            add-content $file2 ' limit: 200)'
            add-content $file2 '{'
            add-content $file2 ' findings {'
            add-content $file2 ' findingId'
            add-content $file2 ' products'
            add-content $file2 ' totalAffectedObjectsCount'
            add-content $file2 ' }'
            add-content $file2 ' }'
            add-content $file2 '}'
            add-content $file2 '"}'


    $file = checkfile
    . $file
    $MYTOKEN2 = getaccesstoken2
    $FINDINGS = invoke-restmethod -method post -Uri "$ACCESSSERVER" -Headers @{Authorization = "Bearer $MYTOKEN2"} -sessionvariable "SkylineAutomationToolkit" -infile skyline.json -ContentType "application/json"


 switch ($CHOICE4) {
 "send2slack" {

    write-output $FINDINGS.data.activeFindings.findings

    $fileslack = "skyline-slack.csv"
        if (-not(Test-Path -Path $fileslack -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $fileslack -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $fileslack

    $FINDINGS.data.activeFindings.findings | foreach-object {
     add-content $fileslack $_
    }
    send2slack
    
  } 
 
  "send2servicenow" {
    write-output $FINDINGS.data.activeFindings.findings

    $fileservicenow = "skyline-servicenow.csv"
    if (-not(Test-Path -Path $fileservicenow -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $fileservicenow -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $fileservicenow

    $FINDINGS.data.activeFindings.findings | foreach-object {
     add-content $fileservicenow $_
    }
    send2servicenow
   }  

  "send2jira" {
    write-output $FINDINGS.data.activeFindings.findings

    $filejira = "skyline-jira.csv"
    if (-not(Test-Path -Path $filejira -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $filejira -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $filejira

    $FINDINGS.data.activeFindings.findings | foreach-object {
     add-content $filejira $_
    }
    send2jira
   }


  "send2csv" {
    write-output $FINDINGS.data.activeFindings.findings

    $filecsv = "skyline-getfinding-category.csv"
    if (-not(Test-Path -Path $filecsv -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $filecsv -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $filecsv

    $FINDINGS.data.activeFindings.findings | select findingId, @{name="products"; expression={$_.products}}, totalAffectedObjectsCount | export-csv $filecsv -notypeinformation
    
   }

  default {
    write-output $FINDINGS.data.activeFindings.findings
  }
 }

 }
 type {
       $file2 = "skyline.json"
    if (Test-Path $file2) {
      clear-content $file2
    }

            add-content $file2 '{ "query" : "'
            add-content $file2 '{'
            add-content $file2 ' activeFindings('
            add-content $file2 ' filter: {'
            add-content $file2 " findingTypes: [$CHOICE3]" 
            add-content $file2 ' }'
            add-content $file2 ' limit: 200)'
            add-content $file2 '{'
            add-content $file2 ' findings {'
            add-content $file2 ' findingId'
            add-content $file2 ' products'
            add-content $file2 ' totalAffectedObjectsCount'
            add-content $file2 ' }'
            add-content $file2 ' }'
            add-content $file2 '}'
            add-content $file2 '"}'


    $file = checkfile
    . $file
    $MYTOKEN2 = getaccesstoken2
    $FINDINGS = invoke-restmethod -method post -Uri "$ACCESSSERVER" -Headers @{Authorization = "Bearer $MYTOKEN2"} -sessionvariable "SkylineAutomationToolkit" -infile skyline.json -ContentType "application/json"
     

switch ($CHOICE4) {
 "send2slack" {

    write-output $FINDINGS.data.activeFindings.findings

    $fileslack = "skyline-slack.csv"
        if (-not(Test-Path -Path $fileslack -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $fileslack -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $fileslack

    $FINDINGS.data.activeFindings.findings | foreach-object {
     add-content $fileslack $_
    }
    send2slack
    
  } 
 
  "send2servicenow" {
    write-output $FINDINGS.data.activeFindings.findings

    $fileservicenow = "skyline-servicenow.csv"
    if (-not(Test-Path -Path $fileservicenow -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $fileservicenow -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $fileservicenow

    $FINDINGS.data.activeFindings.findings | foreach-object {
     add-content $fileservicenow $_
    }
    send2servicenow
   }  

  "send2jira" {
    write-output $FINDINGS.data.activeFindings.findings

    $filejira = "skyline-jira.csv"
    if (-not(Test-Path -Path $filejira -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $filejira -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $filejira

    $FINDINGS.data.activeFindings.findings | foreach-object {
     add-content $filejira $_
    }
    send2jira
   }

  "send2csv" {
    write-output $FINDINGS.data.activeFindings.findings

    $filecsv = "skyline-getfinding-type.csv"
    if (-not(Test-Path -Path $filecsv -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $filecsv -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $filecsv

    $FINDINGS.data.activeFindings.findings | select findingId, @{name="products"; expression={$_.products}}, totalAffectedObjectsCount | export-csv $filecsv -notypeinformation
    
   }

  default {
    write-output $FINDINGS.data.activeFindings.findings
  }
 }

 }
severity {
       $file2 = "skyline.json"
    if (Test-Path $file2) {
      clear-content $file2
    }

            add-content $file2 '{ "query" : "'
            add-content $file2 '{'
            add-content $file2 ' activeFindings('
            add-content $file2 ' filter: {'
            add-content $file2 " severity: [$CHOICE3]" 
            add-content $file2 ' }'
            add-content $file2 ' limit: 200)'
            add-content $file2 '{'
            add-content $file2 ' findings {'
            add-content $file2 ' findingId'
            add-content $file2 ' products'
            add-content $file2 ' totalAffectedObjectsCount'
            add-content $file2 ' }'
            add-content $file2 ' }'
            add-content $file2 '}'
            add-content $file2 '"}'


    $file = checkfile
    . $file
    $MYTOKEN2 = getaccesstoken2
    $FINDINGS = invoke-restmethod -method post -Uri "$ACCESSSERVER" -Headers @{Authorization = "Bearer $MYTOKEN2"} -sessionvariable "SkylineAutomationToolkit" -infile skyline.json -ContentType "application/json"

switch ($CHOICE4) {
 "send2slack" {

    write-output $FINDINGS.data.activeFindings.findings

    $fileslack = "skyline-slack.csv"
        if (-not(Test-Path -Path $fileslack -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $fileslack -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $fileslack

    $FINDINGS.data.activeFindings.findings | foreach-object {
     add-content $fileslack $_
    }
    send2slack
    
  } 
 
  "send2servicenow" {
    write-output $FINDINGS.data.activeFindings.findings

    $fileservicenow = "skyline-servicenow.csv"
    if (-not(Test-Path -Path $fileservicenow -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $fileservicenow -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $fileservicenow

    $FINDINGS.data.activeFindings.findings | foreach-object {
     add-content $fileservicenow $_
    }
    send2servicenow
   }  

  "send2jira" {
    write-output $FINDINGS.data.activeFindings.findings

    $filejira = "skyline-jira.csv"
    if (-not(Test-Path -Path $filejira -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $filejira -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $filejira

    $FINDINGS.data.activeFindings.findings | foreach-object {
     add-content $filejira $_
    }
    send2jira
   }

  "send2csv" {
    write-output $FINDINGS.data.activeFindings.findings

    $filecsv = "skyline-getfinding-severity.csv"
    if (-not(Test-Path -Path $filecsv -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $filecsv -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $filecsv

    $FINDINGS.data.activeFindings.findings | select findingId, @{name="products"; expression={$_.products}}, totalAffectedObjectsCount | export-csv $filecsv -notypeinformation
    
   }

  default {
    write-output $FINDINGS.data.activeFindings.findings
  }
 }

 }
  custom {
    $file = checkfile
    . $file
    $MYTOKEN2 = getaccesstoken2
    $FINDINGS = invoke-restmethod -method post -Uri "$ACCESSSERVER" -Headers @{Authorization = "Bearer $MYTOKEN2"} -sessionvariable "SkylineAutomationToolkit" -infile skyline.json -ContentType "application/json"


switch ($CHOICE3) {
 "send2slack" {

    write-output $FINDINGS.data.activeFindings.findings

    $fileslack = "skyline-slack.csv"
        if (-not(Test-Path -Path $fileslack -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $fileslack -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $fileslack

    $FINDINGS.data.activeFindings.findings | foreach-object {
     add-content $fileslack $_
    }
    send2slack
    
  } 
 
  "send2servicenow" {
    write-output $FINDINGS.data.activeFindings.findings

    $fileservicenow = "skyline-servicenow.csv"
    if (-not(Test-Path -Path $fileservicenow -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $fileservicenow -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $fileservicenow

    $FINDINGS.data.activeFindings.findings | foreach-object {
     add-content $fileservicenow $_
    }
    send2servicenow
   }  

  "send2jira" {
    write-output $FINDINGS.data.activeFindings.findings

    $filejira = "skyline-jira.csv"
    if (-not(Test-Path -Path $filejira -PathType Leaf)) {
         try {
             $null = New-Item -ItemType File -Path $filejira -Force -ErrorAction Stop
         }
         catch {
             throw $_.Exception.Message
         }
    }
    clear-content $filejira

    $FINDINGS.data.activeFindings.findings | foreach-object {
     add-content $filejira $_
    }
    send2jira
   }

  default {
    write-output $FINDINGS.data.activeFindings.findings
  }
 }


 }
default { 
    ''
    'USAGE: skyline-comm get-findings VARIABLE' 
    ' (arg1): [get-findings list] [send2slack|send2servicenow|send2jira|send2csv]'
    ' (arg1): [get-findings longlist] [send2slack|send2servicenow|send2jira|send2csv]'
    ' (arg3): [get-findings detail findingid source] [send2slack|send2servicenow|send2jira|send2csv]'
    ' (arg4): [get-findings longdetail findingid source] [send2slack|send2servicenow|send2jira|send2csv]'
    ' (arg5): [get-findings custom] [send2slack|send2servicenow|send2jira]'
    ' (arg6): [get-findings category SECURITY|NETWORK|COMPUTE|STORAGE] [send2slack|send2servicenow|send2jira|send2csv]'
    ' (arg7): [get-findings type CONFIGURATION|UPGRADE] [send2slack|send2servicenow|send2jira|send2csv]'
    ' (arg8): [get-findings severity CRITICAL|MODERATE|TRIVIAL] [send2slack|send2servicenow|send2jira|send2csv]'
    ''
     createsource
    }
 }
}

default { 
    ''
    'USAGE: skyline-comm ARG VARIABLE' 
    ' (arg1): [prep]'
    ' (arg2): [get-access-token]'
    ' (arg3): [get-findings list] [send2slack|send2servicenow|send2jira|send2csv]'
    ' (arg4): [get-findings longlist] [send2slack|send2servicenow|send2jira|send2csv]'
    ' (arg5): [get-findings detail findingid source] [send2slack|send2servicenow|send2jira|send2csv]'
    ' (arg5): [get-findings longdetail findingid source] [send2slack|send2servicenow|send2jira|send2csv]'
    ' (arg6): [get-findings custom] [send2slack|send2servicenow|send2jira]'
    ' (arg7): [get-findings category SECURITY|NETWORK|COMPUTE|STORAGE] [send2slack|send2servicenow|send2jira|send2csv]'
    ' (arg8): [get-findings type CONFIGURATION|UPGRADE] [send2slack|send2servicenow|send2jira|send2csv]'
    ' (arg9): [get-findings severity CRITICAL|MODERATE|TRIVIAL] [send2slack|send2servicenow|send2jira|send2csv]'
    ''
     createsource
    }
}
} #skyline-comm