Link to home
Start Free TrialLog in
Avatar of cawasaki
cawasaki

asked on

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
Avatar of oBdA
oBdA

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

Avatar of cawasaki

ASKER

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?
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?
YES i have get the problem with 2 machine and it hang on processing.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hello,

same think it hang on processing
finally its work TIMEOUT: Not responding!  but the timeout is very long
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.
already test it from other server same think, i have also try it from my computer machine same problem
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

this one do the job, thank you
Which one works for you finally ?