Skip to content

Commit

Permalink
Search-TssIpRestriction - new command to search IP Address Restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
wsmelton committed Sep 28, 2021
1 parent 8d156e3 commit aa34f65
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .vscode/tss.code-snippets
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@
" }",
"",
" if (\\$restResponse.records.Count -le 0 -and \\$restResponse.records.Length -eq 0) {",
" Write-Warning \"No ${1} found\"",
" Write-Warning \"No records found\"",
" }",
" if (\\$restResponse.records) {",
" [${9}[]]\\$restResponse.records",
" [${7}[]]\\$restResponse.records",
" }",
" } else {",
" Write-Warning \"No valid session found\"",
Expand Down
73 changes: 73 additions & 0 deletions docs/commands/ipaddress-restrictions/Search-TssIpRestriction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Search-TssIpRestriction

## SYNOPSIS
Search for IP Address restrictions

## SYNTAX

```
Search-TssIpRestriction [-TssSession] <Session> [-SortBy <String>] [<CommonParameters>]
```

## DESCRIPTION
Search for IP Address restrictions

## EXAMPLES

### EXAMPLE 1
```
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred
Search-TssIpRestriction -TssSession $session
```

Return list of IP Address restrictions for Secret Server

## 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
```
### -SortBy
Sort by specific property, default Id
```yaml
Type: String
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: Id
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.IpRestrictions.Summary
## NOTES
Requires TssSession object returned by New-TssSession
## RELATED LINKS
[https://thycotic-ps.github.io/thycotic.secretserver/commands/ipaddress-restrictions/Search-TssIpRestriction](https://thycotic-ps.github.io/thycotic.secretserver/commands/ipaddress-restrictions/Search-TssIpRestriction)
[https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/ipaddress-restrictions/Search-TssIpRestriction.ps1](https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/ipaddress-restrictions/Search-TssIpRestriction.ps1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Threading.Tasks;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

namespace Thycotic.PowerShell.IpRestrictions
{
public class Summary
{
public int Id { get; set; }
public string Name { get;set; }
public string Range {get;set;}
}
}
70 changes: 70 additions & 0 deletions src/functions/ipaddress-restrictions/Search-TssIpRestriction.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
function Search-TssIpRestriction {
<#
.SYNOPSIS
Search for IP Address restrictions
.DESCRIPTION
Search for IP Address restrictions
.LINK
https://thycotic-ps.github.io/thycotic.secretserver/commands/ipaddress-restrictions/Search-TssIpRestriction
.LINK
https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/ipaddress-restrictions/Search-TssIpRestriction.ps1
.EXAMPLE
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred
Search-TssIpRestriction -TssSession $session
Return list of IP Address restrictions for Secret Server
.NOTES
Requires TssSession object returned by New-TssSession
#>
[CmdletBinding()]
[OutputType('Thycotic.PowerShell.IpRestrictions.Summary')]
param (
# TssSession object created by New-TssSession for authentication
[Parameter(Mandatory,ValueFromPipeline,Position = 0)]
[Thycotic.PowerShell.Authentication.Session]
$TssSession,

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

$invokeParams.Uri = $uri

Write-Verbose "Performing the operation $($invokeParams.Method) $($invokeParams.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 records found"
}
if ($restResponse.records) {
[Thycotic.PowerShell.IpRestrictions.Summary[]]$restResponse.records
}
} else {
Write-Warning "No valid session found"
}
}
}
24 changes: 24 additions & 0 deletions tests/ipaddress-restrictions/Search-TssIpRestriction.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
BeforeDiscovery {
$commandName = Split-Path ($PSCommandPath.Replace('.Tests.ps1','')) -Leaf
}
Describe "$commandName verify parameters" {
BeforeDiscovery {
[object[]]$knownParameters = 'TssSession', 'SortBy'
[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 Thycotic.PowerShell.IpRestrictions.Summary" -TestCases $commandDetails {
$_.OutputType.Name | Should -Be 'Thycotic.PowerShell.IpRestrictions.Summary'
}
}
}

0 comments on commit aa34f65

Please sign in to comment.