Timeout issue with GetChildItem

I wrote a script to remote 500+ 2k3/2k8 servers for specific files with get-Childitem. It first check if the file exits and if the patch exits. The status is logged in one file and the result is logged in another file.

For some reason I am not getting any result back.

1.If I run one individually, it works and I see the search results.
gci -Pa \\serverA\d$ -In access.log error.log -Re

Open in new window


2. I got many "specified network name" errors, but obviously machines are online if I do a dir
\\serverA\d$.

Get-ChildItem : The specified network name is no longer available.
At getFile.ps1:30 char:6
+         gci <<<<  -Pa $fullsearchpath -In $fileList -Re| % {
    + CategoryInfo          : ReadError: (\\serverA\d$\ora...man\recv\errors:String) [Get-ChildItem], IOException
    + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand

Open in new window


Complete code:
#################################################
$targetfile = "queryfilelist.txt";
$serverListfile = "testservers.txt";
$searchPath = "d$";
#################################################
if ( ! (test-path $targetfile) ) {
	write-host $targetfile not exist.
	break;
	} elseif ( ! (test-path $serverListfile) ) {
	write-host $serverListfile not exist.
	break;
	} else {
$fileList = gc $targetfile;
$serverList = gc $serverListfile;
$servcount = $serverList.count;
$logsuffix = Get-Date -Format "yyyyMM" ;
$log = "getFile-$logsuffix.log";
$out = "getFile-$logsuffix.out";
$progress = 1;
	foreach ($server in $serverList) {
	$date = Get-Date -Format "yyyyMMdd-hh:mm";
	$fullsearchpath = "\\$server\$searchpath";
	$logprefix="$date [$progress/$servcount][$($server.ToUpper())]";
	write-output "$logprefix - Searching file system." | Out-File -Encoding ASCII -a $log;
	if ( ! (test-connection $server -count 1 -q) ) {
		write-output "$logprefix - Host $server not reachable." | Out-File -Encoding ASCII -a $log;
		} elseif ( ! (test-path $fullsearchpath) ) {
		write-output "$logprefix - $fullsearchpath not exist." | Out-File -Encoding ASCII -a $log;
		} else {
		gci -Pa $fullsearchpath -In $fileList -Re| % {
		write-output "$logprefix - $($_.LastWriteTime) $($_.Length) $($_.FullName)"
		} | Out-File -Encoding ASCII -a -FilePath $out;
		}
	$progress++;
}

Open in new window

LVL 21
MazdajaiAsked:
Who is Participating?
 
QlemoConnect With a Mentor Batchelor, Developer and EE Topic AdvisorCommented:
The error message results from an interruption of the gci (as you can see in the error message, the path is some levels deep). Probably a network bandwidth issue, resulting in a timeout.
It is much better to let the dir command run remotely, and to only transfer the result to the controlling machine. That can be done by using WMI, PS Remoting or external tools like psexec from www.sysinternals.com.

Anyway, you should see at leat the attempts to access the servers in your log file, as that is written immediately. Strange you don't ...
0
 
MazdajaiAuthor Commented:
Yup. I agreed the methodology but not all servers (Some are w2k3s) have powershell therefore I didn't go that route.

I commented out the following logics (test-connection and test-path) and it seems to resolved the issue.

Is there a way I can send the stderr of them to a log file?

if ( ! (test-connection $server -count 1 -q) ) {
		write-output "$logprefix - Host $server not reachable." | Out-File -Encoding ASCII -a $log;
		} elseif ( ! (test-path $fullsearchpath) ) {
		write-output "$logprefix - $fullsearchpath not exist." | Out-File -Encoding ASCII -a $log;

Open in new window

0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
test-connection -quiet doesn't send anything to stderr - it just results in a boolean. The same applies to test-path.
0
 
MazdajaiAuthor Commented:
Thanks.

It turns to be access issue. The user was using the wrong account to run the code. :(
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.