Search code examples
powershellsystem-administrationpsexec

Psexec doesnt give me the full results


Ive been trying this script using psexec to get processes of a range of pcs, i make it work but i dont get the results on the table that it should appear, ive been searching on psexec posts but i dont know why it gets me the results in half, i cant use Invoke-command or Get-Pssession because firewall/antivirus and GPOS of my org i can make individual exceptions but they get reset everytime the pcs get an update.

The result of the script is this

How it should appear

$pathscript = "D:\SCRIPTSM\ALV"
$fpassword = "D:\SCRIPTSM\pswd.json"
$rutaLog = "D:\SCRIPTSM\ALV\fallos.log"
$fecha = Date

Set-Location -Path $pathscript

$JsonObject = Get-Content $fpassword | ConvertFrom-Json

$usuario = 'Administrador'
$password = $JsonObject.password


$equipos = "D:\SCRIPTSM\despFi\equinom.txt"
$listaEquipos = Get-Content $equipos

 foreach ($equipo in $listaEquipos){

    if (test-connection -ComputerName $equipo -Count 1 -Quiet){
        
        Set-Location -Path "D:\SCRIPTSM\PSTools"
        .\psexec.exe \\$equipo -u $usuario -p $password -h powershell.exe Get-Process | Select -First 15 Name,ID,VM,PM >> 'D:\SCRIPTSM\ALV\PRUEBNOMB.txt'
        Set-Location -Path $pathscript

    }else{

        Write-Output "No se puede conectar al equipo --> $equipo" >> $rutaLog

    }
} ````


 

Solution

  • Make the Select (Select-Object) call part of the remotely executing PowerShell command, which requires quoting the remotely executing command:

    # Note the '...' around the `Get-Process | Select-Object ...` pipeline
    .\psexec.exe \\$equipo -u $usuario -p $password -h powershell.exe 'Get-Process | Select -First 15 Name,ID,VM,PM' >> 'D:\SCRIPTSM\ALV\PRUEBNOMB.txt'
    

    Note:

    • You'll capture the for-display representation of the processes, as you would see in the console - see alternative below.

    • The output is appended to a (caller-)local file; if instead you want to save to a file on the remote machine, also move the >> redirection inside '...' (you don't need the ' around the target file path - just omit them).

    • The '...' argument containing the command to execute remotely in full is implicitly passed to the -Command (c) parameter of powershell.exe, the Windows PowerShell CLI.

      • As an aside: With pwsh.exe, the PowerShell (Core) 7+ CLI, -Command (-c) must be specified when passing commands, because the -File parameter for executing script files is the default there.

    If you want to capture structured data instead, e.g. CSV, use the following, via ConvertTo-Csv:

    .\psexec.exe \\$equipo -u $usuario -p $password -h powershell.exe 'Get-Process | Select -First 15 Name,ID,VM,PM | ConvertTo-Csv -NoTypeInformation' >> 'D:\SCRIPTSM\ALV\PRUEBNOMB.csv'
    

    Note:

    • Again, if you want to save the output file remotely, omit the >> redirection altogether and replace ConvertTo-Csv with Export-Csv and the target file path:
      ... | Export-Csv -NoTypeInformation D:\SCRIPTSM\ALV\PRUEBNOMB.csv

    As for what you tried:

    • In your attempt, Select -First 15 Name,ID,VM,PM runs locally, on the string output from the remotely executed Get-Process.

    • Since [string] instances have no properties such as Name, ID, ..., Select-Object creates custom objects with such properties whose value is $null`, resulting in the empty output lines you saw.