i work as a network engineer and i would like to write some scripts to make my job more easy especially in situations where i cannot be on site and the client cannot connect to the internet so this is one script i made that would give me basic network test info
However my issue is i cant get it to output to a txt file after the script is ran, i want the user having the choice of weather to create the file or not. after i go through the prompt and click yes it will pause for a bit ( i assume its running the commands) however no file is made on my desktop where the file is stored.
At my work i am running windows 10, and at home xp but i need this to be able to run on either.
here is a copy of what i am having trouble with i have more questions but would first like to get this fixed
i have tried using >>output.txt (commands in here) i also tried command >> output.txt but either way there is no file on my desktop
>>output.txt (ipconfig /all ping google.com tracert google.com)
The file will be saved to your current directory. You'd need to use "%userprofile%\desktop\output.txt" as the file destination to put it on the desktop. You may need the quotes in case the resolution ofuserprofile` contains separators.
I think you have three (3) commands here and need to tell the shell to do them all.
>>"%USERPROFILE%\Desktop\output.txt" (ipconfig /all & ping google.com & tracert google.com)
Alternatively, the .bat script might look like:
>>"%USERPROFILE%\Desktop\output.txt" (
ipconfig /all
ping google.com
tracert google.com
)
Related
I am working on an application that requires my users to share their fully-qualified-domain-name of their windows machine.
To help my users to extract their machine's FQDN, I want to share simple command line steps that they can copy/paste and execute on their terminals to get the result.
I was thinking of below command to extract local machine's FQDN:
echo %COMPUTERNAME%.%USERDNSDOMAIN%
But there are few problems of this command.
It gives output in ALL CAPS. (I can live with it)
It gives incorrect output if the variable is not set.
For example:
If USERDNSDOMAIN value is not set, then, you'll get following output:
echo %COMPUTERNAME%.%USERDNSDOMAIN% //<- Run this on cmd prompt
ClientComputerName.%USERDNSDOMAIN% //<- wrong output: Notice '%USERDNSDOMAIN%' is appended in o/p
Is there any way to stop echoing a variable if it's value is not set?
Please note that I want to extract "fully qualified domain name" of my windows machine through CMD prompt only.
You can get the FQDN name using PowerShell.
=== Get-FQDN.bat
#ECHO OFF
FOR /F %%A IN ('powershell -NoLogo -NoProfile -Command ^
"([System.Net.Dns]::GetHostByName($Env:COMPUTERNAME)).HostName"') DO (
SET "THEFQDN=%%A"
)
ECHO %THEFQDN%
If you have multiple users, then you surely have some way to get programs and batch files installed on them. Once this batch file script is installed into a directory on the user's PATH, it is a one-line command.
Get-FQDN
1st, please note that the "USER DNS Domain" is NOT the domain the computer is joined to, it is the domain the USER who is logged in belongs to.
If you log in as a user from a trusted domain, or a child or parent domain, then it will display that domain.
So, if you log in as a LOCAL account it will be blank (likely you are running into this)
There is a fairly simple way to get the actual computer domain however, by using NLTest. (For the like of me I could never figure out why Microsoft didn't pre-populate a variable with this info.)
At the CMD Line simply dump this into the command prompt (I believe you will need to run with admin privileges but I haven't tested):
FOR /F "tokens=3" %_ IN ('nltest /DOMAIN_TRUSTS /PRIMARY ^|FIND /I "0:"') DO #(ECHO.%COMPUTERNAME%.%_)
The result will be in all caps because that is how Microsoft displays this info.
Here is an example output:
MYLAPTOP.USERS.MYDOMAIN.LOCAL
But on-re-read you want something the users know how to do themselves, so ymmv if you could just send a reference email, or hand it to them each time they need it.
If you just wan this info and other info easily available you could use BGInfo or other options like that to set the desktop background.
Alternatively you could change the logon scripts to generate a simple text file with all the info each time the user logs on, and placed in a certain folder you tell them to look in.
For the particular task I'm doing I am trying to run a .bat file from my computer, but I want the command to execute on another computer. For example I have a .bat file that writes the ipconfig command to a text file. The code for said file looks like this:
#echo off
REM Name: ipconfig.bat
ipconfig /all > a.txt
#pause
Now is when the question I have comes into play; I want to run this .bat file on another computer in my network. So I have written the following .bat file to attempt this:
#echo off
REM Name: SendIpconfig.bat
REM The User variable represents where I tried to enter the I.P address for my laptop.
Set /p n=User:
call ipconfig.bat > %User%
#pause
I have also tried making the file using a | instead of a > when I try to preform my call statement.
You can't just run something on a different computer, there has to be some utility on the remote machine that runs as a server. It listens to incoming connections, runs whatever it is that needs to be executed, and returns the results. This server should also be secure and authenticated (just imagine what would happen if anyone could run arbitrary scripts on your machine just by knowing your ip address).
On Linux, this is normally done using SSH. For windows, what you need is something like PsExec which
"lets you execute processes on other systems, complete with full
interactivity for console applications, without having to manually
install client software"
So normally I can type this into the cmd window ping 216.52.241.254 -t and it tells me my ping to a certain server. How can I create a .bat file that automatically opens the cmd window and types it in so that I don't have to write it out every single time. I tried just putting in ping 216.52.241.254 -t and it just spams it over and over again.
Another way of doing this (potentially a lot easier for you) would be to create a shortcut:
Right Click in windows explorer and hover over "New"
Select "Shortcut"
A dialogue will pop-up. Enter the command you wish to utilize: ping 216.52.241.254 -t
Click Next and name the file.
Now whenever you open the shortcut, it will execute the command.
The advantage of this method over the other is its simpler and allows you to pin it to the Starmenu or Taskbar.
Mona.
It spammed it over and over because you called the batch file ping so it was launching itself.
Very simple:
#echo off
ping 216.52.241.254 -t
Echo.
pause
Open Notepad
Copy and paste this in.
Save as a .bat file, ensuring that you select "all files" option
Run the batch file any time you want to check your ping.
Done!
type the following:
cd\
ping -t 216.52.241.254
You must have keep the file name as 'ping.bat'.
Rename it to something else and it will definitely work.
ex : ping1.bat, something.bat etc., anything else but ping.
It spams it again and again because you used -t which "Pings the specified host until stopped." if you lose the -t it won't do that. Type Ping /? to look through all the options available when using ping and select what is appropriate for what you want it to do.
We need to disconnect and re-map a network drive on Windows 7, using a set of scripts (or an app) that runs off the same network path.
That is, I need something that loads itself into RAM before it runs, so it continues to run after the drive is disconnected.
Any ideas?
Please note that 16-bit apps are NOT supported in 64 bit systems (this explains why the Novell utility failed).
You would need a vbs file running throughout a logon session to remap drives if it's disconnected by user. Need to make this script to run when domain user logs on - e.g. Logon Script in AD or GPO. There are many ways to do it.
You could even disable "Remove Network drives" feature from Explorer GUI via GPO or Reg key (net use command still works).
Or you can tweak solution by Julius for this SO question to fit your need. But consider performance impact of the vbs - only check every n minute(s) in an infinite loop.
We do something similar. We have a batch file on the network that maps the drives a user needs. We update the batch file from time to time, and users run it from a shortcut that we've placed on their desktop:
C:\WINDOWS\system32\cmd.exe /c (#echo off&if not exist \\172.x.x.x\Login (echo Unable to access server&pause) else (md c:\TMP > NUL 2>&1 © \\172.x.x.x\Login\MapDrives.bat C:\TMP /y > NUL 2>&1 &call C:\TMP\MapDrives.bat&del C:\TMP\MapDrives.bat&rd c:\TMP))
You can see that it checks to see if they can access the server, and if they can, it creates a folder C:\TMP, copies the MapDrives.bat file locally, then runs it. Since it is running locally, it can remap network drives without terminating it own execution. And we can update the batch file on the server without pushing it to each user's computer.
If you don't want to create a shortcut with the long command line above, it might work to create a second batch file on the server (e.g., RunMe.bat) that users run from the server. You could place all of the code from the shortcut in the RunMe.bat and accomplish the same thing. Of course, you'd want to add one more line of code to change to the local drive (so Windows doesn't hold open a handle to the network drive). Something like this:
#echo off
C:
if not exist \\172.x.x.x\Login\MapDrives.bat (
echo Unable to access server
pause
) else (
md c:\TMP > NUL 2>&1
copy \\172.x.x.x1\Login\MapDrives.bat C:\TMP /y > NUL 2>&1
C:\TMP\MapDrives.bat
)
I kept the if not exist ... because you might place the RunMe.bat in a different location than the MapDrives.bat, so it still makes sense to verify the user can access the file. Because I didn't use call C:\TMP\MapDrives.bat, it transfers control to the local batch file and any handles to the server should be closed so the drive can be remapped. This means however, that you cannot place more commands after the C:\TMP\MapDrives.bat command.
I have a batch file that part of it copies a folder and its content to a local directory and then continues through the batch process which then tries to execute the contents of the folder . locally run it works on all wintel os and copies the files from \networkshare\folder*.* to %systemdrive%\temp\ .
I have attempted to use e.g psexec #serverlist.txt -c batfile.bat -s -f -d to copy the file and then execute it remotely how ever the issue is that this will not copy the files when run remotely . I think its a authentication issues after you a have remotely executed the batch file the remote system will not allow me to access/authenticate the networkshare
i have tried xcopy , copy , robocopy .
AFAIK you can only authenticate against the next hop using implicit credentials. Connection attempts from the first hop to a second hop will fail, even if your user has the required permisssions. See e.g. here for an explanation.
Try this:
psexec #serverlist.txt -u %USERDOMAIN%\%USERNAME% -d -c batfile.bat
Use explicit credentials so you're authenticated against the remote host. With that the second hop will be the next hop for your authenticated session. Don't run the script as LOCAL SYSTEM (-s), because that account is restricted to local resources.
I've had this issue in the past. Instead of trying to copy/run the BAT file just run the script things from a UNC path if you are able to. I think the problem lies in the BAT file not actually able to run through PSEXEC and copy like you think. Does it give an exit code?
Here's an example I used a while ago to install Adobe reader. I tried to do it through a batch file but no dice. I could not get it to run within the batch file whether I copied it or not. I can't remember the exact reason, I think it had something with the way a batch file is called in the system and runs in some sort of local context. Don't quote me on that though as I can't remember the exact why.
Here is my code example:
psexec /accepteula \\%computer% -s cmd /c msiexec /i "\\UNC\Software\adobe\Adobe Reader 7.0.9.msi" TRANSFORMS="acrobat7.mst" /qn