Check remote file existence on multiple Windows servers - batch-file

I need to check a file status (existing? or last modified date) on multiple remote Windows servers (in LAN). The files are accessible via UNC path. The remote PCs need a user name and password. It's best to be some sort of script.
I am not system admin, but asked to do this by my boss because each of these PCs are host of SQL Server and the file is a backup of database. I am a SQL Server database developer. I was trying to do it using T-SQL, but just found it not geared to do this (although maybe doable).

Create a batch file like (say check.bat):
#echo off
set list=server1 server2 server3 server4 serverN
for %%s in (%list%) do (
echo Logging in to %%s
net use \\%%s\shared /user:mydomain\myuser password
echo Checking file existence on %%s
if exist \\%%s\shared\file.txt (
echo File exist on %%s
) else (
echo File does NOT exist on %%s
)
echo Logging out
net use \\%%s\shared /delete
)
For details on the net use commands, see the accepted answer for question Mapping a network drive without hardcoding a drive letter in a batch file.

Related

Run DB2 command from a windows batch script

I am trying to run a DB2 command (Import) from a windows batch script.
My approach is..
Invoke DB2 command prompt: DB2CMD.exe DB2SETCP.BAT DB2.EXE
Connecting to the database: connect to DBNAME USER USERNAME USING PWD
Executing DB2 command: IMPORT FROM ...
quit
The batch script looks like this..
DB2CMD.exe DB2.EXE "connect to dbname USER username USING pwd" "IMPORT FROM D:\File.txt .... INSERT_UPDATE INTO tablename"
quit
It is working till the second step and the bat script is exiting without running the third step.
Can anyone guide me on this.. Thanks!
To run Db2 commands in a CMD/BAT file on Microsoft Windows, you need to have a Db2 client installed, and you can begin all of the BAT or CMD files
with the pattern below. This lets your script open the DB2CMD.EXE if that is not already opened (so you don't need to do it manually). You also need to have the database catalogued so that you can access the database from the Windows CMD.EXE or DB2CMD.EXE command line. Always verify your commands at the command-line before putting them into a script.
When you get any error, you must specify the exact error when asking for help, either by using copy/paste from the db2cmd window or by attaching a screenshot.
Your second step cannot just exit without first showing some error message, although you may need to ensure that the window does not close before you can see that message. There are many reasons for IMPORT to fail, but you have given no information about which reason Db2 gave. If you edit your question to specify the missing error detail, you will get more help on here.
If you run the script on the same hostname where the database is running then you don't need to specify a userid/password and then it connects as the currently logged on User.
If you need to connect to the database with a specific userid (different from the user logged in to Windows) or if you need to connect to a remote database then you must specify a password to the connect command. You can ask the user to enter a password, for interactive scripts. If you need to have an unattended script you should avoid hardcoding the password in plain text in the script, or use runas.
The example below shows both a connect to a local database, and a remote database (choose only one of the methods).
#if ""%DB2CLP%""=="""" db2cmd /c /i /w ""%0"" %* && goto :EOF
#rem for connecting to a local database as current logged-in Windows account (no password required)
db2 -v connect to dbname
#if errorlevel 1 ( #echo ""FAILED to connect"" && #goto :EOF )
#rem for connecting to a remote database, or connecting with a different account
db2 -v connect to dbname user YOURUSER using THEPASSWORD
#if errorlevel 1 ( #echo ""FAILED to connect"" && #pause ... && #goto :EOF )
db2 -v "IMPORT FROM TO ... "
#if errorlevel 2 ( #echo "FAILED to export..." && #pause ... && #goto :EOF )
db2 -v connect reset

View Local Administrator accounts on a remote computer using Batch

Is there a way to remotely view local administrator accounts on remote computers within a network or possibly run NET LOCALGROUP ADMINISTRATORS and have the result be viewed on the command prompt instead of extracting it in a notepad (via psexec)?
Thanks in advance!
Try a Bat file with this.
for /f %%A in (computers.txt) do (echo Identifying group members on "%%A" ...
psexec -s \%%A net localgroup administrators )
Create a txt file with computer names without spaces one name for each line.
Redirect the command to a txt file and creat a log with it.
Ex. batfile > log.txt

Batch file runs on local desktop but not on file server

I am new to .bat files but have been working on a process for two days. It is very simple and works fine on my desktop (thanks for a code snippet found on the internet), but when I try to run it through a shared drive on our file server, the processing does not occur.
#echo off
REM Batch file to process daily flat file and replace " with empty space for upload into Access/SQL Server
ren C:\Users\Desktop\filejuly17.txt filequotes.txt
for /f "tokens=* delims= " %%a in (C:\Users\Desktop\filequotes.txt) do (
set S=%%a
set S=!S:"=!
>> C:\Users\Desktop\finalfile.txt echo.!S!
)
I have had to switch the ren to a copy on the file server, but have verified that I can copy the one file to another using that process. So am assured that I have the path correctly. However, no matter which options I have tried, I am unable to have this batch file strip out the double quotes that are randomly in the file from user entry. We need to strip these out and replace them with empty spaces in order to have a data migration process then load this flat file into our database.
Thank you for any guidance.
If it helps, my laptop is running on Windows 7, the server I remote connect is running on Windows Server 2008.

Check existance of file on ftp

I want to check existence of file on ftp server using batch script.
Right now I have this code which saves it to ftp server:
#echo off
echo user prayagimages> d:\Images\ftpcmd1.dat
echo Pass123#>> d:\Images\ftpcmd1.dat
echo put d:\Images\%~1>> d:\Images\ftpcmd1.dat
echo quit>> d:\Images\ftpcmd1.dat
ftp -n -s:d:\Images\ftpcmd1.dat 118.139.173.227
del d:\Images\ftpcmd1.dat
Lets say if I post image.jpeg to server.
How do I check whether it is on the server or not?
Thanks.
This questions seem to request similar things:
To search for a file at FTP
Windows XP Batch- IF EXIST FTP with Date Variable
Mainly the first one, but the second link has some useful info too.

Automating the creation of a SQL Server Alias on a Client's desktop

Does anyone know how would I go about automating the creation of an alias for a client that uses SQL Server (something you would normally use Sql Server Client Network Utility to do manually)?
Many thanks
The aliases are stored in the registry, so a registry file would do the trick. Some options
deploy a .reg file for the user to double-click
use remote registry to create the entry
in your setup utility, include the register entry
etc
Here's a base BAT script that I use as a starting point:
set DBSERVERALIAS=AliasName
set DBSERVER=RealServerName
rem %windir%\system32\cliconfg.exe
reg add HKLM\Software\Microsoft\MSSQLServer\Client\ConnectTo /v %DBSERVERALIAS% /t REG_SZ /d "DBMSSOCN,%DBSERVER%" /f
reg query HKLM\Software\Microsoft\MSSQLServer\Client\ConnectTo
rem 64-bit support for database alias
rem %windir%\SysWOW64\cliconfig.exe
if /i NOT "%PROCESSOR_ARCHITECTURE%" == "X86" (
reg add HKLM\Software\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo /v %DBSERVERALIAS% /t REG_SZ /d "DBMSSOCN,%DBSERVER%" /f
reg query HKLM\Software\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo
)
Can you not use DNS to avoid an alias on each client machine? So if anything changes it's one DNS CNAME change rather then updating every client?
An example here: One SQL Server Instance, Two Server Names

Resources