[Powershell Script] Update a workstations assigned department via a request

[Powershell Script] Update a workstations assigned department via a request

The following script will update the department of any workstation that is added to the Asset(s) field of an incident template. I've implemented it so that my first line team can confirm where an asset is located and update it accordingly while on the phone to an end-user.

I hope it helps someone else in the future - 

  1. Create a new pick-list, add in all of the departments listed within your environment and include it within your preferred template(s).
  2. Edit line 10 to match the name of your pick-list field, remove the encapsulation if it doesn't include spaces.
  3. Edit line 14 to match your environments URL.
  4. Edit line 50 and replace the API key with one from your environment.
  5. Place the script in ...\integration\custom_scripts
  6. Configure a new custom trigger with your preferred criteria and set the action type as follows:

    cmd /c start /wait powershell.exe -WindowStyle Hidden -file c:\ManageEngine\ServiceDesk\integration\custom_scripts\Audit.ps1 $COMPLETE_JSON_FILE]

  7. Note: Each new department added to your environment in the future will also need to be added to this field.)       
  8. Note: You may need to include your environments domain name after the $assets variable (i.e. $assets.consonto.com).
  1. # Store request data passed to script - this must be first element in script 
  2. param (
  3.     $json = "none"
  4. )

  5. $jsondata = Get-Content $json -Encoding UTF8   #Encode if necessary
  6. $requestdata = ConvertFrom-Json $jsondata

  7. # Extract required request data passed to script
  8. $dept = $requestdata.request.'Assets Department'
  9. $assets = $requestdata.request.ASSET


  10. $url = "http://localhost/api/cmdb/ci"

  11. $input_data = "<?xml version=""1.0"" encoding=""UTF-8""?>
  12. <API version=""1.0"">
  13.     <citype>
  14.         <name>Workstation</name>
  15.         <criterias>
  16.             <criteria>
  17.                 <parameter>
  18.                     <name compOperator=""IS"">CI Name</name>
  19.                     <value>$assets</value>
  20.                 </parameter>
  21.             </criteria>
  22.         </criterias>
  23.         <newvalue>
  24.             <record>
  25.                 <parameter>
  26.                     <name>Asset State</name>
  27.                     <value>In use</value>
  28.                 </parameter>
  29.                 <multi-valued-parameter name=""Assign Ownership"">
  30.                     <record>
  31.                         <parameter>
  32.                             <name>Department</name>
  33.                             <value>$dept</value>
  34.                         </parameter>
  35.                     </record>
  36.                 </multi-valued-parameter>
  37.             </record>
  38.         </newvalue>
  39.     </citype>
  40. </API>
  41. "
  42. $input_data.Replace('$assets', $assets)
  43. $input_data.Replace('$dept', $dept)

  44. $postParams = @{ OPERATION_NAME="update"; TECHNICIAN_KEY="53CAC4EB-8CF9-4B31-80A7-0E31F0807724"; INPUT_DATA=$input_data; format="json" }
  45. $postParams
  46. $response = Invoke-WebRequest -UseBasicParsing -Uri $url -Method POST -Body $postParams 
  47. $response.content

                  New to ADSelfService Plus?