lasthope
asked on
Need assistance with a PowerShell script used to find ALL .exe files on the C: drive of multiple computers
I would like to use PowerShell or VBScript to find all .exe files on the C: drive of multiple computers then write the path and file to a specified text output file. Only the C: drive and all folders, including the root.
I've started with the following script but it seems to repeat what it found on the first computer for all subsequent computers. Can this be done in PowerShell AND VBScript if it isn't too big a project?
Thanks in advance for your help!!
Below is the PowerShell Script:
# PowerShell script to list specified file types in any folder / any drive
$strComputers = Get-Content -Path "C:\Test\ServerList.txt"
foreach($strComputer in $strComputers)
{
$Dir = get-childitem C:\ -recurse
$List = $Dir | where {$_.extension -eq ".exe"}
# the "- Width 200" statement below can be increased if you have Long Paths
$List |ft fullname -wrap |out-file C:\Test\dircom.txt -Width 200 -append
}
I've started with the following script but it seems to repeat what it found on the first computer for all subsequent computers. Can this be done in PowerShell AND VBScript if it isn't too big a project?
Thanks in advance for your help!!
Below is the PowerShell Script:
# PowerShell script to list specified file types in any folder / any drive
$strComputers = Get-Content -Path "C:\Test\ServerList.txt"
foreach($strComputer in $strComputers)
{
$Dir = get-childitem C:\ -recurse
$List = $Dir | where {$_.extension -eq ".exe"}
# the "- Width 200" statement below can be increased if you have Long Paths
$List |ft fullname -wrap |out-file C:\Test\dircom.txt -Width 200 -append
}
ASKER
Gave the suggested change a quick try. Below are the error messages.
Get-ChildItem : The UNC path should be of the form \\server\share.
At C:\TEST\ListFiles(Use This One).ps1:7 char:21
+ $Dir = get-childitem <<<< \\$_\C$\ -recurse
+ CategoryInfo : InvalidArgument: (\\\C$\:String) [Get-ChildItem], ArgumentException
+ FullyQualifiedErrorId : ItemExistsArgumentError,Mi crosoft.Po werShell.C ommands.Ge tChildItem Command
Get-ChildItem : Cannot find path '\\\C$\' because it does not exist.
At C:\TEST\ListFiles(Use This One).ps1:7 char:21
+ $Dir = get-childitem <<<< \\$_\C$\ -recurse
+ CategoryInfo : ObjectNotFound: (\\\C$\:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.Pow erShell.Co mmands.Get ChildItemC ommand
Get-ChildItem : The UNC path should be of the form \\server\share.
At C:\TEST\ListFiles(Use This One).ps1:7 char:21
+ $Dir = get-childitem <<<< \\$_\C$\ -recurse
+ CategoryInfo : InvalidArgument: (\\\C$\:String) [Get-ChildItem], ArgumentException
+ FullyQualifiedErrorId : ItemExistsArgumentError,Mi
Get-ChildItem : Cannot find path '\\\C$\' because it does not exist.
At C:\TEST\ListFiles(Use This One).ps1:7 char:21
+ $Dir = get-childitem <<<< \\$_\C$\ -recurse
+ CategoryInfo : ObjectNotFound: (\\\C$\:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.Pow
you could do something like that in VBScript:
On Error Resume Next
'open the file system object
Set FSO = CreateObject("Scripting.FileSystemObject")
set WSHShell = wscript.createObject("wscript.shell")
Set Stream = FSO.OpenTextFile("C:\Test\ServerList.txt")
computers = Split(Stream.ReadAll, vbNewLine)
Stream.Close
OutputFilePath="C:\Test\Output.txt"
Set oReportFile = FSO.CreateTextFile(OutputFilePath, True)
For Each computer In computers
Set objWMIService = GetObject("winmgmts:\\" & computer & "\root\cimv2")
Set colFiles = objWMIService. _
ExecQuery("Select * From CIM_DataFile Where Extension = 'exe'")
If colFiles.Count = 0 Then
Wscript.Echo "no exe files found"
Else
For Each oFile in colFiles
oReportFile.WriteLine(oFile.FileName)
Next
End If
Next
Set FSO=nothing
With PowerShell you can try something like that:
$files = get-wmiobject -query "Select * from CIM_DataFile Where Extension = 'exe'" -ComputerName (Get-Content C:\Test\ServerList.txt)
foreach($file in $files)
{
}
ASKER
YZlat - I have a VBscript that will do the list similar to the one you offered here. Only problem is with the "\root\cimv2" the script finds .exe files on ALL drives. I only want the specified drive, in this case C:
What can be done to check only a specified hard drive?
What can be done to check only a specified hard drive?
try
or
$files = get-wmiobject -query "Select * from CIM_DataFile Where Extension = 'exe'" -ComputerName (Get-Content C:\Test\ServerList.txt)
| out-File "C:\Test\Output.txt"
or
$files = get-wmiobject -query "Select * from CIM_DataFile Where Extension = 'exe'" -ComputerName (Get-Content C:\Test\ServerList.txt)
foreach($file in $files)
{
$file.name out-File "C:\Test\Output.txt"
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
On Error Resume Next
'open the file system object
Set FSO = CreateObject("Scripting.FileSystemObject")
set WSHShell = wscript.createObject("wscript.shell")
Set Stream = FSO.OpenTextFile("C:\Test\ServerList.txt")
computers = Split(Stream.ReadAll, vbNewLine)
Stream.Close
OutputFilePath="C:\Test\Output.txt"
Set oReportFile = FSO.CreateTextFile(OutputFilePath, True)
For Each computer In computers
Set objWMIService = GetObject("winmgmts:\\" & computer & "\root\cimv2")
Set colFiles = objWMIService. _
ExecQuery("Select * From CIM_DataFile Where Extension = 'exe' AND Drive='c:' AND Path='Test'")
If colFiles.Count = 0 Then
Wscript.Echo "no exe files found"
Else
For Each oFile in colFiles
oReportFile.WriteLine(oFile.FileName)
Next
End If
Next
Set FSO=nothing
ASKER
YZlat
The PowerShell script works but it again gives ALL drives. I would like to only get results from C:.
The VBScript works but doesn't include the "path" in the output. Any idea what to change?
I think the VBScript is going to be too slow. I'm testing on 2 machines but I have over 120 to run this report for. VBScript is great for many things but I think I'll pass here and stick with PowerShell.
The PowerShell script works but it again gives ALL drives. I would like to only get results from C:.
The VBScript works but doesn't include the "path" in the output. Any idea what to change?
I think the VBScript is going to be too slow. I'm testing on 2 machines but I have over 120 to run this report for. VBScript is great for many things but I think I'll pass here and stick with PowerShell.
Try this in your PowerShell query:
Get-WmiObject -Query "SELECT * From CIM_DataFile WHERE Name LIKE '%.exe' AND Name LIKE 'C:\\%'"
Get-WmiObject -Query "SELECT * From CIM_DataFile WHERE Name LIKE '%.exe' AND Name LIKE 'C:\\%'"
Here's one option, just a slight change to your original code:
Rob.
$strComputers = Get-Content -Path "C:\Test\ServerList.txt"
foreach($strComputer in $strComputers)
{
Write-Host "Scanning \\$strComputer\C$\"
$Files = gci "\\$strComputer\C$\" -recurse | Where {$_.extension -eq ".exe"}
$Files | ft fullname -wrap|out-file C:\Test\dircom.txt -Width 200 -append
}
Rob.
IMO, the CIM_DataFile query takes too long, even if you use
And Drive = 'C:'
to restrict the search to local drives.
Rob.
And Drive = 'C:'
to restrict the search to local drives.
Rob.
ASKER
Sorry ALL. I will give these a try, but I'm getting, and giving, a moving target. Now I need to find .exe files on ALL server hard drives and output to the same text file. One other important thing, the output must include the path to the .exe.
Thanks again and sorry about the moving target.
Thanks again and sorry about the moving target.
ASKER
Rob - I agree with you that the CIM_Datafile query takes too long. And I have 120 servers with multiple drives to search.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Each of these scripts works to the extent of security on our systems. Biggest barrier is no administrative shares, so scripts searching C$, D$ so forth cannot be used. If it were an easy script to change autoshareserver registry key to 1 and back to 0 with the rest of the code sandwiched inbetween this may have been a whole lot easier on me, and possibly faster to complete. As it is it was a long excercise, but the results appear to be accurate.
Thank you for your expert assistance.
Thank you for your expert assistance.
We didn't know you had admin shares disabled.....in that case WMI is your only option. One option though, if WMI does work, is that you could use WMI to *create* a share on the root of the drive, then do the search, and remove the share, but you seem to have moved on anyway.
Glad we could help.
Rob.
Glad we could help.
Rob.
$Dir = get-childitem C:\ -recurse
to this
$Dir = get-childitem \\$_\C$\ -recurse
and it should then search the remote C$ share.
I also think VBScript would be too slow....if I were to do it in VBScript, I'd just shell out to the DOS dir command anyway....
Regards,
Rob.