0

I have the following script to list big files on list of servers. Unfortunately it is not listing anything. However, if I replace the $_.Name by the string D:\ it works fine.

$servers = Get-Content "servers1.txt" | Select-String -pattern `
    "^[^#]"
foreach ($line in $servers) {
    $svr = echo $line | %{$_.Line.split(':')[2]}
    Get-WmiObject Win32_Volume -ComputerName $svr -ErrorAction SilentlyContinue |
    Select-Object __SERVER,Name |
    foreach {
        Invoke-command {Get-ChildItem -path $_.Name -rec | Where-Object `
        -FilterScript {($_.Length -ge 3GB) -and ($_.Name -notlike "*.mdf")}} -computername $svr
    }
} 

Thanks for any help.

1 Answer 1

1

This is scoping issue: in remote command $_.Name does not exist. Try this instead:

Invoke-command {
    param ($Path)
    Get-ChildItem -path $Path -rec | Where-Object {($_.Length -ge 3GB) -and ($_.Name -notlike "*.mdf")}
} -ComputerName $svr -ArgumentList $_.Name
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.