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.
$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
}
} ````


Make the
Select(Select-Object) call part of the remotely executing PowerShell command, which requires quoting the remotely executing command: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 ofpowershell.exe, the Windows PowerShell CLI.pwsh.exe, the PowerShell (Core) 7+ CLI,-Command(-c) must be specified when passing commands, because the-Fileparameter 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:Note:
>>redirection altogether and replaceConvertTo-CsvwithExport-Csvand the target file path:... | Export-Csv -NoTypeInformation D:\SCRIPTSM\ALV\PRUEBNOMB.csvAs for what you tried:
In your attempt,
Select -First 15 Name,ID,VM,PMruns locally, on the string output from the remotely executedGet-Process.Since
[string]instances have no properties such asName,ID, ...,Select-Object creates custom objects with such properties whose value is$null`, resulting in the empty output lines you saw.