Assistance with modifying existing (working) Powershell script to add totals for Users found

Copper Contributor

Hello Forum,

 

I have a working existing PowerShell script.  I'm looking to modify this script whereby two Users are searched for:

PROD

DEV

Where the resulting report would show a total of records found for each of the two above users.

 

Currently we invoke the script using:

 ./logDeploy.ps1 -User Prod

or

./logDeploy.ps1 -User Dev 

 

But we would like to run the script and have it automatically

 

Any assistance that can be provided would be greatly appreciated.

 

Thank you.

 

 

 

param(
    
    [string][parameter(Mandatory=$false, HelpMessage='Event Type filter: ALL, node, cbook, client, policy, dbag')]$EventType = "ALL",
    [string][parameter(Mandatory=$false, HelpMessage='User ID filter')]$User = "ALL",
    [string][parameter(Mandatory=$false, HelpMessage='Task Type filter: ALL, delete, create, update')]$Task = "ALL", 
    [string][parameter(Mandatory=$false, HelpMessage='Start time in format MM/dd/yyyy HH:mm:ss')]$StartTime = "Default",
    [string][parameter(Mandatory=$false, HelpMessage='End Time in format MM/dd/yyyy HH:mm:ss')]$EndTime = "Default"
)

Write-Host "============================Deployment Log ============================"
$Script:Token="xxxxxxxxxxxx"
function InvokeA2APIReq() {
    param(
        [string][parameter(Mandatory=$True, HelpMessage='REST Method')]$Method,
        [string][parameter(Mandatory=$True, HelpMessage='A2 Endpoint')]$Endpoint,
        [parameter(Mandatory=$False, HelpMessage='Body as string')]$Body
    )
 
    $APIPath = "https://company.com/api/v0"
    
    # Set TLS to 1.2
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
 
    if ($Method -match "get")
    {
        $header = @{"api-token" = "$Token"}
        $Request = Invoke-WebRequest -Uri "$APIPath/$Endpoint" -Headers $Header -Method $Method -ContentType "Application/Json"
    }
    else {
        $Header = @{"api-token" = "$Token" ; "accept" = "application/json"}
        $Request = Invoke-WebRequest -Uri "$APIPath/$Endpoint" -Headers $Header -Method $Method -body $Body -ContentType "Application/Json"
    }
    #Parse response; return as object
    return $Request.Content | ConvertFrom-Json
}

# $EndPoint = "eventfeed?collapse=true&filter=event-type:node&filter=task:delete&page_size=1000"
$EndPoint = "eventfeed?collapse=false"

if(-not ($User -eq "ALL")){
    Write-Host "INFO: Retriving event log for user `"$User`""
    $EndPoint += "&filter=requestor_name:$User"
}
switch ($EventType) {
    "ALL"      {}
    "node"     { $EndPoint += "&filter=event-type:$EventType" }
    "client"   { $EndPoint += "&filter=event-type:$EventType" }
    "policy"   { $EndPoint += "&filter=event-type:$EventType" }
    "cbook" { $EndPoint += "&filter=event-type:version" }
    "environment" { $EndPoint += "&filter=event-type:$EventType" }
    "dbag"  { $EndPoint += "&filter=event-type:item" }
}

if( $Task -ne "ALL"){
    $EndPoint += "&filter=task:$Task"
}
$Endpoint += "&page_size=1000"

if($StartTime -ne "Default"){
    [String]$Time = Get-Date (Get-Date -Format "MM/dd/yyyy HH:mm:ss" $StartTime) -UFormat %s
    $Endpoint += "&start=$($Time)000"
}

if($EndTime -ne "Default"){
    [String]$Time = Get-Date (Get-Date -Format "MM/dd/yyyy HH:mm:ss" $EndTime) -UFormat %s
    $Endpoint += "&end=$($Time)000"
}

$EndPoint
$EventFeed = InvokeA2APIReq GET $EndPoint
if($EventFeed){
    $CurTime = Get-Date -Format "yyyy-MM-dd-HH-mm"
    $EventFeed.events | Export-Csv "./deploylog-$($CurTime).csv"
}

 

 

 

1 Reply

@greavette 

Not totally certain what you are looking for, but let's try this:

If you want to search for particular users you need to search inside the $EventFeed variable that is returned by this line:

$EventFeed = InvokeA2APIReq GET $EndPoint

As I don't know what the output looks like I can't offer a lot of help here.

 

Do you want to modify the existing report to add user totals?

 

Do you want a combined reports for the 2 users, with username and counts?

 

It might be better to create a new script that calls your existing script, passing in the required params and then creating a report.

Amend the end of the existing script:

Write-Verbose "[INFO] query string: $EndPoint"
$EventFeed = InvokeA2APIReq GET $EndPoint
Write-Output $EventFeed

to explicitly put the userData onto the pipeline.

 

The new script would be similar to:

$prodUsers = ./logDeploy.ps1 -User Prod
$devUsers  = ./logDeploy.ps1 -User Dev 

(it will need to be in the same dir as the logDeploy script)

and the userData output from the logDeploy script can be parsed inside the new script to create a report.