none
Problems with File Search Output Using PowerShell Remoting RRS feed

  • Question

  • #$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 Tuesday, August 25, 2015 12:43 PM edit title
    Tuesday, August 25, 2015 12:42 PM

Answers

  • 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.


    • Marked as answer by John Strode Tuesday, August 25, 2015 4:55 PM
    Tuesday, August 25, 2015 12:48 PM
  • 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.


    \_(ツ)_/




    • Edited by jrv Tuesday, August 25, 2015 3:09 PM
    • Marked as answer by John Strode Tuesday, August 25, 2015 4:55 PM
    Tuesday, August 25, 2015 1:29 PM

All replies

  • 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.


    • Marked as answer by John Strode Tuesday, August 25, 2015 4:55 PM
    Tuesday, August 25, 2015 12:48 PM
  • 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. :)

    Tuesday, August 25, 2015 12:58 PM
  • 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.


    Tuesday, August 25, 2015 1:13 PM
  • 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.


    \_(ツ)_/




    • Edited by jrv Tuesday, August 25, 2015 3:09 PM
    • Marked as answer by John Strode Tuesday, August 25, 2015 4:55 PM
    Tuesday, August 25, 2015 1:29 PM
  • 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

    Tuesday, August 25, 2015 3:06 PM
  • Change $computer to $_ and it will work.


    \_(ツ)_/

    Tuesday, August 25, 2015 3:08 PM
  • I should have thought of that - Duh!

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

    Tuesday, August 25, 2015 3:33 PM
  • 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


    \_(ツ)_/

    Tuesday, August 25, 2015 3:44 PM
  • Mike and jrv,

    Thank you for all the help!

    Tuesday, August 25, 2015 4:56 PM
  • You're very welcome, glad you got it working.

    Tuesday, August 25, 2015 5:10 PM
  • You can add -AsJob to Invoke and pass al computers and it will execute much faster.


    \_(ツ)_/

    Tuesday, August 25, 2015 5:22 PM