how to stop echo command from displaying variables which are not set? - windows

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.

Related

WMI Getting Registry Value via Command Prompt

I need to verify if a chrome extension is installed or not installed on remote computers.
Extension id is unique value like that "234aljksdfklja3idffklsasf".
I need to search "HKEY_CURRENT_USER\SOFTWARE\Google\Chrome\PreferenceMACs\Default\extensions.settings"
direction for extension id "234aljksdfklja3idffklsasf"
How to do that?
i think i will use code below but i need some help
WMIC /NameSpace:\root\default Class StdRegProv ....
Instead of WMIC, you should use the REG cmd, it's made to do this.
The HKCU is whatever the user running the command's registry key is.
Reg allows you to load and check the registry keys of any users not just the one running the command, even users who have not logged in since the last reboot.
Since this is a chrome extension you may need to check every used on the system to see if each has it enabled individually.
Alternatively there is probably an HKLM key that corresponds to the extension being installed, if all you want to know is if it's present at all so it can be removed.
Reg also allows you to query your computers through the network.
One issue you will have if you need to check every user's registry for the key is you need to know what the user's SIDs present on the systen are.
This is findable by querying HKLM, but I am on mobile and can't recall how without getting on my computer later to do this.
Alternatively you can also just load the reg hive file by looping the fire tory structure of the remote machine and loading each user.dat file through the reg command which is much simpler but then the code must execute on each remote machine through a login script or GPO script push.
Basically you can use reg in this manner on a local machine to check the reg of the user you are running as (HKCU)
(reg query "HKCU\SOFTWARE\Google\Chrome\PreferenceMACs\Default\extensions.settings" /s | FIND /I "234aljksdfklja3idffklsasf") && Echo.FOUND 234aljksdfklja3idffklsasf
Of course to run on another computer it's fairly simple to do, but HKCU will be the reg of the admin user you ran the command as with access to that other system, or you can try HKLM to see if that setting exists on the local machine key, which the following checks HKLM instead.
(reg query \\[Computer_Name_or_IP_Address]\hklm\SOFTWARE\Google\Chrome\PreferenceMACs\Default\extensions.settings /s | FIND /I "234aljksdfklja3idffklsasf") && Echo.FOUND 234aljksdfklja3idffklsasf
You could loop a set of computer names/IPs and check each using the above command inside the loop like so:
FOR %A IN (
Computer_A
192.168.12.13
192.168.12.31
Computer_C
) DO (
(
reg query \\%~A\hklm\SOFTWARE\Google\Chrome\PreferenceMACs\Default\extensions.settings /s | FIND /I "234aljksdfklja3idffklsasf"
) && Echo.%~A -- FOUND 234aljksdfklja3idffklsasf || ECHO.%~A -- Key Not Found!
)
If you need to check the actual HKCU of every used on the system then you need to load each reg hive on the system and check it, this is true if you use WMIC as well, and Reg is faster.
From the MS reg page:
reg query <KeyName> [{/v <ValueName> | /ve}] [/s] [/se <Separator>] [/f <Data>] [{/k | /d}] [/c] [/e] [/t <Type>] [/z]
i got one more solution to this situation. But only works if extension is packed. Btw this solution also doesnt solve my problem because my extension is unpacked.
Packeted extensions are stored "AppData\Local\Google\Chrome\User Data\Default\Extensions".
wmic /node:remoteip datafile where "name='C:\Users\username\AppData\Local\Google\Chrome\User Data\Default\Extensions\extensionid\1.0.4_0\manifest.json'"
if extension installed command returns manifest.json file specs.
I got the solution;
wmic /node:ipaddr /NAMESPACE:"\\root\DEFAULT" class stdregprov call GetStringValue ^&H80000001,"SOFTWARE\Google\Chrome\PreferenceMACs\Default\extensions.settings","extensionid"
On localhost, it works but on remote computers access denied.

Command prompt executing command differently than batch file and Visual Studio post-build script

I have a post-build script in Visual Studio that I am trying to run so that we can make remote deployments easier. Earlier in this script (you don't see it here), we hard-code the user in the net use execution below. So all we're doing here is having the user input the server name and database to deploy to and then connect to the network share of that server.
What I've found though is that the code below works fine only if you run it with Command Prompt. It opens up a new window and has the user put in the server and database and then the net use command will correctly show the server the user input, all in the same window.
If you try to use this in a batch file or in Visual Studio though, the net use command shows blank for the server name.
I appreciate any insight anyone might have on this!
START CMD /C "CALL SET /P SERVERNAME=Please Enter The Server Name: & CALL SET /P DATABASE=Please Enter The Database Name: & CALL CMD /C net use \\%SERVERNAME%\d$ /persistent:no * /user:%user%"
Follow advise of #bgalea. Where is %user% defined? Did you mean %username%? Of course this scheme will not work if you plan to have builds done by a build agent later.
The reason it is not working is that variables are expanded at load time. Consequently, %SERVERNAME% is expanded when the line is loaded...BEFORE the user has entered a value. Try changing to this:
SETLOCAL ENABLEDELAYEDEXPANSION SET /P SERVERNAME=Please Enter The Server Name: & SET /P DATABASE=Please Enter The Database Name: & \\!SERVERNAME!\d$ /persistent:no * /user:%username%"
That way you will be using the run time value of SERVERNAME.

how can i run batch file and get output back into the batchfile?

hi i am using windows 7 and wish to get the output of a cmd command and append it to my batch file that i issued the cmd command with
example
#echo off
net user REM (which will return a list of users on the system)
now i want to get one of the users that are returned but without knowing the username and send it back into my batch file as a string such as
#echo off
net user
net user REM (and input the returned username here as a string)
so i can carry on with the rest of my commented out strings to get account creation logs of my local machine users i need it to be automated in gathering information for my many users

How can I send a .bat file command to another IP address?

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"

How to check from what path application is run?

For example I have entered
netsh.exe
in command line (aka cmd.exe)
Now I would like to know which netsh.exe is being run, lets say I have more than one netsh.exe on my PATH (I do know that the first one in the PATH will be run, but lets say I have a very BIG PATH and I don't have time to search for it manually. To be fair its not always your machine you are using and many times PATH is set by admins and many times they are not the best).
Is there any way in windows to find that out from command line? I want to write a BATCH application that is using that.
It's a one-liner batch file:
#for %%e in (%PATHEXT%) do #for %%i in (%1%%e) do #if NOT "%%~$PATH:i"=="" echo %%~$PATH:i
Save this as whereis.cmd, then type
whereis netsh
I think the following blog post does exactly what you want: http://pankaj-k.net/weblog/2004/11/equivalent_of_which_in_windows.html
I would use Windows Management Instrument (WMI) to query:
"SELECT ExecutablePath FROM Win32_Process WHERE Name = 'netsh.exe'"
http://www.activexperts.com/activmonitor/windowsmanagement/wmi/samples/ WMI samples
You will need to find something suitable for your scripting
On Windows Server (at least 2003 and 2008, dont know with 2000) you can use where.exe
Where.exe /?
Description:
Displays the location of files that match the search pattern.
By default, the search is done along the current directory and
in the paths specified by the PATH environment variable.
...
The first file listed is also the first file windows will use.
I use a copy on my XP workstation and it works fine too.

Resources