Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Windows Scripting


  • Please log in to reply
11 replies to this topic

#1 MDTechService

MDTechService

  • Members
  • 303 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Maryland
  • Local time:03:00 AM

Posted 02 October 2012 - 07:31 PM

I've recently started playing around with scripting in Windows. My first attempt is just a utility batch file that pulls a lot of different system information and exports to text and csv files. If someone would be so generous as to provide some constructive criticism, I'd appreciate it :)

Mike

Echo off

del C:\information.txt

Echo Detailed system information for %username% >>C:\information.txt

Echo This information was retrieved on %date% at %time%. >>C:\information.txt

Echo ========================================================== >>C:\information.txt

Echo Below is the IP configuration information >>C:\information.txt

ipconfig /all >>C:\information.txt

Echo ========================================================== >>C:\information.txt

Echo Below are the active TCP connection (ports) on which your computer is listening. >>C:\information.txt

Echo Please wait while I gather the active ports on your computer.

Netstat >>C:\information.txt

Echo ========================================================== >>C:\information.txt

Echo Below is the general system information >>C:\information.txt

systeminfo >>C:\information.txt

Echo ========================================================== >>C:\information.txt

Echo Below are the drivers currently installed on the system >>C:\information.txt

driverquery >>C:\information.txt


Echo ========================================================== >>C:\information.txt

Echo Below are the current tasks running on your computer >>C:\information.txt

tasklist >>C:\information.txt

Echo ========================================================== >>C:\information.txt

Echo Getting installed programs

Echo Below is a listing of all currently installed programs >>C:\information.txt

wmic product list /format:csv >>C:\information.txt
wmic product list /format:csv >>C:\installed_programs.csv

Echo ========================================================= >>C:\information.txt

Echo Below is the directory listing and free space available on all of your disks >>C:\information.txt

Echo Analyzing directories

C:
dir >>C:\information.txt
D:
dir >>C:\information.txt
E:
dir >>C:\information.txt
F:
dir >>C:\information.txt
G:
dir >>C:\information.txt
H:
dir >>C:\information.txt
I:
dir >>C:\information.txt
J:
dir >>C:\information.txt
K:
dir >>C:\information.txt
L:
dir >>C:\information.txt
C:

Echo ========================================================== >>C:\information.txt

Echo Process Complete

Echo Program Will Terminate When You Close Notepad

C:\information.txt
C:\installed_programs.csv

If I am helping you and I haven't replied to your thread in 3 days, please PM me or bump it

Mike D, BS, A+, HPSP, MCTS
I <3 Linux
The Airline Open source airline simulation game
Check the power cable to the wall first!

BC AdBot (Login to Remove)

 


#2 Romeo29

Romeo29

    Learning To Bleep


  • BC Advisor
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:03:00 AM

Posted 02 October 2012 - 08:38 PM

This is a great script :thumbup2:

There is only one problem, newer versions of Windows like Windows 7 do not allow a regular user to create or save files in the root folder (in C:\). So you better save it in the user's profile folder. how about using %UserProfile%\Information.txt ?

Let us know what other things you add in this script :)

#3 MDTechService

MDTechService
  • Topic Starter

  • Members
  • 303 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Maryland
  • Local time:03:00 AM

Posted 02 October 2012 - 09:07 PM

Because of having to access the other disks, it has to be run as an administrator. When run as an administrator, it will allow the creation of the files in the C:\ directory.

Perhaps I will develop a regular user version?
If I am helping you and I haven't replied to your thread in 3 days, please PM me or bump it

Mike D, BS, A+, HPSP, MCTS
I <3 Linux
The Airline Open source airline simulation game
Check the power cable to the wall first!

#4 PropagandaPanda

PropagandaPanda


  • Malware Response Team
  • 10,433 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:04:00 AM

Posted 02 October 2012 - 10:27 PM

Hello.

Do you have any programming background?

For code that you are repeating for slightly different entries, you can use FOR.

We can replace
ECHO AA
ECHO BB
ECHO CC
ECHO DD

with

FOR %%A in (AA BB CC DD) DO (
    ECHO %%A
)


With Regards,
The Panda

#5 MDTechService

MDTechService
  • Topic Starter

  • Members
  • 303 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Maryland
  • Local time:03:00 AM

Posted 02 October 2012 - 11:58 PM

A very limited programming background. I know the fundamentals of C++ and C#, but most of my work deals with X/HTML and CSS.

After staring at the screen for 15 minutes, this is what I'm thinking:

FOR %%dir in (information.bat) DO
    dir >>C:\information.txt

FOR %%brk in (information.bat) DO
    echo ================ >>C:\information.txt


and then the script would appear

C:
%%dir
...
...
L:
%%dir
C:
%%brk
I'm a bit unclear on the usage of the set parameter. I think the rest of it is correct, but I may very well be out in left field somewhere.

Edit: I went google hunting and did some reading. I'm now tired and more thoroughly confused than when I started. I'll look more into it tomorrow.

Edited by MDTechService, 03 October 2012 - 12:52 AM.

If I am helping you and I haven't replied to your thread in 3 days, please PM me or bump it

Mike D, BS, A+, HPSP, MCTS
I <3 Linux
The Airline Open source airline simulation game
Check the power cable to the wall first!

#6 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:01:00 AM

Posted 03 October 2012 - 01:48 AM

Unless you need immediate easy deployment to machines you don't control, you might want to consider using PowerShell instead of the windows batch processor. The batch processor is mostly a complicated monstrosity designed to be compatible with MS-DOS batch files. It has a number of significant problems, such as a lack of any standard way of handing Unicode in the console.

PowerShell is the "modern replacement" of the batch processor on Windows; but unfortunately it isn't installed by default. (At least, this is what I have been told; I have not verified this myself)

Unfortunately I can't speak much as to the differences between the languages because I avoid either like The Black Plague. :)

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#7 Romeo29

Romeo29

    Learning To Bleep


  • BC Advisor
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:03:00 AM

Posted 03 October 2012 - 07:47 AM

Because of having to access the other disks, it has to be run as an administrator.

What are you talking about? All the commands in this batch can be run by a standard user. You do not need Admin access.

Edited by Romeo29, 03 October 2012 - 07:47 AM.


#8 ident

ident

  • Members
  • 109 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Cambridge
  • Local time:04:00 AM

Posted 03 October 2012 - 09:57 AM

I have no Batch scripting experience so i wont comment on that part. How ever what i will point out to you if your file name changes you will have to change it X amount of times. Also rethink the design of reusing code. Does Batch scripting allow methods?

Edited by ident, 03 October 2012 - 09:57 AM.


#9 MDTechService

MDTechService
  • Topic Starter

  • Members
  • 303 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Maryland
  • Local time:03:00 AM

Posted 03 October 2012 - 11:15 AM

Unless you need immediate easy deployment to machines you don't control, you might want to consider using PowerShell instead of the windows batch processor.


I haven't really looked at Powershell yet. I have 7 Ultimate, so it's sitting there waiting for me.

What are you talking about? All the commands in this batch can be run by a standard user. You do not need Admin access.


Nevermind. I completely overlooked that the reason it kept giving me access denied was because the text file had been created in the C:\ directory. Duhhhh :)
If I am helping you and I haven't replied to your thread in 3 days, please PM me or bump it

Mike D, BS, A+, HPSP, MCTS
I <3 Linux
The Airline Open source airline simulation game
Check the power cable to the wall first!

#10 AceInfinity

AceInfinity

  • Members
  • 30 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Canada
  • Local time:01:00 AM

Posted 07 October 2012 - 08:19 PM

Powershell is one of my favorite languages. :)

For this batch script though, there's a few things that can be improved here.

@echo off

set info=%WinDir:~0,1%:\information.txt
set programs=%WinDir:~0,1%:\installed_programs.csv
if exist %info% del /f/q %info%

(
echo Detailed system information for %username%
echo This information was retrieved on %date% at %time%.
echo ----------------------------------------------------------
echo Below is the IP configuration information
ipconfig /all
echo ----------------------------------------------------------
echo Below are the active TCP connection ^(ports^) on which your computer is listening.
) > %info%

echo Please wait while I gather the active ports on your computer.

(
netstat
echo ----------------------------------------------------------
echo Below is the general system information
systeminfo
echo ----------------------------------------------------------
echo Below are the drivers currently installed on the system
driverquery
echo ----------------------------------------------------------
echo Below are the current tasks running on your computer
tasklist
echo ----------------------------------------------------------
) >> %info%

echo Getting installed programs
wmic product list /format:csv >> %programs%
(
echo Below is a listing of all currently installed programs
wmic product list /format:list
echo ----------------------------------------------------------
echo Below is the directory listing and free space available on all of your disks
) >> %info%

echo Analyzing directories

for %%A in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
	dir %%A: >> %info%
)

echo ---------------------------------------------------------- >> %info%
echo Process Complete
echo Program Will Terminate When You Close Notepad

%info%
%programs%

Echoing a CSV based format to a text file will not work very well btw.. And instead of switching to the drive then calling dir, why not call dir to the filepath of the drive letter? This avoids any errors when the drive does not exist. You also assume that the Windows Directory IS on drive C:\ which may not always be the case.

Edited by AceInfinity, 07 October 2012 - 08:38 PM.

mvp.png
Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer


#11 DarkSnake-Kobra

DarkSnake-Kobra

  • Members
  • 633 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Iowa, USA
  • Local time:03:00 AM

Posted 08 October 2012 - 11:36 AM

PowerShell is the "modern replacement" of the batch processor on Windows; but unfortunately it isn't installed by default. (At least, this is what I have been told; I have not verified this myself)


Windows 7/8 come with PowerShell installed by default under Accessories. Vista I believe they eventually offered that as a recommended update. :)

#12 MDTechService

MDTechService
  • Topic Starter

  • Members
  • 303 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Maryland
  • Local time:03:00 AM

Posted 09 October 2012 - 05:29 PM

I have PowerShell, just haven't gotten around to playing with it yet :)

Ace, thanks for the suggestions. I will probably implement them once I read through some syntax and usage stuff.
If I am helping you and I haven't replied to your thread in 3 days, please PM me or bump it

Mike D, BS, A+, HPSP, MCTS
I <3 Linux
The Airline Open source airline simulation game
Check the power cable to the wall first!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users