0

One liner works fine from my server to a remote computer. I want to take my one liner and convert it to a ForEach loop. I want to loop through all the computers in my environment.

One liner:

Write-output (Get-ChildItem '\\mycomputer\C$\Program Files\OSC\LogGen\LOGS').Count 

ForEach attempt (not working) foreach loop through list of computers and return count results for each computer in list.

$computerlist = gc "D:\computerlist.txt"

ForEach ($host in $computerlist) { 
Write-Output (Get-ChildItem "\\$host\C$\Program Files\LOGS").Count
}
1
  • 1
    not working is an absolutely useless problem description. In what way does it not work? Do you get the wrong results? Do you get an error? If so, what is the exact error message you're getting? You alone know what not working means, and if you want someone here to help you you need to share that information. We can't read your mind or see your screen from here. Commented Dec 13, 2016 at 1:09

1 Answer 1

4

Try look in to ps drives, They are really neat and i use them everywhere now.

New-PSDrive -Name U -PSProvider FileSystem -Root "\\$PC\C$"

This will create a new drive called U: that you can interact with just like a network drive. The main difference is this drive only exists in the scope of the script. When creating drives in a loop its inportant to close the drive to free up the letter for the next iteration.

ForEach ($PC in $computerlist) { 
    New-PSDrive -Name U -PSProvider FileSystem -Root "\\$PC\C$"
    Write-Output (Get-ChildItem "U:\Program Files\LOGS").Count
    Remove-PSDrive U -force
}
Sign up to request clarification or add additional context in comments.

1 Comment

Had to to change $host to $var I was getting an error, Host is a restricted variable.

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.