Interacting with SCCM in button onClick events

Not sure where the issue lies with this one, but i’m trying to design a little menu that will take an asset name and MAC address from a text field and add them to an SCCM device collection.

New-UDTextbox -id Asset -label “Asset Tag” -icon “arrow_circle_o_right”
New-UDTextbox -id MAC -label “MAC Address” -icon “arrow_circle_o_right”

New-UDButton -id "Submit" -Text "Submit" -onclick {
    $asset = (Get-UDElement -id Asset).Attributes.value
    $mac = (Get-UDElement -id MAC).Attributes.value
    $collection = "collection ID"

    Import-CMComputerInformation -computername $asset -macaddress $mac -collection $collection

Doesn’t work. I’ve checked using a UDToast and the variables are picking up values. Now the SCCM cmdlets need to be run from the CM Drive, so you need to specify this normally using Set-Location “CMDrive:”. Initially I thought that this could be the issue, but i’ve tried moving the Set-Location inside the -onclick event handler, but this also makes no difference.

I’ve also tried simpler cmdlets, just trying the basic Get-CMDevice -name $asset etc, and nothing is being returned. I’ve been able to interact with other systems (ie AD, I can get Get-ADComputer etc to return results from inside an event handler), it just seems to be SCCM that doesn’t.

Any help gratefully received

Are you able to do these in a regular powershell window without using UD? Also, have you tried to use Enable-UDLogging to review any errors in there?

Yes, the same cmdlets work when I try them in normal PS.

Enabling logging was a good shout though.
“[Warn] ExecutionService Error executing endpoint script. The term ‘Import-CMComputerInformation’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.”

Looks like I need to move the Import-Module statement for the SCCM module into the event handler. Just tried that and it now works fine.

Thanks :smiley:

Glad you got it working!