PsExec for Remote Computer Installed Software List

I have the following code, it should work in theory, but it’s not and I can’t for the life of me figure out why.

I’ve also tried piping with “>” but still nothing. I can’t explain why it isn’t working.

Function executePsexec ($computer,$flags, $path,$arguments)
{ 
Invoke-Expression ".\PsExec.exe \\$computer $flags $path $arguments" #> $null
}
#This should create a local file 'Software.txt' on the remote machine, but it doesn't
executePsExec -computer "computer" -flags "-s -h -nobanner" -path "powershell.exe" -arguments 'Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Out-File C:\Temp\Software.txt'

#This should return a list of the installed software to my console, but it returns a bunch of empty lines
executePsExec -computer "computer" -flags "-s -h -nobanner" -path "powershell.exe" -arguments 'Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate'

#This should create a file on the remote system with the following information, but it doesn't
executePsExec -computer "computer" -flags "-s -h -nobanner" -path "powershell.exe" -arguments 'Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Out-File C:\Temp\Software.txt'

Your -arguments parameter should have no spaces

-arguments 'Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Out-File C:\Temp\Software.txt'

otherwise psexec will take it as separate command options causing it to error out

This works for example:

$ParameterList = @{
    computer    = 'computername'
    flags       = '-s -h -nobanner' 
    path        = 'powershell.exe'
    arguments   = '"Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Out-File C:\Temp\Software.txt"'
}
$Result = executePsExec @ParameterList *>&1 | Out-String
if ($Result -match 'error code 0') { 'Success' } else { $Result }

In example #2,

$ParameterList = @{
    computer    = 'computername'
    flags       = '-s -h -nobanner' 
    path        = 'powershell.exe'
    arguments   = '"Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate"'
Out-File C:\Temp\Software.txt'
}
$Result = executePsExec @ParameterList *>&1 | Out-String
if ($Result -match 'error code 0') { 'Success' } else { $Result }

The $Result variable will contain text representing console output of the Get-ItemProperty cmdlet AND the psexec.exe
It’s just text, that may be truncated, and cannot be reformatted via Format-List or similar cmdlets. As if you’re working in a linux environment. This is exactly why you should consider using PowerShell remoting instead of psexec

[quote quote=168796]Your -arguments parameter should have no spaces

-arguments 'Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Out-File C:\Temp\Software.txt'
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
otherwise psexec will take it as separate command options causing it to error out

This works for example:

PowerShell
$ParameterList = @{
computer = 'computername'
flags = '-s -h -nobanner'
path = 'powershell.exe'
arguments = '"Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Out-File C:\Temp\Software.txt"'
}
$Result = executePsExec @ParameterList *>&1 | Out-String
if ($Result -match 'error code 0') { 'Success' } else { $Result }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
In example #2,
PowerShell
$ParameterList = @{
computer = 'computername'
flags = '-s -h -nobanner'
path = 'powershell.exe'
arguments = '"Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate"'
Out-File C:\Temp\Software.txt'
}
$Result = executePsExec @ParameterList *>&1 | Out-String
if ($Result -match 'error code 0') { 'Success' } else { $Result }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The $Result variable will contain text representing console output of the Get-ItemProperty cmdlet AND the psexec.exe

It’s just text, that may be truncated, and cannot be reformatted via Format-List or similar cmdlets. As if you’re working in a linux environment. This is exactly why you should consider using PowerShell remoting instead of psexec

[/quote]
There are spaces between everything though, such as between Get-ItemProperty and HKLM. What does *>&1 do?

How do you know it is the spaces? I use the following command to check to see if a process is running, and it doesn’t encounter an error

executePsExec -computer $computer -flags "-s -h -nobanner" -path "powershell.exe" -arguments 'Get-Process | Where-Object {"$_.ProcessName" -like "*explorer*" }'
I prefer to use PsExec because it doesn't require additional dependencies.

Why would enclosing it with both single AND double quotes fix the problem? I tried it myself and it worked, but what’s the reasoning behind that? How does it affect spacing?

Why doesn’t this command suffer from the same problem?

executePsExec -computer $computer -flags "-s -h -nobanner" -path "powershell.exe" -arguments 'Get-Process | Where-Object {"$_.ProcessName" -like "*explorer*" }'

You’re passing a parameter to invoke-expression in your function that looks like:

.\PsExec.exe \\my-PC -s -h -nobanner powershell.exe Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Out-File C:\Temp\Software.txt

When psexec.exe tries to parse the text above, it splits it using the ‘space’ character as a delimiter. So it recognizes the following command options:

my-PC
-s
-h
-nobanner
powershell.exe
Get-ItemProperty
HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*
|
Out-File
C:\Temp\Software.txt

It accepts \my-pc as the -computername command option, -s as another command option, …
until it comes across HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall* and it errors out since it cannot match it to one of the command options it’s looking for.

On the other hand using

.\PsExec.exe \\my-PC -s -h -nobanner powershell.exe "Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Out-File C:\Temp\Software.txt"

gets parsed into

\\my-PC
-s
-h
-nobanner
powershell.exe
"Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Out-File C:\Temp\Software.txt"

which is accepted by psexec.exe parser - it can match each of these strings to a command option…

Usage: psexec [\\computer[,computer2[,...] | @file]][-u user [-p psswd][-n s][-r servicename][-h][-l][-s|-e][-x][-i [session]][-c [-f|-v]][-w directory][-d][-<priority>][-a n,n,...] cmd [arguments]
     -a         Separate processors on which the application can run with
                commas where 1 is the lowest numbered CPU. For example,
                to run the application on CPU 2 and CPU 4, enter:
                "-a 2,4"
     -c         Copy the specified program to the remote system for
                execution. If you omit this option the application
                must be in the system path on the remote system.
     -d         Don't wait for process to terminate (non-interactive).
     -e         Does not load the specified account's profile.
     -f         Copy the specified program even if the file already
                exists on the remote system.
     -i         Run the program so that it interacts with the desktop of the
                specified session on the remote system. If no session is
                specified the process runs in the console session.
     -h         If the target system is Vista or higher, has the process
                run with the account's elevated token, if available.
     -l         Run process as limited user (strips the Administrators group
                and allows only privileges assigned to the Users group).
                On Windows Vista the process runs with Low Integrity.
     -n         Specifies timeout in seconds connecting to remote computers.
     -p         Specifies optional password for user name. If you omit this
                you will be prompted to enter a hidden password.
     -r         Specifies the name of the remote service to create or interact.
                with.
     -s         Run the remote process in the System account.
     -u         Specifies optional user name for login to remote
                computer.
     -v         Copy the specified file only if it has a higher version number
                or is newer on than the one on the remote system.
     -w         Set the working directory of the process (relative to
                remote computer).
     -x         Display the UI on the Winlogon secure desktop (local system
                only).
     -arm       Specifies the remote computer is of ARM architecture.
     -priority	Specifies -low, -belownormal, -abovenormal, -high or
                -realtime to run the process at a different priority. Use
                -background to run at low memory and I/O priority on Vista.
     computer   Direct PsExec to run the application on the remote
                computer or computers specified. If you omit the computer
                name PsExec runs the application on the local system, 
                and if you specify a wildcard (\\*), PsExec runs the
                command on all computers in the current domain.
     @file      PsExec will execute the command on each of the computers listed
                in the file.
     cmd	    Name of application to execute.
     arguments  Arguments to pass (note that file paths must be
                absolute paths on the target system).
     -accepteula This flag suppresses the display of the license dialog.
     -nobanner   Do not display the startup banner and copyright message.

But -arguments is in single quotes, so wouldn’t it evaluate to this instead? Note the single quotes found around the powershell command.

.\PsExec.exe \\my-PC -s -h -nobanner powershell.exe 'Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Out-File C:\Temp\Software.txt'   

And that doesn’t explain why this works, because it shouldn’t

executePsExec -computer $computer -flags "-s -h -nobanner" -path "powershell.exe" -arguments 'Get-Process | Where-Object {"$_.ProcessName" -like "*explorer*" }'

EDIT:
I must be missing something. When it replaces the variable $arguments with the string, does it remove the quotes, regardless of if they are double or single quotes?

$a = 'hello'   #==> hello
$a = '"hello"' #==> "hello"

When you pass a string in a variable in PowerShell, the string delimiter is removed. In line 1 of the example above $a returns hello not ‘hello’
If you need it to return “hello” in quotes, you enclose the quotes in your string delimiter as in line 2 of this example.

Thank you, I don’t know why that confused me like it did.