Get file count in same folder on list of servers

I have a list of servers and they all have a folder called c:\auditLogs. I need to make sure there is never more than 1 file in that folder. I need to get the server name and a file count in the result set. It isn’t working.

[Pre]$myfile = “c:\downloads\auditserverList.csv”

$ServerList = Get-Content $myfile

$scriptBlock = (get-childitem c:\auditLogs | measure-object).count

$results = @()
foreach ($i in $ServerList){
SystemName = $env:COMPUTERNAME
FileCount = $scriptBlock.Count
write-host “Getting file account from each server: ” $i
$results += $scriptBlock.count

Your script never actually runs commands remotely. To run a command/script on a remote machine you need to use something like Invoke-Command or a cmdlet with a -ComputerName parameter. Also, I see you are doing Get-Content on a .csv file. If this is truly a comma seperated value file, then you need to use Import-Csv and access the servername value by it’s header as the property. i.e.

$myfile = “c:\downloads\auditserverList.csv”
$ServerList = (Import-Csv -Path $myfile).ServerName

Otherwise if it is just a text file with the server name on each line, you can use Get-Content.

$myfile = “c:\downloads\auditserverList.csv”
$ServerList = Get-Content $myfile

Invoke-Command -ComputerName $ServerList -ScriptBlock {
        FileCount =  (get-childitem c:\auditLogs | measure-object).count

Welcome to the forums.

When you post code or error messages or sample data oder console output format it as code please.
Thanks in advance.

Assumed your CSV is a proper CSV file and has the header “ServerName” the following snippet should be enough:

$ComputerName = 
    Import-Csv -Path 'c:\downloads\auditserverList.csv' | 
        Select-Object -ExpandProperty ServerName

Invoke-Command -ComputerName $ComputerName -ScriptBlock {
        ComputerName = $env:COMPUTERNAME
        FileCount    = (Get-ChildItem -Path c:\auditLogs -File).count

To be more accurate you could also use cmdlets with CIMSession or PSSession to run remotely. Or just enter an interactive session with a single remote machine in ISE for example.

Also consider this approach doesn’t scale if one is doing a larger number of servers (pick the number). If this is the case, I would have the checking process run as a service on each of the servers rather than trying to do a one to many approach. Then have your one to many approach just validate that specific services are running on each server. If you have really really big environment, then multi-thread your one to many approach so you can check 20 servers or better concurrently.

Great point cadayton! Cmdlets like Invoke-Command will take a collection (array) as an argument for the -ComputerName parameter. When you do that, the scriptblock would be transmitted to all of the remote hosts identified to work in parallel and as the results come back a new property called PSComputerName will be added to the objects identifying where the results came from so no need to manually put the commputername in your script. If instead you call Invoke-Command or any other remoting command in a loop with each iteration containing only one remote host then it would be in serial and not very scalable.

Thanks everyone! I got it working.