# The example shows how to modify an existing file in SecureAnyBox # If you want to use PowerShell commands to work with SecureAnyBox, first # install SABPowerShellModule.msi # First, we need to authenticate the user. Connect-Sab command connects to SecureAnyBox and # opens the login dialogue. # Due to security reasons, we strongly recommend not using an admin user account. # Create (or use) a user account with minimum permissions (READ and CREATE) 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 dialogue for entering the code if ( $loginStatus.SecondFactorRequired ) { set-2fa } try{ # After the user logs in, it is necessary to get the id of destination Safe Box, # to where the file will create. # PowerShell commands Get-Records, Get-SafeBoxes and where-object are used to get record id # for a given record name. # When the response from the SecureAnyBox server is received, it is necessary to # check if the received id is valid and the script can continue $recordName = 'file_name' $safeBoxes = (Get-SafeBoxes | Where-Object { $_.Type -eq "SAFEBOX" }) + (Get-SafeBoxes | Get-SafeBoxes | Where-Object { $_.Type -eq "SAFEBOX" }) | Sort-Object -Unique -Property id $recordId = $safeBoxes | Get-Records | where-object { $_.Name -like $recordName -and $_.Template -eq 'File'} | Select-Object -expand id if ( $recordId -le 0 ) { Write-Host "File '$recordName'not found." exit } # If multiple files with the same name found, it is necessary to select # which file should be modified. if ($recordId.GetType().Name -eq 'Object[]'){ Write-Host $recordId.count "files found." Write-Host "------------------------------------------" foreach ($id in $recordId){ Get-Record -Id $id Write-Host "------------------------------------------" } $selectId = Read-Host "Enter id of file to modify" if ($recordId.contains($selectId)){ $recordId = $selectId Write-Host "`nFile '$recordName' found. (id $recordId)`n" } else { Write-Host "`nEntered id is invalid ! `n" exit } } else{ Write-Host "File '$recordName' found. (id $recordId)" } # Powershell function Set-AccessCode prompts the user to enter the Access Code # and store it on the SecureAnyBox server for a specified amount of seconds. A stored Access Code # allows saving secure fields to a new record and uploading a file. Set-AccessCode -Timeout 60 ## == value of timeout in seconds # Using the Get-Record command user obtains existing record from the SecureAnyBox into the $file variable. # In the variable, it is possible to change all file record's values except the file. # Note: If you do not want to change any of the values, add the # character to the beginning of a specific line. $file = Get-Record -Id $recordId # $file.Name = "name" $file.Description = "description" $file.Tags.Remove('tag1') ## removes tags 'tag1' $file.Tags.Add('tag2') $file.Note = "noteline1`nnoteline2" ## use `n to add new line $file.FileName = "name of file to upload" $file.Attributes."sec-note" = "sec-note12`nsec-note22" ## use `n to add new line # To make change in any record, we need to know the id of record and changed variables. # PowerShell Set-Record function returns the file object. If the id from returned file # object is greater than zero, then the file record modification was successful. After the validation, # it is possible to change the file by uploading a new one. $newRec = $file | Set-Record -Id $recordId $fileId = $newRec.id $fileName = $newRec.Name if ( $fileId -gt 0 ) { Write-Host "File '$fileName' created. (id $fileId )" } ## Decide, whether the file of record should be changed (0 = false, 1 = true) $uploadFile = 1 ## enter full path of file to store in SecureAnyBox $filePath = "full file path" if ($uploadFile -eq 1){ set-file -Id $fileId $filePath Write-Host "File '$filePath' uploaded to file record '$fileName'." } } finally{ # When the connection is not needed anymore, disconnect from the SecureAnyBox server. $loginStatus = Disconnect-SAB if(! $loginStatus.Authenticated){ Write-Host "Disconnected from SecureAnyBox server" } }