Solved

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

Posted on 2016-11-17
4
41 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 36

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 83

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

777 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