# Example showing how to search for records by name according to a specified # pattern. # If you want to use PowerShell commands to work with SecureAnyBox, first # install SecureAnyBoxPowerShellModule.msi # First, enter a search pattern in the dialogue box. If you click the OK button, # the example will continue to run. When you click the Cancel button or enter # a blank search pattern, the example will stop. [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null $pattern = [Microsoft.VisualBasic.Interaction]::InputBox('Search Pattern', 'Enter Search Pattern', '') if ( $pattern -eq $null -or $pattern -eq '' ) { exit } # Then, we need to authenticate the user. Connect-Sab command connects to SecureAnyBox and # opens the login dialog. # Due to security reasons, we strongly recommend not using an admin user account. # Create (or use) a user account with minimum permissions (READ) only # for the destination Safe Box instead. $sabUrl = "http://127.0.0.1/sab" ## enter Url of SecureAnyBox $loginStatus = connect-sab $sabUrl # If the second-factor authentication is required, the set-2fa function # opens a dialog for entering the code if ( $loginStatus.SecondFactorRequired ) { set-2fa } # $format variable specifies width of columns in printed results table # $nameClmWidth specifies a maximum length of printed names of Safe Box Groups, Safe Boxes and records $format = "{0,-35}{1,-14}{2,-35}" $nameClmWidth = 34 # Print the header of the results table Write-Host `n($format -f "Name", "Type", "Parent") Write-Host $('-' * 80) $found = 0 # The script uses the "like" operator to filter the records found # using wildcards similar to name filtering in Windows/DOS command lines. # Therefore, the search pattern must be delimited by asterisks '*'. # A new search pattern (with asterisks at the beginning and the end) is stored # in the $workPattern variable. $workPattern = '*' + $pattern + '*' # We now scan the Safe Box Groups and their nested Safe Boxes. # PowerShell commands Get-Safeboxes (imported from SABPowerShellModule.msi) # and Where-Object (standard command) are used to retrieve an array of Safe Box # objects according to a specified condition (get Safe Box Groups only) $groups = Get-SafeBoxes | Where-Object { $_.Type -eq "SAFEBOX_GROUP" } # In the next step, script compares the names of Safe Box Groups to specified search pattern. foreach ( $groupItem in $groups ) { if ( $groupItem.Name -like $workPattern ) { [string]$name = $groupItem.Name # Results of the comparison are printed in fixed-width table columns # The $format variable contains the column widths. Write-Host ($format -f $name.substring(0,[System.Math]::Min($nameClmWidth, $name.Length)), $groupItem.Type, "") $found++ } # From each of Safe Box Group, PowerShell Get-Safeboxes command retrieves nested Safe Boxes # Safe Box names are compared to a search pattern $boxes = $groupItem | Get-SafeBoxes foreach ( $boxItem in $boxes ) { if ( $boxItem.Name -like $workPattern ) { [string]$name = $boxItem.Name # Results of the comparison are printed in fixed width table columns # The $format variable contains the column widths. Write-Host ($format -f $name.substring(0,[System.Math]::Min($nameClmWidth, $name.Length)), $boxItem.Type, $groupItem.Name) $found++ } # From each of Safe Box, PowerShell Get-Records and Get-Record commands retrieves nested records # Record names are compared to a search pattern using Where-Object PowerShell command $records = $boxItem | Get-Records | Get-Record | Where-Object -Property Name -Like $workPattern foreach ( $recItem in $records ) { if ( $recItem.Name -like $workPattern ) { #$path variable is used in results table as Parent $path = $groupItem.Name + "\" + $boxItem.Name [string]$name = $recItem.Name # Results are printed in fixed width table columns # The $format variable contains the column widths. Write-Host ($format -f $name.substring(0,[System.Math]::Min($nameClmWidth, $name.Length)), $recItem.Template, $path) $found++ } } } } # Now the script compares the search pattern with names of all Safe Boxes in the root level. # PowerShell commands Get-Safeboxes (imported from SABPowerShellModule.msi) # and Where-Object (standard command) are used to retrieve an array of Safe Box # objects according to a specified condition (get Safe Boxes only) $boxes = Get-SafeBoxes | Where-Object { $_.Type -eq "SAFEBOX" } foreach ( $boxItem in $boxes ) { if ( $boxItem.Name -like $workPattern ) { [string]$name = $boxItem.Name # Results are printed in fixed width table columns # The $format variable contains the column widths. Write-Host ($format -f $name.substring(0,[System.Math]::Min($nameClmWidth, $name.Length)), $boxItem.Type, "") $found++ } # From each of Safe Box, PowerShell Get-Records and Get-Record commands retrieves nested records # Record names are compared to a search pattern using Where-Object PowerShell command $records = $boxItem | Get-Records | Get-Record | Where-Object -Property Name -Like $workPattern foreach ( $recItem in $records ) { [string]$name = $recItem.Name # Results are printed in fixed width table columns # The $format variable contains the column widths. Write-Host ($format -f $name.substring(0,[System.Math]::Min($nameClmWidth, $name.Length)), $recItem.Template, $boxItem.Name) $found++ } } Write-Host `n"Printed" $found "rows"