Batch script not able to access remote drive when scheduled - file

I have a simple batch script that copies files locally from a Sharepoint drive that's mapped on the local machine. This runs just fine when being manually run, but when run using task scheduler, it seems as though the script cannot access the drive at all. Even a simple DIR print out fails. It's also worth noting that the user set to run the script in scheduler has full rights to everything involved and is the same account being used to run it manually.
A couple of the settings for the scheduled task:
set to run whether user is logged in or not
running with highest privileges
Any idea how I could get this working in scheduler?
Edit:
Screen shots of the scripts and the task run history
http://imgur.com/a/wCsET
EDIT 2
callCopyFiles.bat
del "C:\apache\htdocs\IESite\Reports\*.*?"
FOR /f "delims=" %%L in (directorylist.txt) do (
echo %%L >> log.txt
CALL copyfiles.bat "%%L"
)
copyfiles.bat
FOR /F "delims=" %%I IN ('DIR %1 /s /b /O:-D') DO (
COPY "%%I" "C:\apache\htdocs\IESite\Reports"
goto :end
)
:ender

My suspicion is that this line in callCopyFiles.bat:
CALL copyfiles.bat "%%L"
is failing because the current directory is different when your batch file is running as a scheduled task.
Try changing this line to list the full path...something like:
CALL C:\YourFolderHere\copyfiles.bat "%%L"
EDIT
Based on the comments, I think you likely need to map the Z: drive within the copyfiles.bat file.
EDIT 2
Don't specify the credentials when mapping the drive.
Just add NET USE Z: \\YourServer\ShareName to the copyfiles.bat file.

Related

Scheduled task and printer

i want to watch a folder on my Win7 64bit Machine for new pdf files - and print them autmatically when there is a pdf file in the folder. After printing, the pdf file should be moved in a subfolder. So, after some google research i did a small batch file.
cd "D:\print"
for %%i in (*.pdf) do (
"C:\Program Files\Tracker Software\PDF Viewer\PDFXCview.exe" /print "%%i"
timeout /T 10 /nobreak
move D:\print\*.pdf D:\print\printed
echo %%i
)
I stored this in folder d:\print as print.cmd . When i start the cmd by doubleclick, my printer starts working and the pdf file moves to the subfolder i defined (D:\print\printed).
To watch the folder, i had the idea to create a sheduled task that repeat this cmd-script all 5 minutes.
BUT:
This dont work, when the script is started via scheduled tasks, the printer is not working - the "movement" of the file instead, is working.
I entered in the scheduled task:
Program: C:\Windows\SysWOW64\cmd.exe
Argument: /c"d:\print\print.cmd"
Any idea, why i cant acces the printer via the scheduled task?
The printer is connected via usb.
Hope i could provide necessary information! Thanks for your answers!
Change this line: move D:\print\*.pdf to move /Y D:\print\%%i
Point the scheduled task to actually start your batch file instead of calling cmd and putting the path to your script in the arguments.
You could also edit the batch file and code it to loop every 5 minutes so you only have to start it once:
PushD %~dp0
:start
for %%i in ("D:\print\*.pdf") do (
"C:\Program Files\Tracker Software\PDF Viewer\PDFXCview.exe" /print "%%i"
move /y "%%i" "D:\print\printed"
echo %%i
)
timeout /T 300 /nobreak
goto start

Reg Query in script not working when executed remotely with psexec

I have recently gotten a script together for querying the reg for a temp folder path and cleaning the folder and it works great(Thanks Compo) but when used remotely with psexec it says, "The system was unable to find the specified registry key or value." Before I use PSEXEC to execute the batch I first have it copied to the C:\ and I use this to do that.
set /p cpu=
robocopy "\\nmcfs01\software\scripts\Jacob's Awesome Outlook Scripts" \\%cpu%\c$\Jacob'sTemp
That part goes well then I execute it with psexec with
psexec \\%cpu% -u administrator -i -d "C:\Jacob'sTemp\compo.bat"
That connects but when it executes the batch file it does not find the reg key. HOWEVER, if I go to the C:\Jacob'sTemp and double click on the compo.bat file it works fine and deletes out the files. Also, if I just use PSEXEC to execute it again outside of this script just separately after it is copied over it still does not work. Here is the script I am using for cleaning it up.
#Echo Off
SetLocal EnableExtensions
(Set OV=14.0)
Choice /C YN /M "Delete Outlook Temp Files?"
If ErrorLevel 2 Exit/B
Set "BK=HKCU\SOFTWARE\Microsoft\Office\"
Set "EK=\Outlook\Security"
Set "VN=OutlookSecureTempFolder"
For /F "Tokens=2*" %%I In ('Reg Query "%BK%%OV%%EK%" /V %VN%') Do Set "VD=%%J"
PushD "%VD%" && (RD/S/Q "%VD%" 2>Nul) && PopD
pause
REM The below commands will empty Jacob'sTemp:
If Exist "%SystemDrive%\Jacob'sTemp" (PushD "%SystemDrive%\Jacob'sTemp" && (
(RD/S/Q "%SystemDrive%\Jacob'sTemp" 2>Nul) && PopD
pause
REM The below commands without the first two characters will remove Jacob'sTemp
If Exist "%SystemDrive%\Jacob'sTemp" (RD/S/Q "%SystemDrive%\Jacob'sTemp"
Pause
Echo(------------------------------------------------------------------------------
Echo( Complete! Goodbye!
Echo(------------------------------------------------------------------------------
Timeout 5 >Nul
It would be a huge help to have this work as intended where we can just type the computer name in and done but I do not know why it does not work when executed remotely and it is copied to the computer. Any help is much appreciated!
HKCU, the target of your reg query, is a per user registry hive. psexec's remote service runs in SYSTEM account and when it issues reg query that wont be directed to the remote machine's currently logged-in user's HKCU. It would be directed to the SYSTEM account's HKCU which maps under HKEY_USERS\S-1-5-18\Software.... Hence the error "The system was unable to find the specified registry key or value."

Compare file creation dates from the command prompt

At my university, we use VMware quite extensively to allow experimentation of admin tasks on otherwise heavily locked down computers. I use the same VM's at home so I don't have to worry about the cluttered environment of my native PC, by copying it to and from my USB flash drive.
The VM I use is snapshot'd with all the settings I like, and set to revert to snapshot on shutdown, so ideally it never changes. As I use multiple computers, I have a script that checks if the VM is present, and if not, copies it over and schedules an auto-open on next login.
My current script looks like this:
#echo off
if exist "C:\Users\[Username]\Desktop\Win7x86\Win7x86.vmx" goto open
if not exist "C:\Users\[Username]\Desktop\Win7x86\Win7x86.vmx" echo VM files not found, preparing to copy from USB
goto usb
:usb
if exist "F:" goto copy
if not exist "F:" echo Please insert USB drive.
pause
goto usb
:copy
robocopy "F:\Virtual Machines\Win7x86" "C:\Users\[Username]\Desktop\Win7x86" /mir /eta
schtasks /create /ru [Username] /rp [Password] /xml "%cd%\Win7x86 Opener.xml" /tn "Win7x86 Opener"
goto open
:open
PATH "%PROGRAMFILES%\VMware\VMware Workstation\"
START vmware.exe -x "C:\Users\[Username]\Desktop\Win7x86\Win7x86.vmx"
My problem is this; sometimes I make change to the VM, and update the snapshot.
I want my script to compare the creation dates of the source files (USB) to the local files (desktop). If the local are newer, then obviously it's been copied since the last update and is fine to run, but if the source files are newer, then the local needs updating.
I plan on using vmware.log as the comparison file as it's small and retains it's creation date (some VM files are deleted and re-created during the VM running process).
I've scoured the internet and the best I found compared dates and listed the newest; but with my very limited batch scripting knowledge, I have no idea how to pipe a robocopy command off that output.
This should do it.
#echo off
for /f "tokens=* usebackq" %%d in (`wmic datafile where Name^="C:\\Users\\[Username]\\Desktop\\Win7x86\\vmware.log" get creationdate ^| findstr ^"[0-9]^"`) do ( set tmpd=%%d )
set vmlocaldate=%tmpd:~0,8%
for /f "tokens=* usebackq" %%d in (`wmic datafile where Name^="F:\\Virtual Machines\\Win7x86\\vmware.log" get creationdate ^| findstr ^"[0-9]^"`) do ( set tmpd=%%d )
set vmusbdate=%tmpd:~0,8%
if %vmusbdate% gtr %vmlocaldate% goto copy
I don't know how this will fit into your script as it will need the USB drive plugged in; at the moment you can launch your and it will straight away open the local virtual machine if it exists, you have to decide if you always need to plug the USB drive in and check the creation date, if you only want to check when if the USB happens to be present, or change your USB prompt to allow you to skip plugging the drive in.
NB. It is significantly easier in both VBScript and PowerShell.

Batch - Loop through folders and run command

I am trying to remove Dropbox from around 500+ Windows 7 computers using a batch script. Dropbox is installed on a per-user basis under "%APPDATA%\Dropbox".
To uninstall silently you have to run "%APPDATA%\Dropbox\bin\DropboxUninstaller.exe /S". I have tested this command and it works fine, but the problem is it only uninstalls it from the user directory you run it from. I have tried taking the DropboxUninstaller.exe file and running it from the root of C:, but it fails.
I will be pushing this script out via SCCM, so it will run under the SYSTEM account. I need to find a way to loop between all of the user direcories, find which profiles have the Dropbox\bin\DropboxUninstaller.exe path/file and run it in that location.
I have tested logging in as another user (local admin) and running the Dropbox uninstaller from another users directory, and it uninstalls fine for the user whos directory I run it in, so I know this will work.
If anyone could help me out with the correct command, that would be great! I have googled around for an answer, but it doesn't seem to be out there.
Thanks in advance,
Search user profiles, and for each if unistaller exists, execute it (asumming an "standard" installation, maybe this needs to be adjusted)
#echo off
setlocal enableextensions
for /F "tokens=2,*" %%a in ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /v ProfileImagePath /s ^| find "REG_EXPAND_SZ" ^| findstr /v /i "\\windows\\ \\system32\\"') do (
call :doUninstall "%%b"
)
endlocal
exit /b
:doUninstall
set "_uninstaller=%~1\AppData\Roaming\Dropbox\bin\DropboxUninstaller.exe"
if not exist "%_uninstaller%" goto :EOF
start "" /wait "%_uninstaller%" /S
goto :EOF
for /r c:\windows %A in (notepad.exe) do "%A" %windir%\win.ini
Remember %%A in a batch and %A at command prompt.

xcopy batch file pst backup

I'm trying to build a batch file script, which will copy the outlook.pst file, from the user's My Documents folder and move it to the server, which I've mapped as a drive, "B:\"
This is the code I have so far:
#echo on
xcopy "c:\Documents and Settings\%username%\My Documents\outlook.pst" "B:\PST\%username%\" -c -d -i -y
exit
The script is designed for Windows XP.
However, when I run this code on the client machine, it doesn't copy the .pst file across, it just runs the command over and over again, until I Ctrl + C it...
Thanks
If I recall correctly, the default location for PST files is in %localappdata%\Microsoft\Outlook\. And if the user has PST files in multiple locations, it's possible that he has more than one with the same name, just in different folders. Good times.
If it's possible that your users might have PST files in locations other than My Documents, I recommend modifying your script with just a couple of very minor changes.
#echo off
setlocal enabledelayedexpansion
for /r "%userprofile%" %%I in (*.pst) do (
rem avoid overwriting in case "outlook.pst" exists in two locations, for instance
if exist "B:\PST\%username%\%%~nxI" (
set cnt=000
rem get count of files matching b:\pst\username\filename*.pst
for /f %%a in ('dir "B:\PST\%username%\%%~nI*%%~xI" 2^>NUL ^| find " File(s) "') do (
set "cnt=!cnt!%%a"
)
rem file of the same name will become filename001.pst, filename002.pst, etc.
set "dest=%%~nI!cnt:~-3!%%~xI"
rem otherwise leave the filename alone
) else set "dest=%%~nxI"
set /P "=Copying !dest!... "<NUL
copy "%%~fI" "B:\PST\%username%\!dest!"
echo Done.
)
I wished to copy outlook.pst from its Microsoft default location to a memory stick using a batch file in Windows XP
I found that xcopy failed to copy Outlook.pst to the memory stick or to My Documents
I now xcopy it in 2 steps, firstly to C:\ then to the Memory Stick
It has worked reliably

Resources