Identify Comuter is in Domain or Workgroup? - windows

Is there a quick way to identify whether a computer is member of domain or workgroup using batch script? If it is the member of domain, What is the domain's name?

Finally I was able to find it with powershell and capture it output in batch variable.
Following is the code
#echo off
REM Bellow logic is to identify "Computer membership"? True=Member of domain | False=Member of Workgroup
for /f %%a in ('powershell "(Get-WmiObject -Class Win32_ComputerSystem).PartOfDomain"') do set ComMem=%%a
echo %ComMem%
If %ComMem% Equ True (
GoTo Domainmember
) Else (
GoTo WorkgroupMember
)
REM Bellow logic is will find Domain / Workgroup Name
:Domainmember
for /f %%f in ('powershell "(Get-WmiObject Win32_ComputerSystem).Domain"') do set MemNme=%%f
Pause
GoTo End
:WorkgroupMember
for /f %%f in ('powershell "(Get-WmiObject Win32_ComputerSystem).Workgroup"') do set MemNme=%%f
:End
Echo %MemNme%
pause

Related

How to find which network profile(s) are active via cmd or batch script

In the windows registry here:
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles\
we can see a list of network IDs with Network name like usually the word Network plus a number eg Network 1 for wired networks.
When Windows first detects a new network you get a pop up asking you if this a Work, Home or Public location. The choice you make changes the Category and Category type in the above mentioned registry entry for that network ID.
I want to retrieve the Category and Category type for the currently connected network profile. Essentially telling me if the network I'm connected to right now is Work, Home or Public.
I know how to do this with a reg query already but I don't know how to find out which of the many network profiles in the above registry path is the active one without looking through them all and matching the network name to the current network.
I looked into using wmic path Win32_NetworkAdapter and I thought I found the way, see here:
for /f "tokens=*" %%f in ('wmic /node:MYPC path WIN32_NetworkAdapter where "GUID is not null" get GUID /value ^| find "="') do set "%%f"
reg query "\\MYPC\HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles\%GUID%" /v Category
I though I found the ID of the current active network by using wmic, using wmic path WIN32_NetworkAdapter as a key called GUID as it was the exact same format as the network IDs you see in the registry path specified above.
As I only had one active network and the above wmic command returned only one non null GUID. But I did not find the returned GUID in the network profile list...
If I can find a way to determine which subkey / network ID in that registry path corresponds to my active network then I can get my desired result.
How can I find out which network profile is being used currently?
Refer to Extracting Network Profiles and displaying only Network Profile description in txt file
You can try something like that :
#echo off
Title GET GUID NETWORK PROFILES
Mode 70,5 & color 0A
:::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights
:::::::::::::::::::::::::::::::::::::::::
REM --> Check for permissions
Reg query "HKU\S-1-5-19\Environment" >nul 2>&1
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
Echo.
ECHO **************************************
ECHO Running Admin shell... Please wait...
ECHO **************************************
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"=""
echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:gotAdmin
::::::::::::::::::::::::::::
:: START ::
::::::::::::::::::::::::::::
Mode 90,20 & color 0A
SetLocal EnableExtensions DisableDelayedExpansion
(Set k=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles)
For /F "Delims==" %%A In ('Set GUID[ 2^>Nul') Do Set "%%A="
Set "i=101"
For /F "Tokens=1,2*" %%A In ('Reg Query "%k%" /S /V Description') Do (
If "%%~nB" NEQ "%%~B" (
Call Set "GUID[%%i:*1=%%]=%%~nB"
) Else (
Call Call Set GUID[%%i:*1=%%]="%%%%GUID[%%i:*1=%%]%%%%","%%C"
Set/A i+=1
)
)
If %i% NEQ 101 (Set GUID[) |findstr /r "{[ABCDEF0123456789][ABCDEF0123456789-][ABCDEF0123456789-]*"
If %i% NEQ 101 (>"%~dp0NetProfs.txt" 2>Nul Set GUID[ |findstr /r "{[ABCDEF0123456789][ABCDEF0123456789-][ABCDEF0123456789-]*")
EndLocal
pause & exit

for loop with array in batch file with replacing a string variable

I am trying to create a batch file to copy the file over network.
first i am mapping the drive using NETcommand and than copy file using xcopy.
follwing batch file works without any problem
net use T: \\192.168.170.239\D /user:User1 PASSWROD
set source=T:\backup
set destination=D:\backup\
xcopy %source% %destination% /y
net use T: /delete
TIMEOUT 5
I would like to replace the static IP '192.168.170.239' and make any array of ip as below and replace the netuse command in a loop.
#echo off
set obj[0]=192.168.170.239
set obj[1]=192.168.170.240
set obj[2]=192.168.170.241
set obj[3]=192.168.170.242
I have treid the following but didn't work
#echo off
set len=2
set obj[0]=192.168.170.239
set obj[1]=192.168.170.240
set i=0
:loop
if %i% equ %len% goto :eof
for /f "usebackq delims== tokens=2" %%j in (`set obj[%i%]`) do (
net use T: \\%%j\D /user:User1 Password
TIMEOUT 10
set source=T:\Autobackup
set destination=D:\Autobackup\
xcopy %source% %destination% /y
net use T: /delete
TIMEOUT 10
)
set /a i=%i%+1
goto loop
It works for the second ip but not for the first ip.
You should really be looking at a structure more like this:
#Echo Off
Rem Undefine any existing variables beginning with obj[
For /F "Delims==" %%A In ('Set obj[ 2^>Nul') Do Set "%%A="
Rem Define your IP list
Set "obj[0]=192.168.170.239"
Set "obj[1]=192.168.170.240"
Set "obj[2]=192.168.170.241"
Set "obj[3]=192.168.170.242"
Rem Define your Map Source and Destination
Set "map=T:"
Set "source=%map%\Autobackup"
Set "destination=D:\Autobackup\"
Rem Loop through the IP list
For /F "Tokens=1* Delims==" %%A In ('Set obj[ 2^>Nul') Do (
Rem Make sure that %map% is not currently mapped
Net Use %map% /Delete 2>Nul
Rem Map the share
Net Use %map% \\%%B\D /User:User1 Password
Rem Perform the required operation
XCopy "%source%" "%destination%" /Y
Rem Delete the mapped share
Net Use %map% /Delete
)
Compo's answer is good. Another way to construct the loop follows. It does not use an "array" of variables. I find this easier to edit and understand.
SET IPLIST=^
192.168.170.239 ^
192.168.170.240 ^
192.168.170.241 ^
192.168.170.242
FOR %%a IN (%IPLIST%) DO (
ECHO %%a
)

Extracting Network Profiles and displaying only Network Profile description in txt file

I am very new to batch scripting and have to use console to interrogate Registry for network profile description and output only the description data to a txt file.
I am using a for /f loop to do this.
I first reg query the whole key so it lists every sub key for network profiles and stores this in a text document. I then for /f this text file to extract out only the subkey name using tokens to store this as a variable.
I then use the variable to reg query the individual keys for the Description name and output this to another text file which should display only the Network profile description. Below is my batch script.
Echo Required to skip line for processing >>%~dp0\1SSID.txt
REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles" /s /v Description >>%~dp0\1SSID.txt
setlocal enableDelayedExpansion
rem get each key from 1SSID.txt
for /f "usebackq skip=1 tokens=1,2" %%i in ("%~dp0\1SSID.txt") do (
echo %%i %%j>>%~dp0\2Processingstage.txt
rem skip the first line and grab tokens 3 from the second line to show description and desription name
for /f "usebackq skip=1 tokens=3" %%k in (`reg query "%%I %%j" /v Description`) do set "Description=%%l
echo Network Description - %%l >>%~dp0\3SSIDoutput.txt
)
)
The first think I notice is the skip=1 doesn't work and look at every line.
As this doesn't work it doesn't extract the correct data to place into the reg query. I have tried with different tokens, without skip, with skip, with delims (which it didn't recognise). Ive been working on this for hours and simply cant get it to work. This is probably simple but I cant find a way around this.
Here is an quick example which should output a file providing each GUID and Description alongside your running script.
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
(Set k=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles)
For /F "Delims==" %%A In ('Set GUID[ 2^>Nul') Do Set "%%A="
Set "i=101"
For /F "EOL=E Tokens=1,2*" %%A In ('Reg Query "%k%" /S /V Description') Do (
If "%%~nB" NEq "%%~B" (Call Set "GUID[%%i:*1=%%]=%%~nB") Else (
Call Call Set GUID[%%i:*1=%%]="%%%%GUID[%%i:*1=%%]%%%%","%%C"
Set/A i+=1))
If %i% NEq 101 (>"%~dp0NetProfs.log" 2>Nul Set GUID[)
EndLocal
Exit/B
You will probably need to right-click and run as Administrator due to resrictions on those keys.
From this thread : Wi-Fi SSID detail
I don't know if this code works over english machines or not, because, i just tried it until now, on my french machine.
so, just give a try and tell me the results :
#echo off & setlocal enabledelayedexpansion & color 0A
Title %~n0 to get SSID With details
::::::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights ::
::::::::::::::::::::::::::::::::::::::::::::
Set TmpLogFile=%tmp%\TmpLog.txt
If Exist %TmpLogFile% Del %TmpLogFile%
REM --> Check for permissions
Reg query "HKU\S-1-5-19\Environment" >%TmpLogFile% 2>&1
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
Echo.
ECHO ****************************************
ECHO ^| Running Admin shell... Please wait...^|
ECHO ****************************************
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"=""
echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:gotAdmin
::::::::::::::::::::::::::::
::START
::::::::::::::::::::::::::::
Set "TmpLog=%~dp0%~n0_Tmp.txt"
Set "Log=%~dp0%~n0.txt"
If Exist "%TmpLog%" Del "%TmpLog%"
If Exist "%Log%" Del "%Log%"
rem Populate the array
Set i=0
for /f "skip=1 tokens=2 delims=:" %%a in ('netsh wlan show profiles ^|find /i "Profil"') do (
set /A i+=1
set "list[!i!]=%%a"
)
set SSID=%i%
rem Display array elements for SSID List
cls
for /L %%i in (1,1,%SSID%) do (
echo(
echo SSID number %%i: "!list[%%i]:~1!!"
echo(
)
pause
rem Display array elements for SSID List with details
cls
for /L %%i in (1,1,%SSID%) do (
echo(
echo SSID number %%i: "!list[%%i]:~1!!"
echo(
netsh wlan show profiles "!list[%%i]:~1!!" key=clear
netsh wlan show profiles "!list[%%i]:~1!!" key=clear >> "%TmpLog%"
)
Cmd /U /C Type "%TmpLog%" > "%Log%"
If Exist "%TmpLog%" Del "%TmpLog%"
Start "" "%Log%"
pause
exit /b

How should I Create a Batch Menu based on List of Output

I think I may struggle to explain the how I want my batch script to behave so here goes. My script is meant to act as a menu system requiring user input. What I have managed so far is to send a list of datestamps to output to a user. However I am attempting to arrange the script so any timestamp can be selected such as 1,2,3 and based on a prompt for user input. Later I want to save that to a variable in which I can do another operation with afterwards.
So this is what an example of what I can see:
FOR /f "tokens=3 delims= " %%G IN ('%history% ^| findstr /C:"Start Time:"') DO (call :subroutine %%G)
:subroutine
echo %1
GOTO :EOF
My output now looks like this:
20150706232400
20150706232707
20150706232757
20150706233058
20150706233144
Any pointers appreciated. Thanks.
#echo off
setlocal EnableDelayedExpansion
rem Show menu and store options in an array
echo Available datestamps:
echo/
set i=0
FOR /f "tokens=3" %%G IN ('%history% ^| findstr /C:"Start Time:"') DO (
set /A i+=1
echo !i!. %%G
set "option[!i!]=%%G"
)
echo/
:getChoice
set /P "choice=Enter desired option: "
if "!option[%choice%]!" equ "" echo ERROR: no such option & goto getChoice
set "variable=!option[%choice%]!"
ECHO/
ECHO Selected datestamp: %variable%

Using Windows Command in .cmd to test if 32 bit or 64 bit and run a command

I am writing a script that find a registry value and returns that value to the windows command prompt screen and also adds that to a .txt file. I am to a point where I need to also test to see if the machine is 32 bit or 64 bit so that I know which command to use to find the value I need in the registry.
I am looking for logic along the lines of what I have written below:
If 32 bit then (run this command)
else
(run this command)
I am hoping to not have to have any text files or anything else required for this script to work. Below is the code I have so far. Due to the registry location being private, I changed the location to a made up location. (Note: I do have a text file this script reads from that I insert the names of computers and servers. So far the code I have works but with 2 registry find commands I am obviously not getting right results when its checking a 32 bit machine for a 64 bit registry location)
#echo off
Setlocal
::sets the ldt variable to the local date and time in yyyymmdd_hhmmss
for /f "skip=1" %%c in ('wmic os get LocalDateTime') do set ldt=%%c
set ldt=%ldt:~0,8%_%ldt:~8,6%
::searches for computer names in servers.txt and then calls server sub routine. Once it goes back to the for loop it moves onto the next line in servers.txt
for /f %%c in (servers.txt) do (
set server=%%c
call:server
)
pause
goto:eof
--------------------------------------------------------------------
:server
set stamp=%date% at %time%
set DateTime=%stamp%
::Tests for a good ping, if no ping then move onto next machine
ping -n 1 -w 250 %server% > nul
if %errorlevel% NEQ 0 (
ECHO No Ping on %server%
ECHO No Ping on %server% >>AuditScript_%ldt%.txt
goto:eof
)
call:screen
call:log
goto:eof
--------------------------------------------------------------------
:screen
echo.
echo Computer name: %server%
reg query "\\%server%\HKLM\SOFTWARE\Registry Folder1\Information\" /V "Datavalue" | FIND "Datavalue"
reg query "\\%server%\HKLM\SOFTWARE\Wow6432Node\Registry Folder1\Information" /V "Datavalue" | FIND "Datavalue"
echo.
goto:eof
--------------------------------------------------------------------
:log
echo. >>AuditScript_%ldt%.txt
echo Registry value for %server% on %stamp% >>AuditScript_%ldt%.txt
reg query "\\%server%\HKLM\SOFTWARE\Registry Folder1\Information\" /V "Datavalue" | FIND "Datavalue" >>AuditScript_%ldt%.txt
reg query "\\%server%\HKLM\SOFTWARE\Wow6432Node\Registry Folder1\Information" /V "Datavalue" | FIND "Datavalue" >>AuditScript_%ldt%.txt
echo. >>AuditScript_%ldt%.txt
goto:eof
--------------------------------------------------------------------
FOR /F "tokens=3" %%x in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /V PROCESSOR_ARCHITECTURE') do set CPU=%%x
echo CPU Architecture: %CPU%
if "%CPU:~-2%"=="64" (
echo Do This
) else (
echo Do That
)
Try this:
setlocal EnableDelayedExpansion
for /f "delims== tokens=2" %%a in ('wmic os get osarchitecture /value') do (
set arch=%%a
)
if "%arch%"=="32-bit" (
rem do some
) else (
rem do other
)
endlocal
via cmd you can check the %PROCESSOR_ARCHITECTURE% variable like so:
echo %PROCESSOR_ARCHITECTURE% this will output x86 or x64 depending on the OS
so you can use something along the lines of:
#echo off
if %PROCESSOR_ARCHITECTURE% == x86 (
goto :x86
) else (
goto :x64
)
:x86
start "foo.exe"
goto :eof
:x64
start "bar.exe"
goto :eof
:: Installed OS
Set _Bitness=64
IF %PROCESSOR_ARCHITECTURE% == x86 (
IF NOT DEFINED PROCESSOR_ARCHITEW6432 Set _Bitness=32
)
Echo Operating System is %_Bitness% bit
I think that works

Resources