I am searching the drives of multiple servers for a specific file. The code looks something like this...
$servers = Get-ADComputer -filter {Name -like "SERVER-*"}
foreach ($server in $Servers){
$Server.Name
Invoke-Command -ComputerName $server.Name -ScriptBlock {
$filename="db2cmd.exe"
$listOfDrives=(get-psdrive | ? { $_.provider -match 'FileSystem'}).root
foreach ($Drive in $listOfDrives){
If ($Drive -ne "C:\" -and $Drive -ne "A:\") {
Get-ChildItem -Path $Drive -Filter $filename -Recurse -ErrorAction SilentlyContinue -Force
}
}
}
}
When I run it, in most instances, when the file is encountered, it returns the Directory where the file was found..
Directory: E:\IBM\DB2\SQLLIB\BIN
Mode LastWriteTime Length Name PSComputerName
---- ------------- ------ ---- --------------
-a---- 1/5/2024 11:32 AM 55888 db2cmd.exe ServerA
Yet on other servers, neither the Directory nor the Column titles are displayed.
-a---- 1/5/2024 11:32 AM 55888 db2cmd.exe ServerB
I really do not understand the reason for this as it is running the same command on each server. Any help you can provide would be appreciated.
I have tried different parameters, but they seem to change where and what the Get-ChildItems is looking for, and not the way the results are output.
PowerShell is normally used as a functional language and as such should only return one object.
As @Mathias R. Jessen, states. You are trying to
returnboth$Server.Nameand the result ofGet-ChildItemand PowerShell really don't care about the order returned, so they might interact and mix up in the output.You might get other results if you actually specify it should be pure outputs using
Write-Outputinstead.But the best option would be adding the
$Server.Nameproperty to the object returned byGet-ChildItem. Then there could be no object mix up.(On the other hand, that property is already available in PsComputerName since you are using remote sessions.)