AnsweredAssumed Answered

Update Hardware owner with PowerShell and API

Question asked by Oz Merchant Administrator on Jul 5, 2016
Latest reply on Dec 20, 2016 by Ivan Armendariz

(Originally posted on 30, December 2015 by Winship, Andy)

After a lot of trial and error I have finally got some PowerShell code working that uses the Invoke-WebRequest to make updates to Samanage records using the API.
As I had searched high and low for examples to work from I though I would post this to help anyone else trying to use Powershell and the API.  More examples please.


Save this as a ps1 file.
Run the 2nd line (commented out) to create an encrypted txt file for the password, and then run the script after you change the username, owner and hardware ID to match your environment


#Use the following command to create a txt file with encrypted password to be used later
#read-host -assecurestring | convertfrom-securestring | out-file securestring.txt


$username = ""
$password = cat securestring.txt | convertto-securestring
$Credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password


$headers = @{"Accept"="application/vnd.samanage.v1.1+xml"}
$ContentType = "text/xml"


#Create XML data for upload
$newowner = ""
$xmltext = "<hardware><owner><email>$newowner</email></owner></hardware>"
$xmldata = [xml] $xmltext


# 101010 = Samanage ID of Hardware to update
$uri = "";
Invoke-WebRequest $uri -Credential $Credentials -Method put -ContentType $ContentType -Body $xmldata -Headers $headers