Solved

Batch script to scan and echo the result if .pst exist

Posted on 2016-11-17
4
35 Views
Last Modified: 2016-11-26
Looking to do a script that will

1) Use Dsquery to get list of users from the AD OU >  "OU=Blah Users,DC=blah,DC=com"
2) Then for each user above check to see if a .PST exist.
Example path: \\%username%W10\c$\Users\%username%\AppData\Local\Google\Google Apps Sync\
3) If exist Echo Username to a file.

Note: some users in OU may not have a PC so script should not get halted for those.

Thanks in advance.
0
Comment
Question by:GGHC
  • 2
4 Comments
 
LVL 35

Assisted Solution

by:Mahesh
Mahesh earned 250 total points
ID: 41892786
Try below

Import-Module ActiveDirectory -Erroraction Stop

$Allusers = Get-Aduser -Filter * -SearchBase "OU=usersOU,DC=domain,DC=com" -SearchScope Subtree -ErrorAction SilentlyContinue

[String]$Folderpath = "\\server\share\users\$([Environment]::UserName)\somefolder"

foreach ($user in $Allusers) {

if(Test-Path $Folderpath -Include *.pst)

{ 
    Write-Host "$($user.SamAccountName), Path found successfully" 
                                                                    }

else 
    { Write-Host "$($user.SamAccountName), Path did not found" 
                                                                }

}

Open in new window


replace $Folderpath with your once, do not alter variable code

Mahesh.
1
 
LVL 82

Assisted Solution

by:oBdA
oBdA earned 250 total points
ID: 41895480
This will generate a csv file with the results:
@echo off
setlocal enabledelayedexpansion
set StartNode=OU=Blah Users,DC=blah,DC=com
set FilePath=AppData\Local\Google\Google Apps Sync\*.pst
set OutFile=C:\Temp\pst.csv

set TmpFile=%Tmp%\~pst.tmp
>"%OutFile%" echo "UserName","ComputerName","PST","Error"
for /f %%a in ('dsquery.exe user "%StartNode%" -o SamId') do (
	set User=%%~a
	set Computer=%%~aW10
	set Error=
	set PST=False
	echo Processing !User! ...
	ping.exe -n 2 !Computer! | find.exe /i "TTL" >NUL
	if errorlevel 1 (
		set Error=Offline
		echo ... offline.
	) else (
		dir /b "\\!Computer!\C$\Users\!User!\%FilePath%" >NUL 2>"%TmpFile%"
		if errorlevel 1 (
			for /f "delims=" %%e in ('type "%TmpFile%"') do set Error=%%e
			echo ... Error: !Error!.
		) else (
			set PST=True
			echo ... file found.
		)
	)
	>>"%OutFile%" echo "!User!","!Computer!","!PST!","!Error!"
)
echo Results saved to '%OutFile%':
type "%OutFile%"

Open in new window

1
 

Accepted Solution

by:
GGHC earned 0 total points
ID: 41896615
Ended up using the following.

@echo off
setlocal enabledelayedexpansion

for /f %%a in ('dsquery computer -o rdn "OU=myuserss,DC=company,DC=com" -limit 0') do (
If Exist \\%%aw10\c$\users\%%a\AppData\Local\Google\Google Apps Sync\*.pst Echo >>%%a c:\Temp\Outlook.txt
)
0
 

Author Closing Comment

by:GGHC
ID: 41902217
clean and basic
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This is an article about Leadership and accepting and adapting to new challenges. It focuses mostly on upgrading to Windows 10.
A Bare Metal Image backup allows for the restore of an entire system to a similar or dissimilar hardware. They are highly useful for migrations and disaster recovery. Bare Metal Image backups support Full and Incremental backups. Differential backup…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…

708 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

20 Experts available now in Live!

Get 1:1 Help Now