Kaseya Community

Powershell Variable to Custom Field question

  • Hello,

    I am looking to execute a powershell command to check for WLAN driver name and version and add them to a custom field. Here is my code

    ExecuteShellCommandToVariable("powershell.exe -command Get-WmiObject Win32_PnPSignedDriver ^| select devicename, driverversion ^| where {$_.devicename -like {*wireless adapter*}}", "System", false, "All Operating Systems", "half on Fail"
    UpdateSystemInfo("Wireless Driver Version", "#global:cmdresults#", "all Operating Systems", "half on Fail)
    The issue with this is the cmd results are too long to fit into the Custom Field. Any recommendations on capturing this data would be greatly appreciated.
  • You have a couple of options that you could try.

    #1. Write the output to disk, read the contents of the file & partse it to get the information you want in separate fields (we use grep for things like that) then save just the extracted information to the MCFs.

    #2. Write the output to disk, read the contents into a variable then write the data into the agent logs.  You can then role a report to pull that information from the logs

    #3 (my personal favorite) Write the output to disk, read the conetsnts into a variable, then save the information into the machine documents tab using GetFile with the ..\docs path. (or ..\docs\folder)

  • Hi,

    Is it maybe because your result is multiline? Try to make your command show only one result, then you can remove headers from your result by adding this pipe "| foreach {write-host $_.devicename, $_.driverversion}"

    Hope it helps.

  • Keep in mind for powershell, #global:psresults# is the variable for command output.

  • you may want to re-write the line as "execute-powershellcommand64bitsystem" and set the export to variable to true.  then you can use #global:psresults# this will also allow you utilzed the format table headers option in powershell. You can pipe the command to show | ft -hidetableheaders and will remove a lot of the junk in the output.

  • Use the executePowerShell command as Corey indicated.

    This PoSh command will return a single line containing what you're looking for:

    gwmi win32_PnPSignedDriver -Filter "DeviceClass = 'NET'" | Where-Object {$_.DeviceName -like "*wireless*"} | select-object @{n="Wireless Info"; e={$_.DeviceName+": v"+$_.DriverVersion}} | ft -HideTableHeaders

    The result will look like:

    Intel(R) Dual Band Wireless-AC 8260: v20.70.3.3

    You can then write the contents of #global:psresults# to the MCF using UpdateSystemInfo

  • Thank you all so much for your help! I updated my original code to add ^| ft -HideTableHeaders and I am pulling the information I need now! for reference, the full code I used is:

    powershell.exe -command Get-WmiObject Win32_PnPSignedDriver ^| select devicename, driverversion ^| where {$_.devicename -like {*wireless adapter*}} ^| ft -hidetableheaders

    I have been having some issues getting results from powershell commands to variable in Kaseya, so I made this as a workaround to use the executeShellCommandToVariable. I will play around with the psresults with powershell and see why it's not working as well as the shell to variable.

  • Pedro,

    I tried running your script in PowerShell, works great in pulling the info. However, I am getting an error when Kaseya runs it as a executePowerShell64bitsystem. Here is the error:

    You must provide a value expression on the right-hand side of the '-like' opera


    At line:1 char:91

    + gwmi win32_PnPSignedDriver -Filter DeviceClass = 'NET' | Where-Object {$_.Dev

    iceName -like <<<<  *wireless*} | select-object @{n=Wireless Info; e={$_.Device

    Name+: v+$_.DriverVersion}} | ft -HideTableHeaders

       + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordEx


       + FullyQualifiedErrorId : ExpectedValueExpression

    The code I am running is:

    executePowershellCommand64BitSystem gwmi win32_PnPSignedDriver -Filter "DeviceClass = 'NET'" | Where-Object {$_.DeviceName -like "*wireless*"} | select-object @{n="Wireless Info"; e={$_.DeviceName+": v"+$_.DriverVersion}} | ft -HideTableHeaders

  • All of the quotes appear to be missing in the execution, try reversing the use of " (double-quote) & ' (single-quote) characters.

  • I had some issues where ExecutePoweshell was failing to run as expected on some computers. Here is what I did to get it working.

    Turned Pedro's code into a .ps1 file, used a service account with impersonateUser and execute script with executePowerShellCommand64bitUser. This is working perfectly and consistently across the board.

    Thank you so much again everyone for your help with this.