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:


    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:


    As for what you tried: