Problems with File Search Output Using PowerShell Remoting
#$Computers = (Get-ADComputer -Filter { OperatingSystem -Like 'Windows 7 Professional'}).Name

$Computers = @("DDARLING-7D", "DGODFREY-7D")

ForEach ($Computer in $Computers)
{
     Try
         {
             Invoke-Command -Computername $Computer -ScriptBlock {

                $Path = 'C:\Windows\CSC\v2.0.6\namespace\fh-file1\user$\adminstrodej'

                IF(Test-Path -Path $Path)

                    {
                    
                    RMDIR -Path $Path -Recurse -Force
                    
                    }
                Get-ChildItem -Path 'C:\'-Recurse -Filter *.pst | Select-Object -Property Length, Name, DirectoryName, PSComputerName

           
            } -ErrorAction Stop
         }
     Catch
         {
             Add-Content C:\users\strodej\Unavailable-Computers.txt $Computer
         }

}

I was asked to search all the computers in our domain for the presence of user generated PST files. I can do this with PowerShell using Get-ChildItem and PS Remoting however my output was not easily imported into a spreadsheet for my supervisor to interpret. The script above gives me a list of the PST files which is good but I would prefer a table. When try to pipe the output through Format-Table but I lose my PSComputerName. When I try to export to a comma separated file I get nothing.

I also tried to add the results of each iteration to a array but my array is empty. I could use some help.

Length         : 312886272
Name           : Old Email 2015.pst
DirectoryName  : C:\Email
PSComputerName : DGODFREY-7D
RunspaceId     : 897d911b-1380-4d8d-9ec0-35b56fa77250


  • Edited by John Strode 18 hours 27 minutes ago edit title
August 25th, 2015 8:48am

Hi,

Use Export-Csv instead.

That said, why are you using remoting in the first place instead of just using the admin shares? I'd also recommend narrowing your search space from the entire C:\ drive, as that's going to take a significant amount of time.

Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 8:54am

Thanks for looking at my question.

When I tried to pipe the output to the search I got nothing.

Get-ChildItem -Path 'C:\'-Recurse -Filter *.pst | Select-Object -Property Length, Name, DirectoryName | Export-CSV -Path C:\users\username\PSTFiles.csv

Other technicians have squirreled the PST files everywhere. :)

August 25th, 2015 9:05am

If you ended up with a blank output file then that's telling you that no PST files were found.

I'd still be doing this with admin shares, that way you don't end up with an output file on each machine.

Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 9:20am

YOu cannot put the select inside of the invoke. 

It must be done this way:

$sb={
	$Path = 'C:\Windows\CSC\v2.0.6\namespace\fh-file1\user$\adminstrodej'
	if(Test-Path -Path $Path){
		RMDIR -Path $Path -Recurse -Force
	}
	Get-ChildItem C:\* -Filter *.pst -Recurse
}
$Computers = @("DDARLING-7D", "DGODFREY-7D")
$Computers|
	ForEach-Object{ 
		Try{
	    		Invoke-Command -Computername $_-ScriptBlock $sb -ErrorAction Stop
		}
		Catch{
			Add-Content C:\users\strodej\Unavailable-Computers.txt $_
		}
	} |
	Select-Object -Property Length, Name, DirectoryName, PSComputerNam
"PSComputerName" only exists outside of the Invoke.
August 25th, 2015 9:36am

Thanks jrv,

I tried the modifications you suggested and I got the output from one machine, DGODFREY-7D, twice. I closed the ISE Console and tried it again and got nothing. I manually used PSRemoting to connect to the system and ran the recursive search and found the known PST file. I'm not sure what is going on now. ;)

-John

Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 11:12am

Change $computer to $_ and it will work.

August 25th, 2015 11:14am

I should have thought of that - Duh!

How can I include the name of the computer in my output?

Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 11:40am

I should have thought of that - Duh!

How can I include the name of the computer in my output?

I fixed  the above script.  Just run it.  It returns computername automatically.

PS C:\scripts> invoke-command -ScriptBlock { Get-ChildItem C:\* } -ComputerName omega|select pscomputername,fullname

PSComputerName FullName
-------------- --------
omega          C:\Audit
omega          C:\CurrentClients
omega          C:\ELBackups
omega          C:\Eventvwr
omega          C:\Intel
omega          C:\IT
omega          C:\PerfLogs
omega          C:\PortQryUI
omega          C:\PortQryV2
omega          C:\Program Files
omega          C:\Program Files (x86)
omega          C:\rootfolder
omega          C:\scripts

August 25th, 2015 11:50am

Mike and jrv,

Thank you for all the help!

Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 1:02pm

You're very welcome, glad you got it working.
August 25th, 2015 1:16pm

You can add -AsJob to Invoke and pass al computers and it will execute much faster.

Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 1:28pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics