I’m trying to do some processing on the output from a psftp “ls” command. Basically, I want to put all of the file names that match certain conditions into an array. I have this:
PS C:\path\to\pwd> $a = & "C:\Program Files (x86)\PuTTY\psftp.exe" -l myusername -batch -pw mypassword -b sftpbatch.txt myserver | where {$_.split(" ", [StringSplitOptions]'RemoveEmptyEntries')[0] -eq "-rw-r--r--"} | select-object {$_.split(" ", [StringSplitOptions]'RemoveEmptyEntries')[8]}
(If you want more details about that command, I can provide them. The output is very similar to the output of the “ls” command in PowerShell.)
It seems to me that I can do better by selecting the split first, then filtering it with where. When I try this:
$a = & <# ... #> | select-object {$_.split(" ", [StringSplitOptions]'RemoveEmptyEntries')} | where { $_[0] -eq "-rw-r--r--" }
I get
Unable to index into an object of type System.Management.Automation.PSObject.
How can I simplify this?
Something like this should work.
If you are placing this in a script, I would replace the alias
%withForeach-Object, and the alias?withWhere-ObjectEdit:
Here is a more pipeline-oriented approach: