Jump to content
EduGeek EdSec 2026 is Go! 27th Oct in Derby! Join us for a day of EdTech security focused talks, networking, and an evening social ×

Recommended Posts

Posted (edited)

I need to run (correctly) a loop in a script.....

Here's the background:

 

I have a WMIC script that pulls information from a computer. Currently the computer is a single name input on the command line.

 

 

Here's what I currently have (the pauses are just so I can view errors in processing):

 

SET count=1

FOR /f %%a IN (complist.txt) DO Call :subroutine

 

 

:subroutine

Set compname=%%a

set /a count+=1

Echo %compname%

 

 

REM ---------------------------------------------------------- Documentation Beginning

 

Echo.

Echo Connecting to %Compname%...

echo.

 

pause

 

If exist >>%SystemDrive%\WMICDigger\%Compname%_Documentation.csv GOTO :run

else GOTO :write

 

:write

>>%SystemDrive%\WMICDigger\%Compname%_Documentation.csv Echo ID,Location,Owner,Type,Serial,IP Address,OS,Comments

 

:run

Echo Obtaining Computer Name ...

 

Echo Obtaining Manufacturer...

For /F "Tokens=2* Delims==\" %%c In ('WMIC /node:%Compname% /USER:%XUsername% /Password:%XPasscode% ComputerSystem

 

GET manufacturer /Value^|Find "="') Do set Location=%%c

 

Echo Obtaining Last signed on user ...

For /F "Tokens=2* Delims==\" %%i In ('WMIC /node:%Compname% /USER:%XUsername% /Password:%XPasscode% ComputerSystem

 

GET UserName /Value^|Find "="') Do set Owner=%%j

 

Echo Obtaining Model...

For /F "Tokens=2* Delims==\" %%e In ('WMIC /node:%Compname% /USER:%XUsername% /Password:%XPasscode% ComputerSystem

 

GET model /Value^|Find "="') Do set Type=%%e

 

IF Exist %SystemDrive%\WMICDigger\%Compname%.txt del %SystemDrive%\WMICDigger\%Compname%.txt

 

Echo Obtaining Serial Number ...

 

 

For /F %%k in ('Wmic /node:%Compname% /USER:%XUsername% /Password:%XPasscode% bios get serialnumber /Value^|Find

 

"Serial"') Do set Serial=%%k

 

Echo Obtaining IP Address Information...

WMIC /node:%Compname% /USER:%XUsername% /Password:%XPasscode% NICconfig get ipaddress | Find /N "IP Address" >>

 

%SystemDrive%\WMICDigger\%Compname%.txt

 

Echo Obtaining OS Version ...

 

 

 

>>%SystemDrive%\WMICDigger\Documentation.csv Echo %compname%,%Location%,%Owner%,%Type%,%Serial%,%IP Address%,%OS%,"none"

 

pause

 

The bit I'm stuck on is in the initial for /f loop and where to define the variable compname, whilst still running the subroutine.

I need it to stop after each line and define the variable compname as the value of that given line (not working), then process the commands. (command processing has tested fine).

I'll be honest, I'm #### when it comes to for loops and it's usually trial and error, but I'm all trial and errored out and figured the wealth of knowledge here might just turn something up.... :D I'm tired and it needs a fresh set of eyes!!

 

Appreciate any help, and once I've finished it I'll distribute here.

 

DT

Edited by DT2
Posted

To be honest I didn't get your question, but do you mean like this?

 

@echo off

SET count=1
FOR /f %%a IN (complist.txt) DO (
Set compname=%%a
Call :subroutine 
)


:subroutine

set /a count+=1
Echo %compname%

pause 

 

aka, it'll use the var from the loop inside the routine?

 

Steve

  • Thanks 1
Posted
To be honest I didn't get your question, but do you mean like this?

 

@echo off

SET count=1
FOR /f %%a IN (complist.txt) DO (
Set compname=%%a
Call :subroutine 
)


:subroutine

set /a count+=1
Echo %compname%

pause 

 

aka, it'll use the var from the loop inside the routine?

 

Steve

 

Thanks Steve. Give me half hour to have my lunch and I'll redress the issue so it makes sense.

 

Darren

Posted

wmicdigger2.zip

 

Ok, so attached is the script...

It pulls information from a computer, you give it a computer name and your domain credentials. It then goes away, runs a set of commands and pulls the information then writes it to a .csv file.

 

What I want it to do is the same thing, but pull the computer names from a text file, set the compname variable and have it run the commands against each computername taken from the text file without having to input user credentials each time.

 

I have a netscan pulling dhcp/computer name information from each subnet, and I don't want to have to use my script x amount of times and have to consolidate x amount of files.

So I've set it to write the information to csv. I'd like it to append all the info for all the computer names processed to the same file.

 

Does that make more sense?

 

The only thing I'm lacking is a sufficient loop to process this. What I need it to say is "for every line in x.txt do set compname then process the commands....." already set an infinite loop once this morning.... tiredness and distractions don't help!!

 

Thanks

 

Darren

Posted
[ATTACH]17283[/ATTACH]

 

Ok, so attached is the script...

It pulls information from a computer, you give it a computer name and your domain credentials. It then goes away, runs a set of commands and pulls the information then writes it to a .csv file.

 

What I want it to do is the same thing, but pull the computer names from a text file, set the compname variable and have it run the commands against each computername taken from the text file without having to input user credentials each time.

 

I have a netscan pulling dhcp/computer name information from each subnet, and I don't want to have to use my script x amount of times and have to consolidate x amount of files.

So I've set it to write the information to csv. I'd like it to append all the info for all the computer names processed to the same file.

 

Does that make more sense?

 

The only thing I'm lacking is a sufficient loop to process this. What I need it to say is "for every line in x.txt do set compname then process the commands....." already set an infinite loop once this morning.... tiredness and distractions don't help!!

 

Thanks

 

Darren

 

If the rest of that script works ok, the loop I posted above should do it.

 

It takes a line from the text file, runs the subroutine, then takes next and runs it etc.

 

So you should be able to put password/username in once, before the loop then it'll run through the file doing it for everyone.

 

If you run that little snippet I posted above, it should do what you want just as a sample. Before editting the big one I mean.

 

Steve

  • Thanks 1
Posted

So a moment of clarity, and I realise I'm monging it.... :doh:

 

:yield:

 

I'm going all french and throwing in the towel for today.....

 

I'll pick it up monday and hopefully be able to compile all the working parts into something functional.

 

Muchas Gracias @Steve21

 

Darren

Posted
So a moment of clarity, and I realise I'm monging it.... :doh:

 

:yield:

 

I'm going all french and throwing in the towel for today.....

 

I'll pick it up monday and hopefully be able to compile all the working parts into something functional.

 

Muchas Gracias @Steve21

 

Darren

 

I've commented out all the do-y stuff lines just not to break my pc :) But like this?

 

dt2.png

 

Editted version:

 

wmicdigger2.zip

 

That how you mean?

 

Steve

  • Thanks 1
Posted

Yes! I went and had coffee and corrected my earlier retardation within a minute or two - it's working perfectly. Combined now with my access DB, I've almost completed an automated asset database... :)

Thank you for your patience and assistance.

 

Darren

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...