0

I need some help. I cant’t find what’s wrong with my PowerShell script.

The goal is quite simple. I have to find (.*pst)-files on the users profile on domain computers in the network.
Location to search is “C:\Users\”. List of the PC names where exported to listcomputer.txt.
The trouble is the script run with no errors and no message at all.

$computers = Get-Content c:\temp\listcomputer.txt
$filePath = "C:\Users\" 
foreach($computer in $computers)
    {
    if(Test-Connection -ComputerName $computer -Quiet) 
{
Invoke-Command -ComputerName $computer -ScriptBlock 
{Get-ChildItem -Path $filePath -Recurse -Include '*.pst, *.ost'} }}


First of all I’ve to check connectivity to hosts by Test-Connection cmdlet.

Separately each of the command run successfully. I've tried it.
For example: Test-Connection -ComputerName $computer runs with “true” result and it’s OK.
Also
Invoke-Command -ComputerName $computer -ScriptBlock {Get-ChildItem -Path $filePath -Recurse -Include '*.pst'}
The result is displayed data with information about files were find in folders.
But all together run with no visible result in the PowerShell console
console view result

Regards!

3
  • Try to add $a = Get-RemoteAccessConnectionStatisticsSummary return $a at the end of your ScriptBlock and let me know the result. Commented Apr 20, 2021 at 19:13
  • 3
    $filePath is unknown in the scriptblock. Try Get-ChildItem -Path $using:filePath .. or hardcode the 'C:\Users' there. You can also define it as param(..) in the scriptblock and use ArgumentList in Invoke-Command Commented Apr 20, 2021 at 19:16
  • You can read about what Theo mentioned in above comment in about_remote_variables and about_scopes Commented Apr 20, 2021 at 20:15

1 Answer 1

1

.pst can be located anywhere, even other drives, or attached storage. You are only looking for C:\.

So maybe this refactor to hit all potential connected drives.:

Get-Content -Path 'c:\temp\listcomputer.txt' | 
ForEach-Object { 
    if(Test-Connection -ComputerName $PSItem -Quiet)
    {
        Invoke-Command -ComputerName $PSItem -ScriptBlock {
            (Get-CimInstance -ClassName Win32_LogicalDisk).DeviceID | 
            ForEach-Object {
                If ($PSItem -eq 'C:')
                {Get-ChildItem -Path "$PSItem\Users" -Recurse -Include '*.pst, *.ost'}
                Else {Get-ChildItem -Path $PSItem -Recurse -Include '*.pst, *.ost'}
            }
        } 
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.