Payton Flint's Tech Blog
Menu
  • Home
  • Blog
  • Categories
  • Resources
  • About
  • Contact
Menu

PowerShell – Convert PsExec-run Cmdlet Output Strings Back to Objects, Properties, & Values

Posted on August 14, 2023August 24, 2023 by paytonflint

I have been experimenting with using PsExec of the Sysinternals suite to gather information from remote Windows systems for diagnostic use. I’ve found it to be a powerful tool, especially when integrated with PowerShell scripts. It is able to execute a script or command on a remote machine and return the output back to the shell, and can often do so when PowerShell itself is unable to suffice. However, if the returned output is an object with numerous properties, as so many cmdlets will return, PsExec is not capable of passing objects back to the shell. They will return as a string, and it is desirable to convert them back into usable PowerShell objects for further use.

Because PsExec is able to run on systems that may not have PowerShell remoting, WinRM, or IIS enabled, we can have insight into our machines without further opening them up. This helps maintain the security posture of the environment to a small degree, making it preferential to other options – if only you could return actual usable objects.

Alas, I have created a ConvertTo-Objects script that accepts the string input and will convert it back to usable objects for further use with your script. Execution might look something like this:

$CompInfo = psexec -s -nobanner -h -i \\$ComputerName Powershell.exe -Command "Get-ComputerInfo" 2> $null
$CompInfo = ConvertTo-Objects -inputString $CompInfo

Here is the link to this script on my GitHub: https://github.com/p8nflnt/Infosec-Toolbox/blob/main/ConvertTo-Objects.ps1

And here is the code:

<#
.SYNOPSIS
    Convert Property : Value strings back into usable objects
    Intended to compliment use of PsExec utility within PoSH scripts

.NOTES
    Name: ConvertTo-Objects
    Author: Payton Flint
    Version: 1.1
    DateCreated: 2023-Aug

.LINK
    https://paytonflint.com/powershell-convert-psexec-run-cmdlet-output-strings-back-to-objects-properties-values/
    https://github.com/p8nflnt/Infosec-Toolbox/blob/main/Convert-ToObjects.ps1
#>
Function ConvertTo-Objects {
    param (
        $inputString
    )

    # Split the input string into lines
    $lines = $inputString -split "`n"

    # Initialize an empty array to hold objects
    $objects = @()

    # Initialize an empty hashtable to hold property values
    $properties = @{}

    # Iterate through each line and extract property and value using regex
    foreach ($line in $lines) {
        if ($line -match '^(.*?):\s*(.*)$') {
            $property = $matches[1].Trim()
            $value = $matches[2].Trim()

            if ($property -ne '') {
                $properties[$property] = $value
            }
        }
    }

    # If there are properties, create an object and add it to the array
    if ($properties.Count -gt 0) {
        $object = [PSCustomObject]$properties
        $objects += $object
    }

    # Return the resulting objects
    Write-Output $objects
} # End Function ConvertTo-Objects

# Call the function with the input
$Output = ConvertTo-Objects -inputString $input

$Output

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

About The Author

Author's Portrait

In my journey as a technologist and 11 years of experience as an IT professional, I have found my niche as Director of Infrastructure Services; developing my skillsets in management, scripting, cloud infrastructure, identity management, and networking.

I have experience as a Systems Administrator and Engineer for large enterprises including the DoD, government agencies, and a nuclear-generation site.

I've been blessed to collaborate with engineers at esteemed Fortune 50 corporations, and one of Africa's largest, to ensure successful implementation of my work.

GitHub Button

Credentials

M365 Endpoint Administrator Associate
M365 Fundamentals
Microsoft AZ-900
CompTIA CSIS
CompTIA CIOS
CompTIA Security+
CompTIA Network+
CompTIA A+
  • April 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
© 2022 Payton Flint | The views and opinions expressed on this website belong solely to the author/owner and do not represent the perspectives of any individuals, institutions, or organizations, whether affiliated personally or professionally, unless explicitly stated otherwise. The content and products on this website are provided as-is with no warranties or guaranties, are for informational/demonstrative purposes only, do not constitute professional advice, and are not to be used maliciously. The author/owner is not responsible for any consequences arising from actions taken based on information provided on this website, nor from the use/misuse of products from this site. All trademarks are the property of their respective owners.