I've been working to clean up a messy Active Directory as well as a network file system in the same state and I understand the concept of mapping users network drives and currently use a combination of batch and vbs files to do so. However, I need to start fresh and was wondering if there was any way to detect and delete the users shortcuts on their desktop associated with the previous network drives. (Yes - I understand how to delete all of the network drives, but: How do I detect and delete the shortcuts on the desktop associated with them?)
I've already written and custom tailored my own scripts to map drives and place shortcuts. I just need to get rid of any old shortcuts. I can't afford to delete all of the .ink files on the desktop, either. Only any associated with preexisting network drives.
I'm working in an XP / Server 2003 client/server environment.
Another question: If a script runs every time a user logs on through the domain and adds the same network shares over and over again without first deleting them, (even though it would be the same script every time) would it / does it - do any harm? <- I didn't research this one a whole lot yet because i've been crawling through Google and took a peak see through Stack to try and find a solution for the first question/issue.
Any help / advice would be greatly appreciated. Thanks!
Let's say there was before in logon batch
%SystemRoot%\System32\net.exe use Z: \\OldServer\OldShare
and the users created shortcuts to files / directories on this share on their desktop or in a subdirectory of the desktop.
A shortcut file contains always both – the path to the file with drive letter as well as the UNC path with server and share names.
A simple batch file to find in the desktop directory and all its subdirectories *.lnk files containing \\OldServer\OldShare and delete all found shortcut files is:
#echo off
for /F "delims=" %%I in ('%SystemRoot%\System32\findstr.exe /I /M /S "\\\\OldServer\\OldShare" "%USERPROFILE%\Desktop\*.lnk" 2^>nul') do (
echo Deleting shortcut file "%%I"
del "%%I"
)
For details about /I /M /S run in a command prompt window findstr /?
As it can be seen each backslash in the find string must be escaped with one more backslash.
It is also possible to search for "Z:\\" instead of "\\\\OldServer\\OldShare"
But be careful with deletion of a *.lnk file just based on drive letter because users could have mapped a different share to this drive letter. Those users would not be happy if their shortcuts are deleted, too.
The batch file could ask the user for confirmation before deleting every found shortcut containing the drive letter of not anymore existing drive:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
cls
echo Searching for shortcuts to drive Z: ...
for /F "delims=" %%I in ('%SystemRoot%\System32\findstr.exe /I /M /S "Z:\\" "%USERPROFILE%\Desktop\*.lnk" 2^>nul') do (
echo/
echo Shortcut "%%~nI" might be not valid anymore.
echo/
setlocal EnableDelayedExpansion
set Confirm=
set /P "Confirm=Delete the shortcut (y/n)? "
if /I "!Confirm!" == "y" (endlocal & del /F "%%I") else endlocal
)
endlocal
It is no problem if a network drive mapping using a command like
%SystemRoot%\System32\net.exe use Z: \\MyServer\MyShare
is done in logon batch file on every logon. An error message is output by net use if drive letter Z: is used already, for example if the network drive mapping was done persistent and the user started the computer first without a network connection, then plugged-in the network cable and next after a few seconds entered user name and password to logon to Windows and on domain server of the company.
It is possible to use
%SystemRoot%\System32\net.exe use Z: /delete 2>nul
%SystemRoot%\System32\net.exe use Z: \\MyServer\MyShare
to first delete an already existing network drive mapping before mapping the share to drive letter Z:. I do not recommend to use wildcard * instead of Z: as that would delete also all network drive mappings created by the user.
For computers not only used in the company network, it is often better to make the drive mapping not persistent by using
%SystemRoot%\System32\net.exe use /persistent:no
%SystemRoot%\System32\net.exe use Z: \\MyServer\MyShare
%SystemRoot%\System32\net.exe use /persistent:yes
Windows does not save in this case in Windows registry under key HKEY_CURRENT_USER\Network that \\MyServer\MyShare should be mapped to Z: and therefore the network drive mapping exists only for current user session. The network drive mapping is removed automatically once Windows is restarted or the user logs off.
Related
I am trying to create an active setup to copy files to all users profile after application launch.
Using this command, I am unable to copy file(s) to user profile
The problem is that although no one uses profiles, the "Default" Profile have a random prefix name (24xwe234.default \ 45qw324w.default).
xcopy "C:\Temp\123.cfg" C:\users\%username%\AppData\Roaming\Mozilla\Firefox\Profiles\*.default" /d /y
How can I copy the file to the ****.default/ Folder via batch script?
If you have Administrative privileges (in order to read/write other users %HOMEPATH%), then everything below could be applied to other users, else it's limited to your own user only.
Although xcopy is a pretty advanced command, I don't think it can handle wildcards at destination in this scenario (also didn't you miss an "*" after ".default"?).
One alternative would be to use a .bat script that iterates (using [SS64]: FOR /F (or [MS.Docs]: for)) over the .default* like dirs (corresponding to Firefox profiles - not an expert on this area, could there be more than 1?) and copies the file (using good old [MS.Docs]: copy) in each of them (if any).
code.bat:
#echo off
setlocal enableextensions
set _SOURCE_FILE="C:\Temp\123.cfg"
set _TARGET_USER=%USERNAME%
set _FIREFOX_PROFILES_PATH="C:\Users\%_TARGET_USER%\AppData\Roaming\Mozilla\Firefox\Profiles"
for /f %%f in ('dir /b "%_FIREFOX_PROFILES_PATH:"=%\*.default*"') do (
echo Copying %_SOURCE_FILE% to "%_FIREFOX_PROFILES_PATH:"=%\%%f"
copy /y %_SOURCE_FILE% "%_FIREFOX_PROFILES_PATH:"=%\%%f"
)
As it is now, the script copies the file in the default Firefox dir(s) for current user only.
Again, with Administrative privileges, it can be extended to iterate all existing users, and do the same thing for each of them (well, it could be extended anyway, but it will still do only what's doing now, when run by a regular user).
But anyway, the question (or, at least the step that posed problems) is about the current user's .default profile.
I want to copy specific file from one machine to 5 other machines, so I have server list file contains the ip addresses of each machine like:
10.10.1.3
10.10.1.4
10.10.1.5
10.10.1.6
10.10.1.7
in my batch file:
SET File=C:\Files\servers.txt
SET User=user
SET Password=pass
IF EXIST b:\ (
NET USE b: /DELETE /Y
)
FOR /F %%A IN (%File%) DO (
START /WAIT NET USE b: \\%%A\C$\Temp /user:%User% %Password%
COPY C:\Logs\L1.log b:\L1.log /Y
IF EXIST b:\ (
NET USE b: /DELETE /Y
)
)
the problem is in the first server I get error message The system cannot find the drive specified but for the others servers everything works great.
I think it's something with the NET USE of course maybe the map network is deleted before finish copy?
Is there any way in a batch file to loop some servers and for each one of the servers open map network copy files wait till copy is completed and move on to next server?
EDIT:
I have an update for this problem:
the source machine and the target machine are both in different domains.
I have a user define as admin in both of the machines.
The machines knows each other (I can open the target folder in the source machine like \server\C$\temp and I can paste anything I want there)
I tried to copy files without using net use and just copy from C:\file.log \server\c$\temp\file.log for each server (I have 5) and for 3 servers it worked and the other two I had an error: Logon failure: unknown user name or bad password
FOR /F %%A IN (%File%) DO (
COPY C:\temp\file.log \\server\c$\temp\file.log /Y
)
What can be the problem?
Please help?
Thank you in advanced.
The best way would be if the user/password is the same on every machine (credientals shared throughout the domain)
In that case, no need for NET USE, just copy to UNC path directly
FOR /F %%A IN (C:\Files\servers.txt) DO COPY /Y C:\Logs\L1.log \\%%A\C$\Temp\L1.log
I understood you have a problem on 2 machines out of 5. You cannot access C$ directly if you are not administrator of the machines.
Check:
domain (echo %USERDOMAIN% on the remote machine): All machines must be on the same domain for it to work
whether you are administrator or not on the remote machine: you cannot mount drives with the C$ trick if you are not administrator.
There is an alternate solution for you if this doesn't work out:
Share C:\TEMP (read/write) as the same name ex: sharedtemp on the 5 machines (or ask an admin to do that)
Then adapt the script like this:
FOR /F %%A IN (C:\Files\servers.txt) DO COPY /Y C:\Logs\L1.log \\%%A\sharedtemp\L1.log
Even if you are not an administrator, you'll be able to access the share that way.
When using START command, you need to either specify new window name, or leave it blank in double quotes, if you want it to run in the same open CMD window. However, you don't need to use START in this particular case. Try adding timeout for the 1st or all server connections in the list:
IF EXIST b:\null NET USE b: /DELETE
:: more code here
NET USE b: \\%%A\C$\Temp /user:%User% %Password%
if "%%A"=="10.10.1.3" timeout 5 >nul
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.
I want to delete couple of folders using batch file.
But problem is that these folders are present on some other machine.
But I can take the remote access to this machine on my machine.
Please help me with this issue. I don't have any idea about this.
A less comprehensive, but more simple answer...
del /F \\myserver\w_root$\users\selah\selah.txt
del /F \\myserver\w_root$\users\selah\status.txt
As long as you can mount the remote folder with a user who has delete authority, you can simply do something like this:
rem Set up the remote path - assuming it is sharable
set MY_DIR=\\10.1.1.1\some\path
rem Mount the remote path
net use %MY_DIR% %MY_PASSWORD% /USER:%MY_USER%
rem Delete a file
if exist "%MY_DIR%\MyFile" del /F "%MY_DIR%\MyFile" >nul
rem Unmount the remote path
net use %MY_DIR% /delete >nul
If you want to remove folders, you'll just need to make sure the mount point is at least one level above where you'll be deleting.
I'm wanting to create a batch file that will back up my computer. This is what I have right now:
#echo off
:: variables
net use U:\\...more stuff...
set drive=U:\
set backupcmd=xcopy /s /c /d /e /i /r /y
echo ### Backing up My Documents...
%backupcmd% "%USERPROFILE%\My Documents" "%drive%\My Backup\My Documents"
echo Backup Complete!
#pause
It's simple enough. It works fine for a drive that is physically attached to my computer. However, I have a networked drive that I'd like to use for this task. I've tried a few things I found on forums, but to no avail. In the above code, I started attempting to use "net use." When I run it, I get "invalid drive specification" or "network path not found."
How can I get this to work?
Any help is appreciated.
You have:
net use U:\\...more stuff...
but there needs to be a space between the drive specification and the network share:
net use U: \\...more stuff...