Skip to content

Commit

Permalink
Search-TssSystemLog - new command
Browse files Browse the repository at this point in the history
  • Loading branch information
wsmelton committed Aug 10, 2021
1 parent 3fecdb8 commit 6ca69de
Show file tree
Hide file tree
Showing 9 changed files with 318 additions and 16 deletions.
19 changes: 9 additions & 10 deletions .vscode/tss.code-snippets
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@
" ${2:Short of what command does}",
"",
" .DESCRIPTION",
" ${3:Longer of what command does}",
" ${2}",
"",
" .LINK",
" https://thycotic-ps.github.io/thycotic.secretserver/commands/${7}/Search-Tss${1}",
" https://thycotic-ps.github.io/thycotic.secretserver/commands/${3:tag name}/Search-Tss${1}",
"",
" .LINK",
" https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/${7:Folder name}/Search-Tss${1}.ps1",
" https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/${3}/Search-Tss${1}.ps1",
"",
" .EXAMPLE",
" \\$session = New-TssSession -SecretServer https://alpha -Credential \\$ssCred",
Expand All @@ -116,16 +116,16 @@
" Requires TssSession object returned by New-TssSession",
" #>",
" [CmdletBinding()]",
" [OutputType('Tss${7}')]",
" [OutputType('${7:Class name}')]",
" param (",
" # TssSession object created by New-TssSession for authentication",
" [Parameter(Mandatory,ValueFromPipeline,Position = 0)]",
" [Thycotic.PowerShell.Authentication.Session]",
" \\$TssSession,",
"",
" # Short description for parameter",
" [Alias(\"${1}Id\")]",
" [int]",
" [Alias(\"${8:alias if desired}\")]",
" [${9:Class/Type}]",
" \\$${4},",
"",
" # Sort by specific property, default ${1}Name",
Expand All @@ -140,14 +140,13 @@
" Write-Verbose \"Provided command parameters: \\$(. \\$GetInvocation \\$PSCmdlet.MyInvocation)\"",
" if (\\$tssParams.ContainsKey('TssSession') -and \\$TssSession.IsValidSession()) {",
" . \\$CheckVersion \\$TssSession '10.9.000000' \\$PSCmdlet.MyInvocation",
" \\$restResponse = \\$null",
" \\$uri = \\$TssSession.ApiUrl, '${1}s' -join '/'",
" \\$uri = \\$TssSession.ApiUrl, '${10:endpoint}' -join '/'",
" \\$uri = \\$uri, \"sortBy[0].direction=asc&sortBy[0].name=\\$SortBy&take=\\$(\\$TssSession.Take)\" -join '?'",
" \\$invokeParams.Method = 'GET'",
"",
" \\$filters = @()",
" switch (\\$tssParams.Keys) {",
" '${4} { \\$filters += \"filter.${4}=\\$${4}\" }",
" '${4}' { \\$filters += \"filter.${4}=\\$${4}\" }",
" }",
" if (\\$filters) {",
" \\$uriFilter = \\$filters -join '&'",
Expand All @@ -170,7 +169,7 @@
" Write-Warning \"No ${1} found\"",
" }",
" if (\\$restResponse.records) {",
" [Tss${7}[]]\\$restResponse.records",
" [${9}[]]\\$restResponse.records",
" }",
" } else {",
" Write-Warning \"No valid session found\"",
Expand Down
105 changes: 105 additions & 0 deletions docs/commands/diagnostics/Search-TssSystemLog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Search-TssSystemLog

## SYNOPSIS
Search the Secret Server System Log

## SYNTAX

```
Search-TssSystemLog [-TssSession] <Session> [-SearchText <String>] [-LogLevel <LogLevel>] [-SortBy <String>]
[<CommonParameters>]
```

## DESCRIPTION
Search the Secret Server System Log

## EXAMPLES

### EXAMPLE 1
```
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred
Search-TssSystemLog -TssSession $session -SearchText "powershell"
```

Return Log messages matching the text "powershell"

## PARAMETERS

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

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

Required: True
Position: 1
Default value: None
Accept pipeline input: True (ByValue)
Accept wildcard characters: False
```
### -SearchText
Text to search for in System Log
```yaml
Type: String
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -LogLevel
Log Level to filter on
```yaml
Type: LogLevel
Parameter Sets: (All)
Aliases:
Accepted values: Emergency, Alert, Critical, Error, Warning, Notice, Information, Debug

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -SortBy
Sort by specific property, default SystemLogName
```yaml
Type: String
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: SystemLogName
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
### Thycotic.PowerShell.Diagnostics.SystemLog
## NOTES
Requires TssSession object returned by New-TssSession
## RELATED LINKS
[https://thycotic-ps.github.io/thycotic.secretserver/commands/diagnostics/Search-TssSystemLog](https://thycotic-ps.github.io/thycotic.secretserver/commands/diagnostics/Search-TssSystemLog)
[https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/diagnostics/Search-TssSystemLog.ps1](https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/diagnostics/Search-TssSystemLog.ps1)
6 changes: 6 additions & 0 deletions docs/getting_started/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ Secret Server REST API was first released with version 9.0. The API has grown si
| [Search-TssMetadata] | 10.9.000064 |
| [Get-TssFolder] (-FolderPath param) | 11.0.000000 |
| [Get-TssSecret] (-Path param) | 11.0.000000 |
| [Get-TssSecretPolicy] | 11.0.000000 |
| [Set-TssSecretPolicy] | 11.0.000000 |
| [Search-TssSystemLog] | 11.0.000000 |

[Get-TssSecretAudit]:/thycotic.secretserver/commands/secrets/Get-TssSecretAudit
[Get-TssUserRoleAssigned]:/thycotic.secretserver/commands/users/Get-TssUserRoleAssigned
Expand All @@ -33,3 +36,6 @@ Secret Server REST API was first released with version 9.0. The API has grown si
[Search-TssMetadata]:/thycotic.secretserver/commands/metadata/Search-TssMetadata
[Get-TssFolder]:/thycotic.secretserver/commands/folders/Get-TssFolder
[Get-TssSecret]:/thycotic.secretserver/commands/secrets/Get-TssSecret
[Get-TssSecretPolicy]:/thycotic.secretserver/commands/secret-policies/Get-TssSecretPolicy
[Set-TssSecretPolicy]:/thycotic.secretserver/commands/secret-policies/Set-TssSecretPolicy
[Search-TssSystemLog]:/thycotic.secretserver/commands/diagnostics/Search-TssSystemLog
37 changes: 37 additions & 0 deletions src/Thycotic.SecretServer.Format.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -2544,5 +2544,42 @@
</TableControl>
</View>


<!-- Thycotic.PowerShell.Diagnostics.SystemLog -->
<View>
<Name>Diagnostics.SystemLog</Name>
<ViewSelectedBy>
<TypeName>Thycotic.PowerShell.Diagnostics.SystemLog</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Alignment>Left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Alignment>Center</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Alignment>Left</Alignment>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>DateRecorded</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>LogLevel</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>LogMessage</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>

</ViewDefinitions>
</Configuration>
13 changes: 7 additions & 6 deletions src/Thycotic.SecretServer.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Generated by: Shawn Melton
#
# Generated on: 8/9/2021
# Generated on: 8/10/2021
#

@{
Expand Down Expand Up @@ -122,11 +122,12 @@ FunctionsToExport = 'Add-TssEventPipeline', 'Add-TssFolderPermission',
'Search-TssSecretAccessRequest', 'Search-TssSecretDependency',
'Search-TssSecretHook', 'Search-TssSecretPermission',
'Search-TssSecretPolicy', 'Search-TssSecretTemplate',
'Search-TssUser', 'Search-TssWorkflowTemplate', 'Set-TssFolder',
'Set-TssSecret', 'Set-TssSecretExpiration', 'Set-TssSecretField',
'Set-TssSecretPolicy', 'Set-TssSecretRpcAssociated',
'Set-TssSecretRpcPrivileged', 'Set-TssSecretSecurity',
'Set-TssSecretTemplate', 'Show-TssCurrentUser', 'Start-TssDiscovery',
'Search-TssSystemLog', 'Search-TssUser', 'Search-TssWorkflowTemplate',
'Set-TssFolder', 'Set-TssSecret', 'Set-TssSecretExpiration',
'Set-TssSecretField', 'Set-TssSecretPolicy',
'Set-TssSecretRpcAssociated', 'Set-TssSecretRpcPrivileged',
'Set-TssSecretSecurity', 'Set-TssSecretTemplate',
'Show-TssCurrentUser', 'Start-TssDiscovery',
'Start-TssSecretChangePassword', 'Start-TssSecretDependency',
'Start-TssSecretHeartbeat', 'Stop-TssSecretChangePassword',
'Test-TssFolderAction', 'Test-TssSdkClient', 'Test-TssSecretAction',
Expand Down
17 changes: 17 additions & 0 deletions src/Thycotic.SecretServer/classes/diagnostics/SystemLog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Threading.Tasks;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using Thycotic.PowerShell.Enums;

namespace Thycotic.PowerShell.Diagnostics
{
public class SystemLog
{
public string CorrelationId { get; set; }
public DateTime? DateRecorded { get; set; }
public LogLevel LogLevel { get; set; }
public string LogMessage { get; set; }
public string MachineName { get; set; }
}
}
19 changes: 19 additions & 0 deletions src/Thycotic.SecretServer/enums/LogLevel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Threading.Tasks;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

namespace Thycotic.PowerShell.Enums
{
public enum LogLevel
{
Emergency,
Alert,
Critical,
Error,
Warning,
Notice,
Information,
Debug
}
}
94 changes: 94 additions & 0 deletions src/functions/diagnostics/Search-TssSystemLog.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
function Search-TssSystemLog {
<#
.SYNOPSIS
Search the Secret Server System Log
.DESCRIPTION
Search the Secret Server System Log
.LINK
https://thycotic-ps.github.io/thycotic.secretserver/commands/diagnostics/Search-TssSystemLog
.LINK
https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/diagnostics/Search-TssSystemLog.ps1
.EXAMPLE
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred
Search-TssSystemLog -TssSession $session -SearchText "powershell"
Return Log messages matching the text "powershell"
.EXAMPLE
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred
Search-TssSystemLog -TssSession $session -SearchText "Azure AD"
Return Log messages matching the text "Azure AD"
.NOTES
Requires TssSession object returned by New-TssSession
#>
[CmdletBinding()]
[OutputType('Thycotic.PowerShell.Diagnostics.SystemLog')]
param (
# TssSession object created by New-TssSession for authentication
[Parameter(Mandatory,ValueFromPipeline,Position = 0)]
[Thycotic.PowerShell.Authentication.Session]
$TssSession,

# Text to search for in System Log
[string]
$SearchText,

# Log Level to filter on
[Thycotic.PowerShell.Enums.LogLevel]
$LogLevel,

# Sort by specific property, default DateRecorded
[string]
$SortBy = 'DateRecorded'
)
begin {
$tssParams = $PSBoundParameters
$invokeParams = . $GetInvokeApiParams $TssSession
}
process {
Write-Verbose "Provided command parameters: $(. $GetInvocation $PSCmdlet.MyInvocation)"
if ($tssParams.ContainsKey('TssSession') -and $TssSession.IsValidSession()) {
. $CheckVersion $TssSession '11.0.000000' $PSCmdlet.MyInvocation
$uri = ($TssSession.ApiUrl -replace 'v1','v2'), 'diagnostics', 'system-logs' -join '/'
$uri = $uri, "sortBy[0].direction=desc&sortBy[0].name=$SortBy&take=$($TssSession.Take)" -join '?'
$invokeParams.Method = 'GET'

$filters = @()
switch ($tssParams.Keys) {
'SearchText' { $filters += "filter.searchTerm=$SearchText" }
'LogLevel' { $filters += "filter.logLevel=$LogLevel"}
}
if ($filters) {
$uriFilter = $filters -join '&'
Write-Verbose "Filters: $uriFilter"
$uri = $uri, $uriFilter -join '&'
}
$invokeParams.Uri = $uri

Write-Verbose "Performing the operation $($invokeParams.Method) $uri"
try {
$apiResponse = Invoke-TssApi @invokeParams
$restResponse = . $ProcessResponse $apiResponse
} catch {
Write-Warning "Issue on search request"
$err = $_
. $ErrorHandling $err
}

if ($restResponse.records.Count -le 0 -and $restResponse.records.Length -eq 0) {
Write-Warning "No messages found in the System Log"
}
if ($restResponse.records) {
[Thycotic.PowerShell.Diagnostics.SystemLog[]]$restResponse.records
}
} else {
Write-Warning "No valid session found"
}
}
}
Loading

0 comments on commit 6ca69de

Please sign in to comment.