Solved

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

Posted on 2014-03-04
42
526 Views
Last Modified: 2014-03-10
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.
0
Comment
Question by:rdefino
  • 21
  • 14
  • 5
  • +2
42 Comments
 
LVL 35

Expert Comment

by:Joseph Daly
Comment Utility
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
 
LVL 35

Expert Comment

by:Mahesh
Comment Utility
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
 

Author Comment

by:rdefino
Comment Utility
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
 

Author Comment

by:rdefino
Comment Utility
I think I found it. -Filter is needed

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

Expert Comment

by:becraig
Comment Utility
Get-adcomputer -filter * | % {
Test-Path -path "\\$_.name\c$\rest\of\path
}
0
 

Author Comment

by:rdefino
Comment Utility
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
 
LVL 28

Expert Comment

by:becraig
Comment Utility
You need to close the double quotes
 Get-adcomputer -filter * | % {Test-Path -path "\\$_.name\c$\Windows\System32\oobe\Info\backgrounds\backgrounddefault.jpg"}
0
 

Author Comment

by:rdefino
Comment Utility
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
 
LVL 28

Expert Comment

by:becraig
Comment Utility
$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
 

Author Comment

by:rdefino
Comment Utility
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
 
LVL 28

Expert Comment

by:becraig
Comment Utility
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
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
Comment Utility
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
 

Author Comment

by:rdefino
Comment Utility
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
 
LVL 28

Expert Comment

by:becraig
Comment Utility
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
 

Author Comment

by:rdefino
Comment Utility
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
 
LVL 28

Expert Comment

by:becraig
Comment Utility
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
 

Author Comment

by:rdefino
Comment Utility
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
 
LVL 28

Expert Comment

by:becraig
Comment Utility
Yes the ones that pass Test-Connection will let you know if it exists or not.
0
 

Author Comment

by:rdefino
Comment Utility
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
 

Author Comment

by:rdefino
Comment Utility
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
 
LVL 28

Expert Comment

by:becraig
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:rdefino
Comment Utility
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
 
LVL 28

Expert Comment

by:becraig
Comment Utility
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
 

Author Comment

by:rdefino
Comment Utility
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
 
LVL 28

Expert Comment

by:becraig
Comment Utility
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
 

Author Comment

by:rdefino
Comment Utility
OU script found 23 systems and 28 OU's.

1st script is still running.
0
 

Author Comment

by:rdefino
Comment Utility
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
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
Comment Utility
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
 

Author Comment

by:rdefino
Comment Utility
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
 
LVL 28

Expert Comment

by:becraig
Comment Utility
$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
 

Author Comment

by:rdefino
Comment Utility
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
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
Comment Utility
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
 

Author Comment

by:rdefino
Comment Utility
Missing bracket error again.
0
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
Comment Utility
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
 

Author Comment

by:rdefino
Comment Utility
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
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
Comment Utility
remove the -eq $true  again you are quick.... we entered submit at about the same time but I just beat you
0
 

Author Comment

by:rdefino
Comment Utility
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
 
LVL 28

Expert Comment

by:becraig
Comment Utility
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
 

Author Comment

by:rdefino
Comment Utility
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
 
LVL 28

Accepted Solution

by:
becraig earned 500 total points
Comment Utility
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
 
LVL 28

Expert Comment

by:becraig
Comment Utility
Let me know if the above works for you, it should since I had a mis-named variable.
0
 

Author Closing Comment

by:rdefino
Comment Utility
It worked awesome!!! Thank you so much for the help. Wish I knew how to scripted, trying to learn.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Introduction You may have a need to setup a group of users to allow local administrative access on workstations.  In a domain environment this can easily be achieved with Restricted Groups and Group Policies. This article will demonstrate how to…
Synchronize a new Active Directory domain with an existing Office 365 tenant
This tutorial will show how to push an installation of Backup Exec to an additional server in both 2012 and 2014 versions of the software. Click on the Backup Exec button in the upper left corner. From here, select Installation and Licensing, then I…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now