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

%3CLINGO-SUB%20id%3D%22lingo-sub-2771987%22%20slang%3D%22en-US%22%3EAssistance%20with%20modifying%20existing%20(working)%20Powershell%20script%20to%20add%20totals%20for%20Users%20found%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2771987%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20Forum%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20a%20working%20existing%20PowerShell%20script.%26nbsp%3B%20I'm%20looking%20to%20modify%20this%20script%20whereby%20two%20Users%20are%20searched%20for%3A%3C%2FP%3E%3CP%3EPROD%3C%2FP%3E%3CP%3EDEV%3C%2FP%3E%3CP%3EWhere%20the%20resulting%20report%20would%20show%20a%20total%20of%20records%20found%20for%20each%20of%20the%20two%20above%20users.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECurrently%20we%20invoke%20the%20script%20using%3A%3C%2FP%3E%3CP%3E%26nbsp%3B.%2FlogDeploy.ps1%20-User%20Prod%3C%2FP%3E%3CP%3Eor%3C%2FP%3E%3CP%3E.%2FlogDeploy.ps1%20-User%20Dev%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBut%20we%20would%20like%20to%20run%20the%20script%20and%20have%20it%20automatically%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20assistance%20that%20can%20be%20provided%20would%20be%20greatly%20appreciated.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3Eparam(%0A%20%20%20%20%0A%20%20%20%20%5Bstring%5D%5Bparameter(Mandatory%3D%24false%2C%20HelpMessage%3D'Event%20Type%20filter%3A%20ALL%2C%20node%2C%20cbook%2C%20client%2C%20policy%2C%20dbag')%5D%24EventType%20%3D%20%22ALL%22%2C%0A%20%20%20%20%5Bstring%5D%5Bparameter(Mandatory%3D%24false%2C%20HelpMessage%3D'User%20ID%20filter')%5D%24User%20%3D%20%22ALL%22%2C%0A%20%20%20%20%5Bstring%5D%5Bparameter(Mandatory%3D%24false%2C%20HelpMessage%3D'Task%20Type%20filter%3A%20ALL%2C%20delete%2C%20create%2C%20update')%5D%24Task%20%3D%20%22ALL%22%2C%20%0A%20%20%20%20%5Bstring%5D%5Bparameter(Mandatory%3D%24false%2C%20HelpMessage%3D'Start%20time%20in%20format%20MM%2Fdd%2Fyyyy%20HH%3Amm%3Ass')%5D%24StartTime%20%3D%20%22Default%22%2C%0A%20%20%20%20%5Bstring%5D%5Bparameter(Mandatory%3D%24false%2C%20HelpMessage%3D'End%20Time%20in%20format%20MM%2Fdd%2Fyyyy%20HH%3Amm%3Ass')%5D%24EndTime%20%3D%20%22Default%22%0A)%0A%0AWrite-Host%20%22%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3DDeployment%20Log%20%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%22%0A%24Script%3AToken%3D%22xxxxxxxxxxxx%22%0Afunction%20InvokeA2APIReq()%20%7B%0A%20%20%20%20param(%0A%20%20%20%20%20%20%20%20%5Bstring%5D%5Bparameter(Mandatory%3D%24True%2C%20HelpMessage%3D'REST%20Method')%5D%24Method%2C%0A%20%20%20%20%20%20%20%20%5Bstring%5D%5Bparameter(Mandatory%3D%24True%2C%20HelpMessage%3D'A2%20Endpoint')%5D%24Endpoint%2C%0A%20%20%20%20%20%20%20%20%5Bparameter(Mandatory%3D%24False%2C%20HelpMessage%3D'Body%20as%20string')%5D%24Body%0A%20%20%20%20)%0A%20%0A%20%20%20%20%24APIPath%20%3D%20%22https%3A%2F%2Fcompany.com%2Fapi%2Fv0%22%0A%20%20%20%20%0A%20%20%20%20%23%20Set%20TLS%20to%201.2%0A%20%20%20%20%5BNet.ServicePointManager%5D%3A%3ASecurityProtocol%20%3D%20%5BNet.SecurityProtocolType%5D%3A%3ATls12%0A%20%0A%20%20%20%20if%20(%24Method%20-match%20%22get%22)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%24header%20%3D%20%40%7B%22api-token%22%20%3D%20%22%24Token%22%7D%0A%20%20%20%20%20%20%20%20%24Request%20%3D%20Invoke-WebRequest%20-Uri%20%22%24APIPath%2F%24Endpoint%22%20-Headers%20%24Header%20-Method%20%24Method%20-ContentType%20%22Application%2FJson%22%0A%20%20%20%20%7D%0A%20%20%20%20else%20%7B%0A%20%20%20%20%20%20%20%20%24Header%20%3D%20%40%7B%22api-token%22%20%3D%20%22%24Token%22%20%3B%20%22accept%22%20%3D%20%22application%2Fjson%22%7D%0A%20%20%20%20%20%20%20%20%24Request%20%3D%20Invoke-WebRequest%20-Uri%20%22%24APIPath%2F%24Endpoint%22%20-Headers%20%24Header%20-Method%20%24Method%20-body%20%24Body%20-ContentType%20%22Application%2FJson%22%0A%20%20%20%20%7D%0A%20%20%20%20%23Parse%20response%3B%20return%20as%20object%0A%20%20%20%20return%20%24Request.Content%20%7C%20ConvertFrom-Json%0A%7D%0A%0A%23%20%24EndPoint%20%3D%20%22eventfeed%3Fcollapse%3Dtrue%26amp%3Bfilter%3Devent-type%3Anode%26amp%3Bfilter%3Dtask%3Adelete%26amp%3Bpage_size%3D1000%22%0A%24EndPoint%20%3D%20%22eventfeed%3Fcollapse%3Dfalse%22%0A%0Aif(-not%20(%24User%20-eq%20%22ALL%22))%7B%0A%20%20%20%20Write-Host%20%22INFO%3A%20Retriving%20event%20log%20for%20user%20%60%22%24User%60%22%22%0A%20%20%20%20%24EndPoint%20%2B%3D%20%22%26amp%3Bfilter%3Drequestor_name%3A%24User%22%0A%7D%0Aswitch%20(%24EventType)%20%7B%0A%20%20%20%20%22ALL%22%20%20%20%20%20%20%7B%7D%0A%20%20%20%20%22node%22%20%20%20%20%20%7B%20%24EndPoint%20%2B%3D%20%22%26amp%3Bfilter%3Devent-type%3A%24EventType%22%20%7D%0A%20%20%20%20%22client%22%20%20%20%7B%20%24EndPoint%20%2B%3D%20%22%26amp%3Bfilter%3Devent-type%3A%24EventType%22%20%7D%0A%20%20%20%20%22policy%22%20%20%20%7B%20%24EndPoint%20%2B%3D%20%22%26amp%3Bfilter%3Devent-type%3A%24EventType%22%20%7D%0A%20%20%20%20%22cbook%22%20%7B%20%24EndPoint%20%2B%3D%20%22%26amp%3Bfilter%3Devent-type%3Aversion%22%20%7D%0A%20%20%20%20%22environment%22%20%7B%20%24EndPoint%20%2B%3D%20%22%26amp%3Bfilter%3Devent-type%3A%24EventType%22%20%7D%0A%20%20%20%20%22dbag%22%20%20%7B%20%24EndPoint%20%2B%3D%20%22%26amp%3Bfilter%3Devent-type%3Aitem%22%20%7D%0A%7D%0A%0Aif(%20%24Task%20-ne%20%22ALL%22)%7B%0A%20%20%20%20%24EndPoint%20%2B%3D%20%22%26amp%3Bfilter%3Dtask%3A%24Task%22%0A%7D%0A%24Endpoint%20%2B%3D%20%22%26amp%3Bpage_size%3D1000%22%0A%0Aif(%24StartTime%20-ne%20%22Default%22)%7B%0A%20%20%20%20%5BString%5D%24Time%20%3D%20Get-Date%20(Get-Date%20-Format%20%22MM%2Fdd%2Fyyyy%20HH%3Amm%3Ass%22%20%24StartTime)%20-UFormat%20%25s%0A%20%20%20%20%24Endpoint%20%2B%3D%20%22%26amp%3Bstart%3D%24(%24Time)000%22%0A%7D%0A%0Aif(%24EndTime%20-ne%20%22Default%22)%7B%0A%20%20%20%20%5BString%5D%24Time%20%3D%20Get-Date%20(Get-Date%20-Format%20%22MM%2Fdd%2Fyyyy%20HH%3Amm%3Ass%22%20%24EndTime)%20-UFormat%20%25s%0A%20%20%20%20%24Endpoint%20%2B%3D%20%22%26amp%3Bend%3D%24(%24Time)000%22%0A%7D%0A%0A%24EndPoint%0A%24EventFeed%20%3D%20InvokeA2APIReq%20GET%20%24EndPoint%0Aif(%24EventFeed)%7B%0A%20%20%20%20%24CurTime%20%3D%20Get-Date%20-Format%20%22yyyy-MM-dd-HH-mm%22%0A%20%20%20%20%24EventFeed.events%20%7C%20Export-Csv%20%22.%2Fdeploylog-%24(%24CurTime).csv%22%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2771987%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2782000%22%20slang%3D%22en-US%22%3ERe%3A%20Assistance%20with%20modifying%20existing%20(working)%20Powershell%20script%20to%20add%20totals%20for%20Users%20found%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2782000%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1162689%22%20target%3D%22_blank%22%3E%40greavette%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENot%20totally%20certain%20what%20you%20are%20looking%20for%2C%20but%20let's%20try%20this%3A%3C%2FP%3E%3CP%3EIf%20you%20want%20to%20search%20for%20particular%20users%20you%20need%20to%20search%20inside%20the%20%24EventFeed%20variable%20that%20is%20returned%20by%20this%20line%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24EventFeed%20%3D%20InvokeA2APIReq%20GET%20%24EndPoint%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EAs%20I%20don't%20know%20what%20the%20output%20looks%20like%20I%20can't%20offer%20a%20lot%20of%20help%20here.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDo%20you%20want%20to%20modify%20the%20existing%20report%20to%20add%20user%20totals%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDo%20you%20want%20a%20combined%20reports%20for%20the%202%20users%2C%20with%20username%20and%20counts%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt%20might%20be%20better%20to%20create%20a%20%3CEM%3Enew%3C%2FEM%3E%20script%20that%20calls%20your%20existing%20script%2C%20passing%20in%20the%20required%20params%20and%20then%20creating%20a%20report.%3C%2FP%3E%3CP%3EAmend%20the%20end%20of%20the%20existing%20script%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EWrite-Verbose%20%22%5BINFO%5D%20query%20string%3A%20%24EndPoint%22%0A%24EventFeed%20%3D%20InvokeA2APIReq%20GET%20%24EndPoint%0AWrite-Output%20%24EventFeed%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3Eto%20explicitly%20put%20the%20userData%20onto%20the%20pipeline.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20new%20script%20would%20be%20similar%20to%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24prodUsers%20%3D%20.%2FlogDeploy.ps1%20-User%20Prod%0A%24devUsers%20%20%3D%20.%2FlogDeploy.ps1%20-User%20Dev%20%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E(it%20will%20need%20to%20be%20in%20the%20same%20dir%20as%20the%20logDeploy%20script)%3C%2FP%3E%3CP%3Eand%20the%20userData%20output%20from%20the%20logDeploy%20script%20can%20be%20parsed%20inside%20the%20new%20script%20to%20create%20a%20report.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Regular Visitor

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.