Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
$URI = "https://myserver/webservices/sswebservice.asmx" #The URI of the webservice
$templateName = "File Storage" #The name of the secret template we are going to use.
$username = "myusername"
$domain = "" #Leave empty if you aren't using domain accounts
$password = "mypassword"
$organizationCode = "" #Leave empty unless you are using Secret Server Online
$pathToFile = "C:\Users\My Account\Desktop\input.txt" #The path to the file we are going to upload.
$maxFieldSize = 1991
$secretName = "My New File 2"
#End Configuration
$ss = New-WebServiceProxy -UseDefaultCredential -uri $URI
#Set the correct username and password. It's recommended to use an empty string rather than "Local" for domain
$token = $ss.Authenticate($username, $password, $organizationCode, $domain)
if ($token.Errors.Length -gt 0)
echo $token.Errors
#Get the template for the secret we are adding
$template = $ss.GetSecretTemplates($token.Token).SecretTemplates | Where {$_.Name -eq $templateName}
$fields = $ss.GetSecretTemplateFields($token.Token, $template.Id).Fields | Where {$_.DisplayName -match "Item\d"} | Sort DisplayName
$content = [System.Convert]::ToBase64String([System.IO.File]::ReadAllBytes($pathToFile))
$contentSplit = @()
While($content.Length -gt 0)
$fileSection = $content.Substring(0, [System.Math]::Min($content.Length, $maxFieldSize))
$contentSplit = $contentSplit + $fileSection
$content = $content.Substring($fileSection.Length)
While($contentSplit.Length -lt $fields.Length)
$contentSplit = $contentSplit + ""
$fieldIds = @()
$fields | ForEach {$fieldIds = $fieldIds + $_.Id}
$ss.AddSecret($token.Token, $template.Id, $secretName, $fieldIds, $contentSplit, -1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment