Logon / Logoff Scripts with xcopy not Working - windows

I'm setting up a pair of logon and logoff scripts that will download and upload the users Outlook signatures automatically to and from their home directory (Z:\ drive).I'm using the scripts below, which work fine when run manually, but do not seem to be running at all (or at least not working) when set up as logon and logoff scripts with group policy. I have verified with gpresult that the scripts are indeed applied. 
Logon Script (to download signatures):
#echo off
set LOGFILE=Z:\batch.log
call :LOG >> %LOGFILE%
exit /B
:LOG
if exist "Z:\Signatures\" (
xcopy /e /Y /D Z:\Signatures %appdata%\Microsoft\Signatures
)
Logoff Script (to upload signatures):
#echo off
set LOGFILE=Z:\batch.log
call :LOG >> %LOGFILE%
exit /B
:LOG
if not exist "Z:\Signatures\" (
mkdir Z:\Signatures
attrib +h Z:\Signatures /s /d
)
xcopy /e /Y /D %appdata%\Microsoft\Signatures Z:\Signatures
Does anyone have an idea why this wouldn't be working?

Fixed by using %homeshare% instead of the mapped Z:\ drive

Related

Copy User Subfolders Using Robocopy

I am trying to copy the Normal.dotm from all user's C:\Users\%username%\AppData\Roaming\Microsoft\Templates to C:\Temp\%username%. I'm specifically trying to backup the data before a reinstall as part of an SCCM Task Sequence. Any thoughts?
I have tried a few different scripts using robocopy and it either gets stuck in a loop or only copies one directory.
robocopy C:\Users\%username%\AppData\Roaming\Microsoft\Templates\. C:\Temp\%username% /s /create
Only copies directory cmd is run as:
robocopy C:\Users\ C:\Temp\ /s /xjd normal.dotm
Creates loop and creates C:\Users\Application\Data\Application Data\ forever
The account running this will need to be an Administrator in order to access everyone's directories. When you believe the correct commands are being created, remove the lower case echo from them.
#ECHO OFF
FOR /F "delims=" %%f IN ('DIR /S /B "C:\Users\Normal.dotm"') DO (
SET "TDIR=C:\temp%%~pf"
IF NOT EXIST "%TDIR%" (echo MKDIR "%TDIR%")
echo COPY "%%~f" "%TDIR%"
)
You might use a different way:
#echo off
setlocal EnableDelayedExpansion
for /R "C:\Users\" %%A IN (Normal.dotm) do (
set "fpath=%%~fA"
if not "!fpath:\AppData\Roaming\Microsoft\Templates\=!" == "!fpath!" (
rem Find username:
for /F "tokens=3 delims=\" %%B IN ("%%A") do (
set "current_username=%%B"
)
rem Copy files:
copy "!fpath!" "C:\Temp\!current_username!\"
)
)
That, of course, requires administrator privileges, you cannot enter other users directories without admin privileges. Right-click on your file and select 'Run As Administrator'.

Check if files exist in cmd

How do I check if a bunch of files exist in my script?
I want my script to perform some add the files if its not present or just ignore and proceed with the rest of the code.
This is what I have writtes:
if not exist qapi/qapi*.h (
#ECHO OFF
CD %~dp0\..\..
ECHO %CD%
SET TOP_IOE_SW= %CD%
ECHO %TOP_IOE_SW%
SET BUILD_DIR=%TOP_IOE_SW%/build/ms
icacls * /q /c /t /grant Users:F
mkdir include\qapi
xcopy /Y qapi_export\qapi*.h include\qapi
xcopy /Y qapi\common\qapi*.h include\qapi
xcopy /Y core\api\qapi*.h include\qapi
xcopy /Y qapi\build include\build\
)
else (
call :next)
:next
////the rest of the code comes here///
When I run the script, it keeps prompting me to overwrite these files.
How can I have this done without the prompt?
Thanks

Different results running batch script as administrator than running it form elevated command prompt

I have a script to change registry values.
Running it from an elevated command window it changes the key ok.
Running it directly as an administrator (right click) it does nothing, same thing running it from task scheduler with elevated privileges.
Any idea why?
Code, basically it checks IP number and according to that it changes autoconfig proxy script with several option of .reg files with the different internet connection setup key.
#ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
SET Target=%computername%
IF %Target%.==. ECHO Missing target!&PAUSE&GOTO :EOF
FOR /F "tokens=2 delims=[]" %%A in ('PING %Target% -4 -n 1 ^| FIND "["') DO (
SET TargetIP=%%A
SET TargetIP
)
IF NOT DEFINED TargetIP ECHO NO IP found!&PAUSE&GOTO :EOF
:Compare
SET PartialIP=%TargetIP:~0,9%
IF "%PartialIP%"=="172.20.25" (
REGEDIT /S conbae.reg
goto :eof)
IF "%PartialIP%"=="172.20.22" (
REGEDIT /S conbna.reg
goto :eof)
SET PartialIP=%TargetIP:~0,6%
SET PartialIP
IF "%PartialIP%"=="172.20" (
REGEDIT /S conweb.reg
) ELSE (
REGEDIT /S sinbae.reg
)
Right clicking to run as Admin changes the working directory to the system32 folder, and the conbae.reg file can't be found as it is in the current folder.
You can change conbae.reg to "%~dp0conbae.reg" in all reg files and places and it should work fine too.

Bat file delete it's own folder

I have a folder test which has some other files and folders. I also have a bat file delete.bat
test
test\delete.bat
The bat file delete the contents of test using the next command:
rmdir C:\test /s /q
I also want to delete the folder test. If i copy the delet.bat file and paste it in another directory the test folder will be deleted.
However, if i run the delete.bat file inside the test folder, all the contents of the test folder(included the delete.bat file) are deleted, but the test folder not.
I consider that this is because the test folder is opened.
Any suggestion? Is there any command that i can add at the beginning if delete.bat in order to close the folder first and then run the command rmdir C:\test /s /q?
The following delete.bat works fine is you run it using win+R in Windows.
cd c:\
rmdir C:\test /s /q
Ok,
Sasha_gud helps a lot.
Now i find a way to add code in bat file in order to run as administrator (so the run as administrator will be run automatically).
For more information look at
How can I auto-elevate my batch file, so that it requests from UAC administrator rights if required?
Here is the code:
:::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights
:::::::::::::::::::::::::::::::::::::::::
#echo off
CLS
ECHO.
ECHO =============================
ECHO Running Admin shell
ECHO =============================
:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )
:getPrivileges
if '%1'=='ELEV' (shift & goto gotPrivileges)
ECHO.
ECHO **************************************
ECHO Invoking UAC for Privilege Escalation
ECHO **************************************
setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs"
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs"
"%temp%\OEgetPrivileges.vbs"
exit /B
:gotPrivileges
::::::::::::::::::::::::::::
:START
::::::::::::::::::::::::::::
setlocal & pushd .
REM Run shell as admin (example) - put here code as you like
cd c:\
rmdir C:\Users\haris\Desktop\test /s /q
Only the 2 last lines are my code.

Copying User Profile to Default Profile in Windows 7

I'm trying to find a way using, hopefully, a batch file to copy a modified user profile to the default user profile so that any user that logs in to the computer will adopt the profile setup. We're using a utility called pGina to call the default profile for a user logged in through a RADIUS server, as opposed to having the user log in to the domain directly. Currently, we're trying to use robocopy for this.
cd C:\Users\
rmdir Default /s /q
robocopy /COPY:DAT /R:5 /E C:\Users\user1 C:\Users\Default
pause
Does anyone have any idea how to successfully copy the profile? Thanks in advance.
This is what I found when searching for an answer to your problem
rem delete old Default User profile
RD /s /q "%systemdrive%\Profiles\Default User"
rem copy current user profile to default user profile
xcopy "%USERPROFILE%\*.*" "%systemdrive%\Profiles\Default User" /e /c /I /H /R /K /Y
rem delete non-need some files
del /f /q "%systemdrive%\Profiles\Default User\*.*"
rem set default attributes
attrib +h "%systemdrive%\Profiles\Default User"
rem registry trick
rem no directly copy locked ntuser.dat file
rem use reg tools to save current user registry to file
reg save HKCU "%systemdrive%\Profiles\Default User\ntuser.dat"
rem set default attributes to hive file
attrib +H +S "%systemdrive%\Profiles\Default User\ntuser.dat"

Resources