Searching Secret Server programmatically - PowerShell script

For instructions with using PowerShell and Integrated Windows Authentication (no embedded passwords) see: 
http://support.thycotic.com/KB/a98/using-web-services-with-windows-authentication-powershell.aspx

Secret Server Webservices can be called using scripts. This example demonstrates how to authenticate and search for a secret programatically in PowerShell. There are other KB articles with examples in different scripting and programming languages.

Steps:
1) Save the script below to a file, such as searchsecret.ps1
2) Change the script as needed to match your Secret Server and username/password/domain.
3) Change the $searchterm to match your search
4) Open a command window (cmd.exe)
5) Navigate to the same directory as searchsecret.ps1
6) Run the script by using .\searchsecret.ps1 OR powershell .\searchsecret.ps1



$url = 'http://mysecretserver/webservices/sswebservice.asmx'
$username = 'myusername'
$password = 'mypassword'
$domain = 'mydomain'   # leave blank for local users

$searchterm = 'VPN'
$proxy = New-WebServiceProxy -uri $url -UseDefaultCredential

# get a token for further use by authenticating using username/password
$result1 = $proxy.Authenticate($username, $password, '', $domain)
if ($result1.Errors.length -gt 0){
$result1.Errors[0]
exit

else 
{
$token = $result1.Token
}

# search secrets with our searchterm (authenticate by passing in our token)
Write-Host 'Searching for: ' $searchterm
$result2 = $proxy.SearchSecrets($token, $searchterm)
if ($result2.Errors.length -gt 0){
$result2.Errors[0]
}
else
{
Write-Host 'Got search results: ' $result2.SecretSummaries.length

# If you want the data as XML
# $xml = convertto-xml $result2.SecretSummaries -As string -Depth 20
# $xml

$result2.SecretSummaries | ForEach-Object { Write-Host 'SecretId:' $_.SecretId '  Name:' $_.SecretName  ' FolderId:' $_FolderId }

# if ($result2.SecretSummaries.length -gt 0) {
# $result2.SecretSummaries[0]
# }

}

Article ID: 237, Created On: 1/16/2012, Modified: 1/17/2012