I'm curious to see if anyone has written a script to use the inventory API (WSAPI) to inventory a non-supported OS yet.

The API functionality is described starting on p.113 in the 5.4 admin guide: https://www.kace.com/support/resources/~/media/Files/Support/Documentation/K1000/v54/K1000-Admin-Guide-v54.ashx

If you have, and don't mind sharing, please consider posting below or creating a blog entry for it.

 

0 Comments   [ + ] Show Comments

Comments

Please log in to comment

Community Chosen Answer

1

Hello,

here we go. I wrote it : http://www.itninja.com/blog/view/how-to-use-the-k1000-5-4-inventory-api-with-powershell

Regards,

StockTrader

 

Answered 01/03/2013 by: StockTrader
Red Belt

Please log in to comment

Answers

0

I'm trying hard but it is not so easy.

First of all the documentation makes the assumption at the point 2 of the instruction that the hexadecimal representation that results from the MD5 function is lowercase.

This is important and it is not specified.

To reply to build the authetication token you will need this:

auth_token=MD5(session_key + "|" + MD5(password))

where M5D returns a lowercase string that represent the hexadecimal result of the md5 hash function ( example: 5ca2aa845c8cd5ace6b016841f100d82 ).

I'm trying to write a script in powershell but the difficulty (at least for me) is to maintain the session between the first request and the subsequent request.

In the first you need to get the session_key and in the subsequent reqeust you need to reply with the session key and the operation to perform.

The problem I have is that using the System.Net.WebClient the session is not automatically mantained so the subsequent request after the fist one will reply to a session_key already expired.

As example I post my code here:

function MD5_String($s) {
    $result2 = "";
    $algo = [System.Security.Cryptography.HashAlgorithm]::Create("MD5");
    $hashByteArray2=$algo.ComputeHash($([Char[]]$s));
    foreach ($byte in $hashByteArray2)
    { $result2 += “{0:x2}” -f $byte };
    $result2;}
$password = "myNicePassword"   
$client = New-Object System.Net.WebClient
$session_string = $client.DownloadString("http://k1000org.kace.local/service/wsapi.php?keyreq=true");
$passwordMD5=MD5_String($password);
$tokenMD5=MD5_String($session_string+"|"+$passwordMD5);
$tokenMD5;
$downloadString="http://k1000org.kace.local/service/wsapi.php?req=newuuid&key="+$tokenMD5
$downloadString
$newuuid= $client.DownloadString($downloadString);
$newuuid; //does not work because seems that every invocation of the method DownloadString() creates a new session

 

Any help is very appreciated :-)

Regards,

StockTrader

Answered 12/28/2012 by: StockTrader
Red Belt

  • I reply to myself.
    Due to a trainy Saturday I was able to find a way to progress a little bit in my investigation and I found a way to maintain the session between two web calls using PowerShell.
    I so amended my script that it is now able to ask the K1000 to generate a new KUID

    #-----------
    function MD5_String($s) {
    $result2 = ""
    $algo = [System.Security.Cryptography.HashAlgorithm]::Create("MD5")
    $hashByteArray2=$algo.ComputeHash($([Char[]]$s))
    foreach ($byte in $hashByteArray2)
    { $result2 += “{0:x2}†-f $byte }
    $result2}

    $password = "MyPassword"
    #first request - We need to obtain the Session Key from K1000
    $client = New-Object System.Net.WebClient
    $client.Headers.Add("User-Agent", "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0;)")
    $session_string = $client.DownloadString("http://k1000org.kace.local/service/wsapi.php?keyreq=true")
    $cookie = $client.ResponseHeaders["Set-Cookie"] #we need to save the cookies to maintain the session
    $passwordMD5=MD5_String($password)
    $tokenMD5=MD5_String($session_string+"|"+$passwordMD5)
    Write-Host "Token to be used to reply to the K1000: " $tokenMD5
    #second request - now we ask K1000 to generate a new GUID
    $client = New-Object System.Net.WebClient
    $client.Headers.Add("User-Agent", "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0;)")
    $client.Headers.Add("Cookie", $cookie) #we send back the cookies we saved in the first request
    $downloadString="http://k1000org.kace.local/service/wsapi.php?req=newuuid&key="+$tokenMD5
    $newuuid= $client.DownloadString($downloadString)
    Write-Host "The UUID generated by the K1000 is: " $newuuid
    #---------The END

    Now the next challenge is to send the XML file.....
    Fortunately we are never short of rain here so maybe I'll find a good answer soon :-)
    Regards,
    StockTrader
Please log in to comment
0

Hello,

 

I made some progresses but I'm now a bit in stuck.

The problem is that I'm able to obain a KUID from the K1000 but when I try to send the file this is discarded  and in the K1000 log the message "Rejecting inventory submission with blank kuid." is logged.

The KUID is specified in the file I try to send and it is specified in the URI string as well as a GET parameter but...something is strange there.

My new script (PowerShell 2.0) is the following:

<#
 Testapi.ps1
 Test on how to use the K1000 INVENTORY API
 By StockTrader for DELL KACE
 You will need PowerShell 2.0
#>
function MD5_String($s) {
    $result2 = ""
    $algo = [System.Security.Cryptography.HashAlgorithm]::Create("MD5")
    $hashByteArray2=$algo.ComputeHash($([Char[]]$s))
    foreach ($byte in $hashByteArray2)
    { $result2 += “{0:x2}” -f $byte }
    $result2}
   
$password = "MyPassWord"   
#first request - We need to obtain the Session Key from K1000
$client = New-Object System.Net.WebClient
$client.Headers.Add("User-Agent", "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0;)") #better K1000 thinks we are a real browser
$session_string = $client.DownloadString("http://k1000org.kace.local/service/wsapi.php?keyreq=true")
Write-Host "The K1000 replied that the session string is: " $session_string
$cookie = $client.ResponseHeaders["Set-Cookie"] #we need to save the cookies to maintain the session
$passwordMD5=MD5_String($password)
$tokenMD5=MD5_String($session_string+"|"+$passwordMD5) #I calculate the reply token as indicaded in the help file
Write-Host "Token to be used to reply to the K1000: " $tokenMD5
#second request - now we ask K1000 to generate a new GUID
$client = New-Object System.Net.WebClient #better to have a fresh object than re-use the previous one
$client.Headers.Add("User-Agent", "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0;)") #better K1000 thinks we are a real browser
$client.Headers.Add("Cookie", $cookie) #we send back the cookies we saved in the first request. If you do not do this it will not work!
$downloadString="http://k1000org.kace.local/service/wsapi.php?req=newuuid&key="+$tokenMD5
$newuuid= $client.DownloadString($downloadString) #now we ask, as a test, to generate a new KUID
Write-Host "The UUID generated by the K1000 is: " $newuuid
$client = New-Object System.Net.WebClient
$client.Headers.Add("User-Agent", "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0;)")
$client.Headers.Add("Cookie", $cookie) #we send back the cookies we saved in the first request
#Now I try to uplkoad a well formatted XML file.
#Actually the file is the one proposed in the help of the K1000
#The file contains the KUID but I specify the same KUID as well in the GET string.
$uploadstring='http://k1000org.kace.local/service/wsapi.php?req=loadxml&key='+$tokenMD5+'&KUID=AABBCCCE-3764-4C57-A304-2FD23728DDA0&version=5.4'
Write-Host $uploadstring
$uploadFilePath="C:\\Program Files\\Dell\\KACE\\test.xml"
#$client.QueryString.Add("KUID","F1234567-C2D2-4055-85BB-294E6A3D22D9") #redundant. The effect is the same that to add the parameter to the URI string
$client.uploadfile($uploadstring,$uploadFilePath)
Write-Host "The End"

 Any ideas?!? I tried to alalyze with Wireshark the web requests and I do not find anything strange or unusual there.

Regards,

StockTrader

Answered 12/31/2012 by: StockTrader
Red Belt

Please log in to comment
0

I'm near :-)

Thanks to the help of a KACE developer I realized that we cannot use the method uploadfile of the System.Net.WebClient so I'm re-writing the script in a different way using lower level classes....

 Stay tuned :-)

StockTrader

Answered 01/03/2013 by: StockTrader
Red Belt

Please log in to comment
Answer this question or Comment on this question for clarity
Five Steps for Easier- and More Effective- IT Inventorying
Having an accurate inventory of all your IT assets is critical to ensuring that users are productive and that every dollar is well spent. This white paper outlines five steps that improve inventorying both hardware and software.

Share