KaceSMA icon indicating copy to clipboard operation
KaceSMA copied to clipboard

Updating Asset custom fields

Open kawhiteMBU opened this issue 3 years ago • 5 comments

I want to update the owner field on an asset. When I run Get-SMAAsset I get this:

id              : 7341
asset_type_id   : 10202
name            : Polycom VVX 411 (PoE)
asset_data_id   : 351
owner_id        : 11450
modified        : 2021-05-04 15:54:43
created         : 2021-03-30 13:36:06
mapped_id       : 0
asset_class_id  : 0
archive         : 
asset_status_id : 493
field_10283     : @{id=11450; user_name=user; [email protected]; full_name=User}
field_10284     : Spare
field_10278     : Polycom VVX 411 (PoE)
field_10279     : 4001
field_10280     : 64167fd2854f
field_10281     : Department
field_10285     : 
field_10286     : 
field_10287     : 
asset_type_name : VOIP Phones
location        : @{id=7274; asset_type_id=1; name=Spare; owner_id=0; asset_class_id=11011; asset_status_id=0}
Associations    : {}

The field I need to update is field_10283. I've tried creating a hash table with the info to be updated but I still get errors.

Powershell shows:

At U:\Documents\WindowsPowerShell\Modules\KaceSMA\2.0.1\private\New-ApiPUTRequest.ps1:29 char:9
         Invoke-RestMethod @IRMSplat
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

KACE Server logs show:

[Tue May 04 15:52:19.012997 2021] [proxy_fcgi:error] [pid 34407:tid 34382394112] [client 127.0.0.1:20597] AH01071: Got error 'PHP message: [2021-05-04 15:52:19 -0400] PHP Warning: preg_match() expects parameter 2 to be string, array given in /kbox/kboxwww/include/RowValues.class.php on line 337\nPHP message: [2021-05-04 15:52:19 -0400] PHP Warning: strcasecmp() expects parameter 1 to be string, array given in /kbox/kboxwww/include/RowValues.class.php on line 337\n'

Any help on how to format the code to update this field is appreciated.

kawhiteMBU avatar May 04 '21 20:05 kawhiteMBU

can you post the exact code you're using to update the asset? (Set-SmaAsset)

ArtisanByteCrafter avatar May 04 '21 20:05 ArtisanByteCrafter

Here it is. The update to field_10281 works fine.

'Assets' = @(
        @{
        'field_10281'    = 'New Department'
        'field_10283'    = @{
            'id'             = 678
            'user_name'    = 'new_user'
            'email'        = '[email protected]'
            'full_name'    = 'User, New D.'
            }
        }
    )
}
Set-SmaAsset -ID $AssetID -Body $SetAssetBody

kawhiteMBU avatar May 04 '21 20:05 kawhiteMBU

I don't actually have a Kace appliance anymore due to a job change, so I can really only be of limited help unfortunately. Looking at your hashtable, you don't seem to have a top level node with the ID of the asset you want to update. It's been a few years since i've been able to do this, but i think that bit is necessary.

$SetAssetBody = @{
    'Assets' = @(
        @{
            'id' = 1234 # ID of asset you want to update
            'field_10281' = 'New Department'
            'field_10283' = @{
                'id'        = 678
                'user_name' = 'new_user'
                'email'     = '[email protected]'
                'full_name' = 'User, New D.'
            }
        }
    )
}

ArtisanByteCrafter avatar May 04 '21 21:05 ArtisanByteCrafter

I updated the code to this but it still gives the same error.

'Assets' = @(
        @{
        'id'             = 7341
        'field_10281'    = 'New Department'
        'field_10283'    = @{
            'id'           = 678
            'user_name'    = 'new_user'
            'email'        = '[email protected]'
            'full_name'    = 'User, New D.'
            }
        }
    )
}
Set-SmaAsset -ID 7341 -Body $SetAssetBody
Invoke-RestMethod : The remote server returned an error: (500) Internal Server Error.
At U:\Documents\WindowsPowerShell\Modules\KaceSMA\2.0.1\private\New-ApiPUTRequest.ps1:29 char:9
         Invoke-RestMethod @IRMSplat
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~
     CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
     FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

As a side question, how do you get your code to show up clean in the comments?

kawhiteMBU avatar May 04 '21 21:05 kawhiteMBU

Github flavored markdown supports code formatting, so just use 3 back ticks followed by a language, and it formats it for you. Note the closing 3 back ticks too at the bottom:

image

Kace's API is woefully poorly documented. I looked through the last version of the PDF if have access to (SMA v10) and there are no indicators of what format it wants for any field other than a simple string.

Wish i could be of more help.

ArtisanByteCrafter avatar May 04 '21 21:05 ArtisanByteCrafter