How to script a way to find a certain file on multiple systems

I need to check all my windows systems in my domain for a file "backgrounddefault.jpg".

The full path is C:\Windows\System32\oobe\Info\backgrounds\

I need to get a count and system names for how many systems have this file in this location.

Is there a way to script using the systems in AD to output this to a csv file?

My account have access to all systems.
rdefinoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Joseph DalyCommented:
It going to be a little bit tough because not all computers may be on at the same time but you could try something like. The syntax may not be right because I dont have AD or powershell in front of me to test.

get-adcomputer * | test-path \\$_.name\C$\Windows\System32\oobe\Info\backgrounds\backgrounddefault.jpg

http://technet.microsoft.com/en-us/library/ee177015.aspx
0
MaheshArchitectCommented:
May be this is indirect answer to your question but it will useful hopefully

Use GPO preferences under computer configuration \ Preferences and add their new file in update mode and place their source and destination path for above JPG file

Apply this GPO on OU containing computers.
During every reboot or whenever policy updates on client computers, it will check if complete path exists with file, if its not found then it will create any missing folder in the path and copy file from source

So in that case you don't have to worry about file.
Because if your active directory is not cleaned properly, you may get lots of stale computer records there and then it will be dificult task to check if the file is exists on each computers
through scripts

Hope that helps

Mahesh
0
rdefinoAuthor Commented:
Hi joe,, I tried the script, but I get:   Get-ADComputer : Cannot find an object with identity: '*' under: 'DC=global,DC=mydomain,DC=com'.

Also, not finding anything related to what variable would be used for "*"

Hi Mahesh,

I don;t need to make any changes on the system as of now. A folder and file were created on some system using a GPO and we are just trying to get a count on how many systems have this file on the system now.
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

rdefinoAuthor Commented:
I think I found it. -Filter is needed

get-adcomputer -filter * | test-path \\$_.name\C$\Windows\System32\oobe\Info\backgrounds\backgrounddefault.jpg
0
becraigCommented:
Get-adcomputer -filter * | % {
Test-Path -path "\\$_.name\c$\rest\of\path
}
0
rdefinoAuthor Commented:
Hi becraig,

I tried Get-adcomputer -filter * | % {Test-Path -path "\\$_.name\c$\Windows\System32\oobe\Info\backgrounds\backgrounddefault.jpg}

ad all I get is >> prompt
0
becraigCommented:
You need to close the double quotes
 Get-adcomputer -filter * | % {Test-Path -path "\\$_.name\c$\Windows\System32\oobe\Info\backgrounds\backgrounddefault.jpg"}
0
rdefinoAuthor Commented:
So I see alot of falses, but not sure what systems they are for, which is fine, becase I just need to know what systems have the jpg file on them.

But I do see some "test path: the given path's format is not supported..."

Is there a way to get the output to show the system names of the systems that have the jpg file?

Not sure is this will show it.


False
False
False
False
Test-Path : The given path's format is not supported.
At line:1 char:41
+  Get-adcomputer -filter * | % {Test-Path <<<<  -path "\\$_.name\c$\Windows\System32\oobe\Info\backgrounds\backgroundd
efault.jpg"}
    + CategoryInfo          : InvalidOperation: (\\CN=TC-systemname\0...ounddefault.jpg:String) [Test-Path], NotSupportedEx
   ception
    + FullyQualifiedErrorId : ItemExistsNotSupportedError,Microsoft.PowerShell.Commands.TestPathCommand

False
False
False
0
becraigCommented:
$Report = @()

 Get-adcomputer -filter * | % {
$Server= $_.name
If (test-connection $server){
$response= Test-Path -path "\\$_.name\c$\Windows\System32\oobe\Info\backgrounds\backgrounddefault.jpg"
$Report+= $Server, $response
} }
$report | out-file file.txt
0
rdefinoAuthor Commented:
Since it was word wraped when pasting I tried to clean it up, but it's not working:


PS C:\Windows\system32>  Get-adcomputer -filter * | % {$Server= $_.name If (test-connection $server){$response= Test-Pat
h -path "\\$_.name\c$\Windows\System32\oobe\Info\backgrounds\backgrounddefault.jpg" $Report+= $Server, $response} } $rep
ort | out-file file.txt


Unexpected token 'if' in expression or statement.
At line:1 char:51
+  Get-adcomputer -filter * | % {$Server= $_.name If <<<<  (test-connection $server){$response= Test-Path -path "\\$_.n
ame\c$\Windows\System32\oobe\Info\backgrounds\backgrounddefault.jpg" $Report+= $Server, $response} } $report | out-file
 file.txt
    + CategoryInfo          : ParserError: (if:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken
0
becraigCommented:
The { must be on the same line with %


Eg

| % {
Then the rest of the script can follow, sorry I'm on my phone.

Get-adcomputer -filter * | % {
$Server= $_.name If (test-connection $server){$response= Test-Path -path "\\$server\c$\Windows\System.......
0
David Johnson, CD, MVPOwnerCommented:
Get-adcomputer -filter * | % 
     {
     $Server= $_.name If (test-connection $server)
          {
          $response= Test-Path -path "\\$_.name\c$\Windows\System32\oobe\Info\backgrounds\backgrounddefault.jpg" 
          $Report+= $Server, $response
          }
       } 
$report | out-file file.txt

Open in new window

0
rdefinoAuthor Commented:
So I saved this into a .ps1 file and when I run it, It get:
PS C:\> .\back.ps1
Unexpected token 'if' in expression or statement.
At C:\back.ps1:3 char:25
+      $Server= $_.name If <<<<  (test-connection $server)
    + CategoryInfo          : ParserError: (if:String) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken

Is this the wrong way to run it?
0
becraigCommented:
Ok I am on my computer :)

$Report = @()
Get-adcomputer -filter * % {
$Server= $_.name
If (test-connection $server)
{
write-host "$server responded Checking ..."
$response = Test-Path -path "\\$server\c$\Windows\System32\oobe\Info\backgrounds\backgrounddefault.jpg"
$Report += $Server, $response
} 
}
$report | out-file file.txt 

Open in new window



Just tested format is correct and works for me.

Save as ps1 and run
0
rdefinoAuthor Commented:
this is what is exactly in my .ps1 file:

$Report = @()
Get-adcomputer -filter * % {
$Server= $_.name
If (test-connection $server)
{
write-host "$server responded Checking ..."
$response = Test-Path -path "\\$server\c$\Windows\System32\oobe\Info\backgrounds\backgrounddefault.jpg"
$Report += $Server, $response
}
}
$report | out-file c:\background.txt

----------------------------------------------
when I run it I get:

PS C:\> .\back.ps1
Get-ADComputer : A positional parameter cannot be found that accepts argument '%'.
At C:\back.ps1:2 char:15
+ Get-adcomputer <<<<  -filter * % {
    + CategoryInfo          : InvalidArgument: (:) [Get-ADComputer], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.GetADComputer
0
becraigCommented:
I missed a pipe too early in the morning.
$Report = @() 
Get-adcomputer -filter * | % {
$Server= $_.name 
If (test-connection $server)
 { 
write-host "$server responded Checking ..."
 $response = Test-Path -path "\\$server\c$\Windows\System32\oobe\Info\backgrounds\backgrounddefault.jpg" 
$Report += $Server, $response 
}
 } 
$report | out-file file.txt

Open in new window

0
rdefinoAuthor Commented:
Ok, so it's running. :) Thank you all.

Now I see alot of "testing connection to computer" :failed.

I take it those are the system take are not online.

Will online the ones that are online and that have this jpg file be logged in the out put file?
0
becraigCommented:
Yes the ones that pass Test-Connection will let you know if it exists or not.
0
rdefinoAuthor Commented:
Odd, it's been running for 30mins and have not logged on system it found online. It's like it's checking every system that's not online.  I would think it would have  found at least on system by now.
0
rdefinoAuthor Commented:
so the script finish running, but it didn't find any system with the file. I know it definitely didn't check the 8000 or so systems we have in our domain.

The output file show about 15 system marked as false.

notsure what went wrong here. Any thoughts?
0
becraigCommented:
We can remove the Test-connection option

I am not sure your architecture so there might be a valid reason for the test connection failure.


One way to verify is to take a server at random and try to navigate to the path.
If you are able to navigate to the share successfully for one of the servers that failed ping, then we will need to change.

Also can you try to navigate to the path for one of the servers that failed ?

It might be a typo in the name.
0
rdefinoAuthor Commented:
So I cannot ping any of the systems that failed the test-connection.

But the 15 or so systems that are in the output file are pingable and do not contain the jpg file.

We have 1 domain with all system under a few different OU's.

It definitely didn't check all the systems.
0
becraigCommented:
The script only calls a built in AD cmdlet get-adcomputer

If they are all in a single domain and in the domain you are calling the script from you should have gotten all your computers in the result-set.

So
1) The script works as expected in that the results align with your manual check.


2) we should verify your list of computers to be sure you are getting all the servers
(I can create a script to output server count and unique OUs)
3) I will remove the test-connection param so you can check path w/o the ping test.

Are you able to connect  to any of the servers that did not respond to ping  ?
0
rdefinoAuthor Commented:
All the system that failed the connection test, I cannot ping. I just spot checked a bunch of them.

The only ones I can connect to are the ones in the output file. About 15 systems.

We have 1 domain and multiple DC's.
0
becraigCommented:
Ok I'm back so here are two things to help to figure out what happened.

1) we adjusted the script to not do the ping test
$Report = @() 
Get-adcomputer -filter * | % {
$Server= $_.name 
write-host "$server responded Checking ..."
 $response = Test-Path -path "\\$server\c$\Windows\System32\oobe\Info\backgrounds\backgrounddefault.jpg" 
$Report += $Server, $response 
}
$report | out-file file.txt

Open in new window

                                           
2) We added a script to give us a count of the number of OUS and the number of servers the script finds.
$ouname = @()
$servercount =@()
$servers = Get-adcomputer -filter * 

foreach ($server in $servers) 
{
$server = [string]$server
$servername = $server.split(',')[0] -join " " -replace 'CN='
$rep = "CN=" + $servername + ","
$ouname += "$server`n" -replace $rep
$servercount += $servername
}

$oulist = $ouname | sort -unique
$servercount | out-file c:\servers.txt
$oulist | out-file c:\oulist.txt
write-host "`nNumber of servers found" $servercount.count
write-host "Server List saved to c:\servers.txt`n`n"
write-host "Number of OUs found" $oulist.count
write-host "OU list saved to c:\oulist.txt`n`n"

Open in new window



Though your initial question has been answered this will help you to figure out if we are getting all you servers in your environment.
0
rdefinoAuthor Commented:
OU script found 23 systems and 28 OU's.

1st script is still running.
0
rdefinoAuthor Commented:
So the 1st script was processing systems then stop with this:

Get-ADComputer : Unable to contact the server. This may be because this server does not exist, it is currently down, or
 it does not have the Active Directory Web Services running.
At C:\back.ps1:2 char:15
+ Get-adcomputer <<<<  -filter * | % {
    + CategoryInfo          : ResourceUnavailable: (:) [Get-ADComputer], ADServerDownException
    + FullyQualifiedErrorId : Unable to contact the server. This may be because this server does not exist, it is curr
   ently down, or it does not have the Active Directory Web Services running.,Microsoft.ActiveDirectory.Management.Co
  mmands.GetADComputer
0
David Johnson, CD, MVPOwnerCommented:
Sometimes a Try/Catch helps

$Report = @() 
Get-adcomputer -filter * | % {
$Server= $_.name 
write-host "$server responded Checking ..."
try {
    $response = Test-Path -path "\\$server\c$\Windows\System32\oobe\Info\backgrounds\backgrounddefault.jpg" 
    $Report += $Server, $response 
    }
 catch [System.SystemException] {
    "Base Exception" 
     Write-Error("Something went wrong here")
    }
$report | out-file file.txt

Open in new window

0
rdefinoAuthor Commented:
Looks lie a bracket } is missing. I tried to add it in, but no luck on where it goes.

PS C:\> .\try_catch.ps1
Missing closing '}' in statement block.
At C:\try_catch.ps1:13 char:36
+ $report | out-file c:\try_catch.txt <<<<
    + CategoryInfo          : ParserError: (CloseBraceToken:TokenId) [], ParseException
    + FullyQualifiedErrorId : MissingEndCurlyBrace
0
becraigCommented:
$Report = @() 
Get-adcomputer -filter * | % {
$Server= $_.name 
write-host "$server responded Checking ..."
try {
    $response = Test-Path -path "\\$server\c$\Windows\System32\oobe\Info\backgrounds\backgrounddefault.jpg" 
    $Report += $Server, $response 
    }
 catch [System.SystemException] {
    "Base Exception" 
     Write-Error("Something went wrong here")
    }
    }
$report | out-file file.txt

Open in new window

0
rdefinoAuthor Commented:
So I get this same error after processing some systems. Bails at the same point as when I got this error before:

Get-ADComputer : Unable to contact the server. This may be because this server does not exist, it is currently down, or
 it does not have the Active Directory Web Services running.
At C:\back.ps1:2 char:15
+ Get-adcomputer <<<<  -filter * | % {
    + CategoryInfo          : ResourceUnavailable: (:) [Get-ADComputer], ADServerDownException
    + FullyQualifiedErrorId : Unable to contact the server. This may be because this server does not exist, it is curr
   ently down, or it does not have the Active Directory Web Services running.,Microsoft.ActiveDirectory.Management.Co
  mmands.GetADComputer
0
David Johnson, CD, MVPOwnerCommented:
a check (ping) on the connection will help if the computer is not-online but must respond to pings

if (test-connection -ComputerName $server -Quiet -Count 1) {
$Report = @() 
Get-adcomputer -filter * | % {
$Server= $_.name 
write-host "$server responded Checking ..."
try {
    $response = Test-Path -path "\\$server\c$\Windows\System32\oobe\Info\backgrounds\backgrounddefault.jpg" 
    $Report += $Server, $response 
    }
 catch [System.SystemException] {
    "Base Exception" 
     Write-Error("Something went wrong here")
    }
}
$report | out-file file.txt

Open in new window

0
rdefinoAuthor Commented:
Missing bracket error again.
0
David Johnson, CD, MVPOwnerCommented:
caught me in the middle of an edit
$Report = @() 
if (Test-Connection -ComputerName $_.name -Count 1 -Quiet) 
{
    Get-adcomputer -filter * | % {
    $Server= $_.name 
    write-host "$server responded Checking ..."
    try {
        $response = Test-Path -path "\\$server\c$\Windows\System32\oobe\Info\backgrounds\backgrounddefault.jpg" 
        $Report += $Server, $response 
        }
    catch [System.SystemException] {
        "Base Exception" 
        Write-Error("Something went wrong here")
        }
    }
    }
$report | out-file file.txt
                                                           

Open in new window

0
rdefinoAuthor Commented:
PS C:\> .\try_catch.ps1
Missing statement block after if ( condition ).
At C:\try_catch.ps1:2 char:60
+ if (Test-Connection -ComputerName $_.name -Count 1 -Quiet)  <<<< -eq $true
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : MissingStatementBlock
0
David Johnson, CD, MVPOwnerCommented:
remove the -eq $true  again you are quick.... we entered submit at about the same time but I just beat you
0
rdefinoAuthor Commented:
PS C:\> .\try_catch.ps1
Test-Connection : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Supply an argume
nt that is not null or empty and then try the command again.
At C:\try_catch.ps1:2 char:34
+ if (Test-Connection -ComputerName <<<<  $_.name -Count 1 -Quiet)
    + CategoryInfo          : InvalidData: (:) [Test-Connection], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.TestConnectionCommand
0
becraigCommented:
Looks like you are going in circles :(

Here is a quick solution.
1) your ADWS is not responding - simple solution
Run from a command promtp
 dsquery computer -name * -limit 100000 >> c:\folder\serverlist.txt

Then once this is done and the file is created run the script below:
$Report = @() 
gc c:\serverlist.txt | % {
$server = $_ -replace '"', ''
$servername = $server.split(',')[0] -join " " -replace 'CN='
try {
    $response = Test-Path -path "\\$server\c$\Windows\System32\oobe\Info\backgrounds\backgrounddefault.jpg" 
    $Report += $Servername, $response 
    }
 catch [System.SystemException] {
    "Base Exception" 
     Write-Error("Something went wrong here")
    }
    }
$report | out-file file.txt
                

Open in new window

0
rdefinoAuthor Commented:
So I created the serverlist. then ran the script.

Everything the output file is marked false. So I copied the jpg to a system and ran the script. It still found the system as false.
0
becraigCommented:
Ahh I see the issue above simple typo  (should be \\$servername  not \\$server):

$Report = @() 
gc c:\serverlist.txt | % {
$server = $_ -replace '"', ''
$servername = $server.split(',')[0] -join " " -replace 'CN='
try {
    $response = Test-Path -path "\\$servername\c$\Windows\System32\oobe\Info\backgrounds\backgrounddefault.jpg" 
    $Report += $Servername, $response 
    }
 catch [System.SystemException] {
    "Base Exception" 
     Write-Error("Something went wrong here")
    }
    }
$report | out-file file.txt
                

Open in new window

0

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
becraigCommented:
Let me know if the above works for you, it should since I had a mis-named variable.
0
rdefinoAuthor Commented:
It worked awesome!!! Thank you so much for the help. Wish I knew how to scripted, trying to learn.
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
Active Directory

From novice to tech pro — start learning today.