batch file doesnt work on Windows 7 - windows-7

I am trying to execute a simple batch file with following contents..
ECHO OFF
::CMD will no longer show us what command it’s executing(cleaner)
ECHO As a network admin, I’m getting tired of having to type these commands in!
:: Print some text
IPCONFIG /ALL
:: Outputs tons of network information into the command prompt
PAUSE
:: Lets the user read the important network information
PING www.google.com
:: Ping google to figure out if we’ve got internet!
ECHO All done pinging Google.
::Print some text
PAUSE
But, nothing happens except a flash of command prompt. PAUSE does not seem to have any effect. Please help.

I believe you need to have administrator privileges to run ipconfig.

Related

Telling my backup script in windows to "mount everything that's supposed to be mounted."

I have a backup script that works. I won't put in the entire script, because I don't think it's necessary, but it looks like this:
set backup_dir=R:\__MYBACKUPS
REM *** <<< Right here, I want to have a command that says "Mount everything you know how to mount"
if not exist %backup_dir% (
echo The backup directory does not exist.
echo Making the backup directory %backup_dir%
)
call :get_datetime_stamp
set BACKUPNAME=%backup_dir%\bak%DATEANDTIME%.zip
set default_to_backup="c:\Users\%username%\documents\*.doc*" "c:\Users\%username%\documents\*.csv" "c:\Users\%username%\documents\*.xls*" "c:\Users\%username%\documents\*.pdf" ^
"c:\Users\%username%\documents\*.ppt*" "c:\Users\%username%\documents\*.txt"
set what_to_backup=%default_to_backup% "c:\Users\%username%\etc"
set zip_program="c:\Program Files\winzip\winzip64.exe"
set zip_parameters=-min -a -r
echo EXECUTING THE FOLLOWING BACKUP COMMAND:
echo %zip_program% %zip_parameters% %BACKUPNAME% %what_to_backup%
%zip_program% %zip_parameters% %BACKUPNAME% %what_to_backup%
It works fine, if I mount the R drive. The way I normally do this is when I login I manually go to the explorer window and just click on the R drive ... and that action magically "connects" the R shared network drive. HOWEVER, what I would prefer to do is have the script "mount the drive." I don't want to go in and click all the network drives to connect them. I don't want to specify all the connection garbage. I just want to tell windows, "Mount everything that you already know how to mount."
This is all information that our computer services people have already figured out and configured on all our machines.

How to redirect Windows cmd output to a text file?

I am trying to monitor the cmd output of a certain executable .exe and use it for another process running at the same time.
The problem is that all cmd redirecting functions ( '>','>>','<' and '|') would only redirect the output after a successful return of the last command.
What I wanted to do is to generate some kind of streaming log of my cmd.
You can run in your process in background by using
start /b something.exe > somefile.txt
Windows 20H2: Redirection works fine when logged in as true administrator but will NOT work when logged in as a created administrative user. I have used it for years to redirect the output of a multi target backup system using Hobo copy to put the console output in a log file. I have never been able to get it to work successfully in Windows 10 ver 19 or 20 on a created administrative user.
You can prefix the command with "cmd /c" to start a new command prompt, and redirect the output of the command prompt:
cmd /c YOUR CODE > TextFileName.txt
Note : Use ONLY single greater than (>)
Since the output of cmd is going to TextFileName.txt.
Whereas this misses the error output, so you are not able to see : Request to UnKnown timed-out for each failed address.

How to combine batch file and expect script in cygwin?

i have an application on windows that requires user name and password to start, but i don't want user to enter the user/password, so i created a script to interact with CMD interface to enter the password, using cygwin on windows 8.1, but i can't find expect command on the Cygwin, and i would like to ask also if my script has any issue.
below is the script
#!/usr/bin/expect
sh C:\Users\Osama.Barakat\Desktop\batchfile.bat
expect "Enter the password for administrator:"
send "Pa$$w0rd"
interact
I identified two issues in your question:
How to perform batch files in cygwin/bash?
How to get expect installed/running in cygwin/bash?
1. How to perform batch files in cygwin/bash?
In your sample code, I found
---8<---
sh C:\Users\Osama.Barakat\Desktop\batchfile.bat
--->8---
IMHO, this is a mistake. I believe that bash will try to execute anything as script regardless of the script file extension. But if batchfile.bat is really a batch file then it should be called with the correct interpreter (i.e. cmd.exe) instead.
I tried it to see if there is any issue in the cygwin bash but it worked. My sample batch file test-expect.bat:
#echo off
rem output
echo Hello %USERNAME%
rem input
set /p INPUT= Enter input:
rem output
echo Input: %INPUT%
...tested in bash on cygwin:
$ cmd /c test-expect.bat
Hello Scheff
Enter input: Hello cmd
Input: Hello cmd
$
OK, done.
2. How to get expect installed/running in cygwin/bash?
To install expect on cygwin I googled a little bit and found Installation of Cygwin, expect and Ssh. Then, I started the cygwin-setup setup-x86.exe and tried something else:
On the "Select Packages" page, I set View to Full and Search to expect. I got a list of four entries only where the first was: "expect: Tool for automating interactive applications" (Category: Tcl). I choosed this for installation and had to confirm another package as dependency.
After installation, I tried this in an interactive bash:
$ which expect
/usr/bin/expect
$
OK - looks quite good.
Combining CMD.EXE and expect
To put all together, I wrote another test script test-expect.exp:
#!/usr/bin/expect
spawn cmd /c test-expect.bat
expect "Enter input: "
send "Hello expect\r"
interact
Tested in bash on cygwin:
$ ./test-expect.exp
spawn cmd /c test-expect.bat
Hello Scheff
Enter input: Hello expect
Input: Hello expect
$
Well, it seems to work.

Change PING script to TELNET script?

I need to turn this ping script into a telnet script which I'd like to configure the script to telnet to the address (on my separate target list *.txt file) & either:
connect/disconnect - write success results to file
or
fail - write fail results to file,
& go to next record, then end...
HELP? :)
#echo off
cls
echo Ping test in progress...
for /F %%i in (iplist.txt) do ping -n 3 -a %%i >> result.txt
echo .
echo .
echo Result is ready.
You cannot simply replace ping with telnet. For one thing, the telnet shipped with Windows isn't scriptable in the first place, so you'd have to jump through some hoops to make it work in a script. You'd be better off using a telnet that's actually scriptable, like plink from the PuTTY suite. Also telnet clients can talk to arbitrary services, so you need to specify where you want to connect to (a telnet server uses a different protocol than, say, a web server or a mail server).

Output domain COMPUTERNAME and logged in USERNAME spreadsheet

So the boss asked for a spreadsheet that would show him all the computers in our enterprise and the users that are currently logged in.
I stopped asking "why?" and started with NBTSTAT. The <03> results were inconsistent. Then I tried NET CONFIG WORKSTATION, and finally PSLOGGEDON.EXE (SYSINTERNALS). These were good, but I'd have to find a way to pass the results of NET VIEW and format the output nicely for a csv.
But then I thought there must be a better way. 90% of our PCs are WinXP so I could use WMIC or maybe DSQuery. I'd rather isolate the command execution to the workstations in our AD Computers container and not touch our Servers.
Does anyone have any recommendations?
You could do an LDAP search to Active Directory (LDIFDE)
ldifde -m -f OUTPUT.LDF -b USERNAME DOMAINNAME * -s SERVERNAME -d "cn=users,DC=DOMAINNAME,DC=Microsoft,DC=Com" -r "(objectClass=user)"
searching users then computers by changing the objectclass.
You can also specify just the attributes you want at the end.
eg: ldifde -f e:\Exportuser.ldf -s DomainController -d "ou=user,ou=sitename,dc=uk" -p subtree -r "(objectClass=User)" -l givenname,sn,samaccountname,mail,dn
or use a .net script
PsExec.exe + PsLoggedOn.exe + batch file.
Step 1. Download PsExec.exe and put it in a folder.
Step 2. Put PsExec.exer and PsLoggedOn.exe in a folder together.
Step 3. Determine a network share.
Step 4. Create a text file called pc_list.txt with the results of NET VIEW pasted in it (minus the "\" - should be one computer name per line). Basically should be a text with the following:
computer1
computer2
computer3
computer4
10.1.1.1
10.2.1.3
10.3.1.4
etc.
You can use IP's or Computer names.
Step 5. Create a Batch file called PsExec.bat with the following code:
"location_of_PsExec.exe"\PsExec.exe #"location_of_text_file"\pc_list.txt -u domain\Admin_username -p password -n 60 -c -f "location_of_runPsLoggedOn.bat"\runPsLoggedOn.bat
Step 6. Create another batch file called runPsLoggedOn.bat with the following code:
#echo off
echo.%computername%>>"location_of_network_share"\userlist.txt
PsLoggedOn.exe -x>>"location_of_network_share"\userlist.txt
echo.>>"location_of_network_share"\userlist.txt
Step 7. Run the PsExec.bat
This is really probably not going to be the EXACT answer you're looking for, but this should at least get you pointed in the right direction...
I didn't test this out, but I use something similar to get other things done using PsExec.exe.
You would want to just write in the code to only output the domain\username to the file on your network share, (from PsLoggedOn.exe -x>>"location_of_network_share"\userlist.txt) because the output is like this:
Users logged on locally:
domain\user
No one is logged on via resource shares.
You also might want to find, as far as formatting goes, a way to output that username to a variable so that you could do the following:
echo.%computername% %variable_username_from_PsLoggedOn%>>"location_of_network_share"\userlist.txt
Anyhow, this should get you started on your way to getting this solved...
How about using Netscan from Softperfect?
This seems to give me what I want:
FOR /F "skip=3 delims=\ " %%A IN ('net view') DO WMIC /Node:"%%A" ComputerSystem Get UserName /Format:csv 2>nul | MORE /E +2 >> computer_user_list.csv
Note that there is a tabulator followed by a space after delims=\
Maybe someone else could use it.

Resources