Script to get Windows version and send it by email - windows

I want to monitor version of Windows on all computers in a domain. So I want to create a .bat file that get the local Windows version and if possible send it via email using telnet command.
I never wrote a script in Windows. So it's difficult for me to start in this area. So any help will be appreciated.

for /f "skip=3 delims=\" %%A in ('net view ^| findstr /v /C:"The command completed successfully"') do Echo %%A
Will give you a list of computernames. Put it in a file.
for /f "skip=3 delims=\" %%A in ('net view ^| findstr /v /C:"The command completed successfully"') do Echo %%A >> Computername.txt
Then type to do turned on computers
wmic /node:#"Computername.txt" os get version /format:csv

Related

Get from the windows system the user email registered from cmd

do you know how to retrieve from the windows system the email with which the user registered in the operating system? When you create an account, windows asks you for an email to register. Is it possible to retrieve that information via code line, cmd or who knows what else?
I got a customer PC in today which actually had a single account which was created using an email address. The only possibilities I could find were by trying to retrieve the email address via the Windows registry.
This first idea was to see if the user account still had the default OneDrive account attributed to that email. So based upon that as a possibility you could try to isolate it from the User registry branch.
From the Command Prompt:
For /F "EOL=H Tokens=2*" %G In ('%SystemRoot%\System32\reg.exe Query "HKCU\SOFTWARE\Microsoft\OneDrive\Accounts\Personal" /V "UserEmail" 2^>NUL') Do #Echo(%H
From a batch file:
#For /F "EOL=H Tokens=2*" %%G In ('%SystemRoot%\System32\reg.exe Query "HKCU\SOFTWARE\Microsoft\OneDrive\Accounts\Personal" /V "UserEmail" 2^>NUL') Do #Echo(%%H
Alternatively you'd have to use the Machine registry branch, which would list all found. This however would only identify the current user if theirs was the only account on that machine which had been created to log in with an email address created account. If there are more than one, then it would list them all.
From the Command Prompt:
For /F "Delims=" %G In ('%SystemRoot%\System32\reg.exe Query "HKLM\SOFTWARE\Microsoft\IdentityStore\LogonCache" /S /F "Name2Sid" /K 2^>NUL ^| %SystemRoot%\System32\find.exe "HKEY_"') Do #For /F "EOL=H Tokens=2*" %H In ('%SystemRoot%\System32\reg.exe Query "%G" /S /V "IdentityName" 2^>NUL ^| %SystemRoot%\System32\find.exe "#"') Do #Echo(%I
From a batch file:
#For /F "Delims=" %%G In ('%SystemRoot%\System32\reg.exe Query "HKLM\SOFTWARE\Microsoft\IdentityStore\LogonCache" /S /F "Name2Sid" /K 2^>NUL ^| %SystemRoot%\System32\find.exe "HKEY_"') Do #For /F "EOL=H Tokens=2*" %%H In ('%SystemRoot%\System32\reg.exe Query "%%G" /S /V "IdentityName" 2^>NUL ^| %SystemRoot%\System32\find.exe "#"') Do #Echo(%%I
Please note, as per the comment section, the majority of user's computers I've worked on, and that is a very large number, do not have user accounts created using an email address, and the above examples would be unlikely to perform the task you require.

For loop not working properly in batch file

I am trying to get the current operating system using this batch file. It is:
#echo off
for /f "skip=1 tokens=1* delims= " %%a in ('wmic path win32_operatingsystem get caption') do set _os=%%a %%b %%c %%d
echo You are running %_os%.
goto :eof
My operating system is windows 7 so I expected it will return:
You are running Microsoft Windows 7 Ultimate.
But it returns you are running %c %d.
Why I am getting this result?
There are a few ways, one already posted by Mofi in the above comment which already demonstrates the main change using /value. You can also use the caption itself as variable name with its value:
#echo off
for /f "delims=" %%a in ('wmic path win32_operatingsystem get caption /value') do set %%a>nul 2>&1
echo %caption%

Collect each service's display name and status

I want to get list of services with their display name and their status.
This is what I have tried:
for /f "tokens=2" %s in ('SC query state^= all ^| find "DISPLAY_NAME"') do #(for /f "tokens=4" %t in ('SC query %s ^| find "STATE"') do #echo %s is %t)
But this returns only limited services such as disk, etc.
This is a perfect task for the built-in WMI command line executable, WMIC.exe.
From the cmd.exe prompt:
For /F "Skip=1 Delims=" %A In ('"WMIC Service Get DisplayName, Name, State"') Do #For /F "Delims=" %B In ("%A") Do #Echo(%B
From a batch file:
#For /F "Skip=1 Delims=" %%A In ('"WMIC Service Get DisplayName, Name, State"'
) Do #For /F "Delims=" %%B In ("%%A") Do #Echo(%%B
#Pause
Try getting help from powershell directly in your batch file, like this for example (save as .bat and run it). In the example i type it to the screen and give you ALL services, RUNNING ONLY, and STOPPED ONLY but you could do basically what you want with the content of that txt file (search it with a loop, save a part to a variable, etc.).
#echo off&cls
pushd %~dp0
echo.
echo List ALL services
pause
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "powershell Get-Service | Out-File services.txt"
type services.txt
echo.
echo List services that are RUNNING only
pause
find "Running" services.txt
echo.
echo List services that are STOPPED only
pause
find "Stopped" services.txt
pause
:cleanup
del /f services.txt

Batch scripting, mutlple finds and multple do sets

At the moment I am running the following to get some information from 'systeminfo', however it needs to run systeminfo twice which takes some time. How would I be able to do multple 'Find"XXX" do sets'?
For /f "delims=" %%A IN ('systeminfo ^| Find "OS Name"') DO Set "VarA=%%A"
For /f "delims=" %%A IN ('systeminfo ^| Find "BIOS Version"') DO Set "VarB=%%A"
Any help is greatly appreciated.
Next code snippet could work (note set commands are merely ECHOed for debugging purposes; remove capitalized ECHO no sooner than debugged):
For /f "delims=" %%A IN ('systeminfo') DO (
For /F "delims=" %%G IN ('echo %%A ^| Find /I "OS Name"') Do ECHO Set "VarA=%%A"
For /F "delims=" %%G IN ('echo %%A ^| Find /I "BIOS Version"') DO ECHO Set "VarB=%%A"
)
However, follow Stephan's advice and parse wmic output rather (do not forget /value option). To do that correctly, note great Dave Benham's article WMIC and FOR /F: A fix for the trailing <CR> problem
I too would use WMIC, but there is a simple solution if you want to use SYSTEMINFO - use FINDSTR with two /C:"search strings"
for /f "tokens=1,2*" %A in ('systeminfo ^| findstr /c:"OS Name" /c:"BIOS Version"') do set "%A=%C"
The above will define two variables on an English machine: OS and BIOS.
Thanks for the replies guys.
I wanted to set the bit version windows the batch file would run on, I figured it out ages ago but thought I would post it here for those that came across it:
echo %PROCESSOR_ARCHITECTURE% | find "64" > NUL
If %ERRORLEVEL% equ 0 (Set bit=64) else (Set bit=32)
Thanks!

How do I get the parent command in Windows command line?

In a Unix shell, I can get the parent command using ps -ocommand= -p $PPID. How can I do the same from the Windows shell?
I need this for a Git pre-commit hook that detects whether the commit was initiated with the --amend flag.
A crude way of doing this is by looking up current PID using a title query.
title ABC
for /f "tokens=2" %%P in ('tasklist /V ^| findstr "ABC"') do set CurrentPid=%%P
for /f "tokens=2 skip=1" %%P in ('wmic process where ProcessId^=%CurrentPid% get Caption^,ParentProcessId^,ProcessId') do set ParentProcessId=%%P
wmic process where ProcessId=%ParentProcessId% get CommandLine
There is a lot that can be optimized there.

Resources