Skip to content

Commit

Permalink
Invoke-Report - new command for executing reports
Browse files Browse the repository at this point in the history
  • Loading branch information
wsmelton committed Jul 3, 2021
1 parent d0fd9ca commit 59cd835
Show file tree
Hide file tree
Showing 3 changed files with 253 additions and 0 deletions.
120 changes: 120 additions & 0 deletions docs/commands/reports/Invoke-TssReport.md
@@ -0,0 +1,120 @@
# Invoke-TssReport

## SYNOPSIS
Executes returns the results of a Report.

## SYNTAX

```
Invoke-TssReport [-TssSession] <TssSession> [-ReportId <Int32>] [-ReportName <String>]
[-Parameters <Hashtable>] [<CommonParameters>]
```

## DESCRIPTION
Executes returns the results of a Report.

## EXAMPLES

### EXAMPLE 1
```
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred
Invoke-TssReport -TssSession $session -ReportName GroupMembershipReportByGroup -Parameters @{Group = 1}
```

Executes report GroupMembershipReportByGroup returning PSCustomObject result for Everyone group (1)

### EXAMPLE 2
```
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred
Invoke-TssReport -TssSession $session -ReportName 'Filter Name' -Parameters @{customtext = 'adrastea.jupiter.com\brittney.poole - 4073'}
```

Executes report "Filter Name" returning PSCustomObject result based on custom text filter

### EXAMPLE 3
```
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred
Invoke-TssReport -TssSession $session -ReportId 60
```

Executes report 60 (Changed90DaysReportName) returning PSCustomObject result

## PARAMETERS

### -TssSession
TssSession object created by New-TssSession for auth

```yaml
Type: TssSession
Parameter Sets: (All)
Aliases:

Required: True
Position: 1
Default value: None
Accept pipeline input: True (ByValue)
Accept wildcard characters: False
```

### -ReportId
Report Id

```yaml
Type: Int32
Parameter Sets: (All)
Aliases: Id

Required: False
Position: Named
Default value: 0
Accept pipeline input: False
Accept wildcard characters: False
```

### -ReportName
Name of Report

```yaml
Type: String
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

### -Parameters
Report Parameters provided as hash format of @{\<Parameter Name\>,\<Param Value\>} (see examples)

```yaml
Type: Hashtable
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

### CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).

## INPUTS

## OUTPUTS

### System.Object
## NOTES
Requires TssSession object returned by New-TssSession

## RELATED LINKS

[https://thycotic-ps.github.io/thycotic.secretserver/commands/Invoke-TssReport](https://thycotic-ps.github.io/thycotic.secretserver/commands/Invoke-TssReport)

[https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/reports/Invoke-Report.ps1](https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/reports/Invoke-Report.ps1)

109 changes: 109 additions & 0 deletions src/functions/reports/Invoke-Report.ps1
@@ -0,0 +1,109 @@
function Invoke-Report {
<#
.SYNOPSIS
Executes returns the results of a Report.
.DESCRIPTION
Executes returns the results of a Report.
.EXAMPLE
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred
Invoke-TssReport -TssSession $session -ReportName GroupMembershipReportByGroup -Parameters @{Group = 1}
Executes report GroupMembershipReportByGroup returning PSCustomObject result for Everyone group (1)
.EXAMPLE
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred
Invoke-TssReport -TssSession $session -ReportName 'Filter Name' -Parameters @{customtext = 'adrastea.jupiter.com\brittney.poole - 4073'}
Executes report "Filter Name" returning PSCustomObject result based on custom text filter
.EXAMPLE
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred
Invoke-TssReport -TssSession $session -ReportId 60
Executes report 60 (Changed90DaysReportName) returning PSCustomObject result
.LINK
https://thycotic-ps.github.io/thycotic.secretserver/commands/Invoke-TssReport
.LINK
https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/reports/Invoke-Report.ps1
.NOTES
Requires TssSession object returned by New-TssSession
#>
[CmdletBinding()]
[OutputType([System.Object])]
param (
# TssSession object created by New-TssSession for auth
[Parameter(Mandatory, ValueFromPipeline, Position = 0)]
[TssSession]
$TssSession,

# Report Id
[Alias("Id")]
[int]
$ReportId,

# Name of Report
[string]
$ReportName,

# Report Parameters provided as hash format of @{<Parameter Name>,<Param Value>} (see examples)
[System.Collections.Hashtable]
$Parameters
)
begin {
$tssParams = $PSBoundParameters
$invokeParams = . $GetInvokeTssParams $TssSession
}
process {
Write-Verbose "Provided command parameters: $(. $GetInvocation $PSCmdlet.MyInvocation)"
if ($tssParams.ContainsKey('TssSession') -and $TssSession.IsValidSession()) {
. $CheckVersion $TssSession '10.9.000000' $PSCmdlet.MyInvocation
$uri = $TssSession.ApiUrl, 'reports', 'execute' -join '/'
$invokeParams.Uri = $uri
$invokeParams.Method = 'POST'

$executeBody = @{}
switch ($tssParams.Keys) {
'ReportId' { $executeBody.Add('id', [string]$ReportId) }
'ReportName' { $executeBody.Add('name', $ReportName) }
'Parameters' {
$reportParams = @()
foreach ($param in $Parameters.Keys) {
$reportParams += [ordered]@{
Name = $param
Value = $Parameters.$param
}
}
$executeBody.Add('parameters', $reportParams )
}
}
$invokeParams.Body = $executeBody | ConvertTo-Json -Depth 100
Write-Verbose "$($invokeParams.Method) $uri with: `n$($invokeParams.Body)"
try {
$restResponse = . $InvokeApi @invokeParams
} catch {
Write-Warning 'Issue getting report'
$err = $_
. $ErrorHandling $err
}

if ($restResponse) {
$results = @()
foreach ($row in $restResponse.rows) {
$reportData = [PSCustomObject]@{ }
for ($r = 0;$r -lt $restResponse.columns.Count;$r++) {
$reportData.PSObject.Properties.Add([PSNoteProperty]::new($restResponse.columns[$r],$row[$r]))
}
$results += $reportData
}
$results
}
} else {
Write-Warning 'No valid session found'
}
}
}
24 changes: 24 additions & 0 deletions tests/reports/Invoke-Report.Tests.ps1
@@ -0,0 +1,24 @@
BeforeDiscovery {
$commandName = Split-Path ($PSCommandPath.Replace('.Tests.ps1','')) -Leaf
}
Describe "$commandName verify parameters" {
BeforeDiscovery {
[object[]]$knownParameters = 'TssSession', 'ReportId', 'ReportName', 'Parameters'
[object[]]$currentParams = ([Management.Automation.CommandMetaData]$ExecutionContext.SessionState.InvokeCommand.GetCommand($commandName,'Function')).Parameters.Keys
[object[]]$commandDetails = [System.Management.Automation.CommandInfo]$ExecutionContext.SessionState.InvokeCommand.GetCommand($commandName,'Function')
$unknownParameters = Compare-Object -ReferenceObject $knownParameters -DifferenceObject $currentParams -PassThru
}
Context "Verify parameters" -Foreach @{currentParams = $currentParams } {
It "$commandName should contain <_> parameter" -TestCases $knownParameters {
$_ -in $currentParams | Should -Be $true
}
It "$commandName should not contain parameter: <_>" -TestCases $unknownParameters {
$_ | Should -BeNullOrEmpty
}
}
Context "Command specific details" {
It "$commandName should set OutputType to System.Object" -TestCases $commandDetails {
$_.OutputType.Name | Should -Be 'System.Object'
}
}
}

0 comments on commit 59cd835

Please sign in to comment.