I m using psexec to auto run
get.bat
tasklist | findstr pmill.exe >> dc-01\c$\0001.txt
run_get.bat
psexec #%1 -u administrator -p password -c "C:\get.bat"
pclist.txt
on all PCs on our network,
how can i get the result with PC name instead of only pmill.exe in the text file?
is there anyway i can do from powershell?
I need to get the pc name in result.
Hint plz!
Instead of psexec, try this:
#echo off
setlocal enabledelayedexpansion
for /f %%I in (pclist.txt) do (
set /p q="Checking %%I... "<NUL
ping -n 1 -w 500 %%I>NUL 2>NUL
if !errorlevel!==1 (
echo Offline.
) else (
wmic /node:%%I /user:adminuser /password:pass process where name="pmill.exe" get csname 2>NUL | find /i "%%I" >>dc-01\c$\0001.txt
echo Done.
)
)
That'll output %computername% if pmill.exe is running, or nothing otherwise.
Edit:
If you must use psexec then I suggest changing the logic of your for loop that calls psexec, something like this:
#echo off
setlocal enabledelayedexpansion
for /f %%I in (pclist.txt) do (
set pc=%%I
set /p q="Checking %%I... "<NUL
ping -n 1 -w 500 %%I>NUL 2>NUL
if !errorlevel!==1 (
echo Offline.
) else (
for /f %%z in ('psexec \\!pc! -u adminuser -p pass tasklist 2^>^&1 ^| findstr /i "pmill.exe"') do (
set /p q="pmill.exe found. "<NUL
echo !pc!>>dc-01\c$\0001.txt
)
echo Done.
)
)
Related
I use a .bat file to create folders listed in two .txt files and also for deleting empty folders. All works fine but now I need the same to work on sharepoint.
Can anyone describe how or if this can be done? I have read that maybe I need to find put about powershell?
The .bat file looks like this:
echo off
%CHCP 1252
mkdir C:\Temp\MStruktur
Copy "P:\folderstructure\Niv1.txt" "C:\Temp\MStruktur"
Copy "P:\folderstructure\Niv2.txt" "C:\Temp\MStruktur"
cls
REM -----checking for empty characters
for %%a in (.) do set currentfolder=%%~na
if not "%currentfolder%"=="%currentfolder: =%" GOTO error
:home
echo "Folder structure menu:"
echo -------------------------------------
echo - 1 - create level 1
echo - 2 - create level 2
echo.
echo - S - Delete empty folders
echo.
echo - X - Exit
echo.
set /p web=Valg:
if "%web%"=="1" goto niv1
if "%web%"=="2" goto niv2
if "%web%"=="s" goto slet
if "%web%"=="x" goto slut
goto home
:niv1
cls
for /f %%i in (C:\Temp\MStruktur\niv1.txt) do mkdir %~dp0\%%i
echo.
echo Level 1 created
echo.
pause
exit
:niv2
cls
for /f %%i in (C:\Temp\MStruktur\niv2.txt) do mkdir %~dp0\%%i
echo.
echo Level 2 created
echo.
pause
exit
:slet
cls
for /f "delims=" %%d in ('dir %~dp0 /s /b /ad ^| sort /r') do rd "%%d"
echo.
echo Deleted empty folders
echo.
pause
exit
:error
cls
echo.
echo No blanks in folder name
echo.
pause
exit
:slut
RD /S /Q C:\Temp\MStruktur
exit
I am trying to create a batch file to run start some micro services and database
1 FOR /F "tokens=4 delims= " %%P IN ('netstat -a -n -o ^| findstr :1000') DO #ECHO TaskKill.exe /PID %%P
2 FOR /F "tokens=4 delims= " %%P IN ('netstat -a -n -o ^| findstr :1001') DO #ECHO TaskKill.exe /PID %%P
3 FOR /F "tokens=4 delims= " %%P IN ('netstat -a -n -o ^| findstr :5432') DO #ECHO TaskKill.exe /PID %%P
4 start cd "C:\Program Files\PostgreSQL\10\bin\" & pg_ctl.exe -D "c:\Program Files\PostgreSQL\10\data" start
#REM to start service
5 start javaw -jar -Dserver.port=1000 text-annotation-tool-1.0-SNAPSHOT.jar
Line 1 to 3 and Line 5 execute correctly when executed by commenting line 4.
Line 4 is to start a Postgres server in a new prompt (beacuse of the Dir change). I think the problem is with the way I have used quotes. The 'start' at the beginning and ending of line 4 serve different purpose.
Also, if I execute line 4 in different prompt, How do I close the prompt after execution (nohup equivalent)
There are two errors: You can't "pass" cd to the start command. And start has the quirk to interpret the first quoted parameter as the new window's title. So start "C:\Program Files\PostgreSQL\10\bin\" ... wouldn't work, you need to supply a dummy window title. The & also seems wrong
So you need:
start "Postgres Server" "C:\Program Files\PostgreSQL\10\bin\pg_ctl.exe" -D "c:\Program Files\PostgreSQL\10\data" start
As the full path to pg_ctl.exe is supplied, there is no need for a cd.
But if you want to define the default directory for the new process, you have to use the /D parameter:
start "Postgres Server" /D "C:\Program Files\PostgreSQL\10\bin" pg_ctl.exe -D "c:\Program Files\PostgreSQL\10\data" start
Unrelated, but: putting the Postgres data directory into c:\Program Files\ is a very bad idea. That directory has special permissions for a purpose. You should use %ProgramData% or %AppData% instead
i'm trying to read files and load them all csv files, and once done move the files to some other location :
cd E:\data\
for /f %%a in (’dir /b filename*.CSV) do (
psql -U postgre -W password -c "COPY INTO LA from %%a" zipcodes
mv %%a E:\data\bc\
)
but it gives me following error:
E:\data>for /F %a in (ΓÇÖdir /b filename*.CSV) do (
psql -U postgre -W password-c "COPY INTO LA from %a" zipcodes
mv %a E:\data\bc\
)
The system cannot find the file ΓÇÖdir.
thanks for help
#echo off
setlocal enableextensions disabledelayedexpansion
set "psql=c:\wherever\psqlIs\psql.exe"
pushd "e:\data" && (
for %%a in ("filename*.CSV") do (
"%psql%" -U postgre -W password -c "COPY INTO LA from %%~a" zipcodes
move "%%~a" "E:\data\bc\"
)
popd
)
for /f is intended for file/string processing. To iterate over a set of files use a simple for
Anyway, the problem in your code are the opening single quote (as Alex K has pointed), the missing closing quote and the non existing mv command in windows that should be move
edited it seems there are problems with file loading. psql copy command indicates it is better to use full path names (with backslashes doubled), so
#echo off
setlocal enableextensions enabledelayedexpansion
set "psql=c:\wherever\psqlIs\psql.exe"
pushd "e:\data" && (
for %%a in ("filename*.CSV") do (
set "file=%%~fa"
"%psql%" -U postgre -W password -c "COPY INTO LA from E'!file:\=\\!'" zipcodes
move "%%~a" "E:\data\bc\"
)
popd
)
Here is my pseudo:
Read from LIST
Ping to check if machine is awake | Report result
Check to see if SERVICE is running | Report result
NO? Is it stopped?
YES? Run it | Report result
NO? Must not be installed, remote install please! | Report result
This is driving me bonkers here... I can make the parts of this code work alone just fine, but when I combine it into a nested IF structure, it returns 'Network Error' and 'Running' if a machine does not have the service running...then a manual check at that machine shows it is still actually stopped. Same for machines without the service installed at all.Please tell me where I'm messing up.. Thanks!
#echo off
echo.
echo.
cls
set SERVICE=MyServ
for /f %%i in (\\127.0.0.1\c$\list.txt) do call :DOIT %%i
:DOIT
echo Checking %SERVICE% on %1
#ECHO off
ping -n 2 -w 1000 %1 >trial.txt
find "Reply from" trial.txt>nul
if %ERRORLEVEL% == 1 (echo %1 Network Unresponsive>> "\\127.0.0.1\c$\list_report.txt")
if %ERRORLEVEL% == 0 (
sc \\%1 query %SERVICE% | FIND "STATE" | FIND "RUNNING"
if %ERRORLEVEL% == 0 (echo %1 Running>> "\\127.0.0.1\c$\list_report.txt")
if not %ERRORLEVEL% == 0 (
sc \\%1 query %SERVICE% | FIND "STATE" | FIND "STOPPED"
if %ERRORLEVEL% == 0 (
sc \\%1 start %SERVICE%
echo %1 forced start>> "\\127.0.0.1\c$\list_report.txt""
)
if not %ERRORLEVEL% == 0 (
xcopy /f /y "\\127.0.0.1\!\theAPP.exe" "\\%1\c$\temp\"
\\127.0.0.1\!\psexec \\%1 -s -n 10 -d cmd /c "c:\temp\theAPP.exe"
echo %1 Installed>> "\\127.0.0.1\c$\list_report.txt""
)
)
)
this should fix the issue
for /f %%i in (\\127.0.0.1\c$\list.txt) do call :DOIT %%i
:DOIT
if not "%1"=="" (
echo Checking %SERVICE% on %1
echo.
ping -n 2 -w 1000 %1 | find "Reply from" >nul
if ERRORLEVEL 1 echo %1 Network Unresponsive>> \\127.0.0.1\c$\list_report.txt else (
sc \\%1 query %SERVICE% | find "RUNNING" > nul
if ERRORLEVEL 1 (
sc \\%1 start %SERVICE% > nul
echo %1 Forced Start>> \\127.0.0.1\c$\list_report.txt
) else (
echo %1 Running>> \\127.0.0.1\c$\list_report.txt)
xcopy /f /y \\127.0.0.1\c$\text.txt \\%1\c$\temp\ > nul
\\127.0.0.1\c$\psexec \\%1 -s -n 10 -d cmd /c "type c:\temp\text.txt"
echo %1 Installed>> "\\127.0.0.1\c$\list_report.txt")
)
)
part of the issue is that you have to test for if not %1="" at the top of the script. that is where you get the erroneous text..
then you had some nesting issues, so i used ELSE instead.
you will have to change the text.txt to your filename.exe (and omit the "type" command)
please check answer if this solves your problem
fyi, you will need to have administrator access on the remote machine so that you can turn on and off services
Hi want to exclude 2 or more files from my batch file.
My batch file executes all SQL files in that folder.
Here is the code:
ECHO %USERNAME% started the batch process at %TIME% >output.txt
FOR %%G IN (*.sql) DO (
//IF would go here to skip unwanted files
sqlcmd.exe -S RMSDEV7 -E -d ChaseDataMedia7 -i "%%G" >>output.txt
)
pause
So, how would i add and if statemant to skip the files in the loop that i don't want to execute?
You can chain IFs;
FOR %%G IN (*.sql) DO (
if not %%G==foo.sql if not %%G==bar.sql (
some command "%%G"
)
)
#echo off
setlocal EnableDelayedExpansion
set unwantedFiles=foo bar baz
for %%g in (*.sql) do (
set "test=!unwantedFiles:%%~Ng=!"
if "!test!" == "!unwantedFiles!" (
echo %%~Ng.sql is not unwanted, process it:
echo Process with %%g
)
)
Take a name and copy unwantedFiles by removing current name. If the result is the same as before, this name is NOT in unwantedFiles, so process it...