query computers remotely to find if specific profile folder exist with

hello,

i need from a csv computer list to execute this script to get all profile folder contain X letter

Get-WmiObject -Query "SELECT Name FROM Win32_Directory WHERE Drive = 'C:' AND Path = '\\Users\\' AND Name like '%X%'"
the result must be on a csv file like this:

computername,Name
computer1,c:\users\shfdkjsdXdsqfd
....

if a computer is not on or not respond, the scipt must go to the next computer.

thanks for help
cawasakiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Try this:
Import-Csv -Path C:\Temp\ComputerList.csv | ForEach-Object {
	$computerName = $_.ComputerName
	Write-Host "Processing $($computerName)"
	$result = '' | Select-Object -Property @{n='ComputerName'; e={$computerName}}, Name, Error
	Try {
		$result.Name = (Get-WmiObject -Query "SELECT Name FROM Win32_Directory WHERE Drive = 'C:' AND Path = '\\Users\\' AND Name like '%X%'" -ErrorAction Stop -ComputerName $computerName).Name
	} Catch {
		$result.Error = $_.Exception.Message
	}
	$result
} | Export-Csv -NoTypeInformation -Path C:\Temp\XUserProfiles.csv

Open in new window

0
cawasakiAuthor Commented:
hello,

the script work, but with certain computer the script stop at processing computer99 and not go to the next one, its possible to put a timer of 5 sec for exemple for every computer?
0
oBdACommented:
What makes these machines special? I've never had Get-WmiObject just hang there. It might take a while, but Get-WmiObject should always time out.
Is that behavior reliably reproducible for these machines, or is it intermittent?
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

cawasakiAuthor Commented:
YES i have get the problem with 2 machine and it hang on processing.
0
oBdACommented:
Try this then:
Import-Csv -Path C:\Temp\ComputerList.csv | ForEach-Object {
	$computerName = $_.ComputerName
	Write-Host "Processing $($computerName) " -NoNewline
	$Timeout = (Get-Date).AddSeconds(10)
	$job = Start-Job -ArgumentList $computerName, '%X%' -ScriptBlock {
		Param($ComputerName, $WmiPattern)
		$result = '' | Select-Object -Property @{n='ComputerName'; e={$ComputerName}}, Name, Error
		Try {
			$result.Name = (Get-WmiObject -Query "SELECT Name FROM Win32_Directory WHERE Drive = 'C:' AND Path = '\\Users\\' AND Name like '$($WmiPattern)'" -ErrorAction Stop -ComputerName $ComputerName).Name
		} Catch {
			$result.Error = $_.Exception.Message
		}
		$result
	}
	Do {
		Write-Host '.' -NoNewline
		Start-Sleep -MilliSeconds 500
	} Until (($job.State -ne 'Running') -or ((Get-Date) -gt $Timeout))
	If (-not ($result = Receive-Job -Job $job | Select-Object -Property ComputerName, Name, Error)) {
		$result = '' | Select-Object -Property @{n='ComputerName'; e={$ComputerName}}, Name, @{n='Error'; e={'TIMEOUT: Not responding!'}}
	}
	Remove-Job -Job $job -Force
	If ($result.Name) {
		Write-Host " $($result.Name)" -ForegroundColor Green
	} Else {
		Write-Host " $($result.Error)" -ForegroundColor Red
	}
	$result
} | Export-Csv -NoTypeInformation -Path C:\Temp\XUserProfiles.csv

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cawasakiAuthor Commented:
hello,

same think it hang on processing
0
cawasakiAuthor Commented:
finally its work TIMEOUT: Not responding!  but the timeout is very long
1
oBdACommented:
You might want to try it from a different machine, just in case this is an issue with the local machine's WMI, not the remote ones.
1
cawasakiAuthor Commented:
already test it from other server same think, i have also try it from my computer machine same problem
0
oBdACommented:
Can't tell how optimized the Win32_Directory WMI query is - could it be that the machines are just really slow on delivering results?
How about just querying the UNC?
$Pattern = '*X*'
Import-Csv -Path C:\Temp\ComputerList.csv | ForEach-Object {
	$computerName = $_.ComputerName
	Write-Host "Processing $($computerName)"
	Try {
		If ($Directories = Get-ChildItem -Path "\\$($ComputerName)\C$\Users" -Directory -ErrorAction Stop | Where-Object {$_.Name -like $Pattern}) {
			$Directories | Select-Object -Property  @{n='ComputerName'; e={$computerName}}, @{n='Name'; e={$_.Name}}, Error
		}
	} Catch {
		$_ | Select-Object -Property @{n='ComputerName'; e={$computerName}}, Name, @{n='Error'; e={$_.Exception.Message}}
	}
} | Export-Csv -NoTypeInformation -Path C:\Temp\XUserProfiles.csv

Open in new window

1
cawasakiAuthor Commented:
this one do the job, thank you
1
Senior IT System EngineerIT ProfessionalCommented:
Which one works for you finally ?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.