+ Post New Thread
Results 1 to 8 of 8
Scripts Thread, Help!!! in Coding and Web Development; I need to run (correctly) a loop in a script..... Here's the background: I have a WMIC script that pulls ...
  1. #1
    DT2
    DT2 is offline
    DT2's Avatar
    Join Date
    May 2011
    Location
    Lakeville, Carleton County, New Brunswick
    Posts
    694
    Thank Post
    140
    Thanked 198 Times in 139 Posts
    Rep Power
    73

    Help!!!

    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%_Documentatio n.csv GOTO :run
    else GOTO :write

    :write
    >>%SystemDrive%\WMICDigger\%Compname%_Documentatio n.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.... 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
    Last edited by DT2; 22nd February 2013 at 03:31 PM.

  2. #2

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,656
    Thank Post
    324
    Thanked 506 Times in 474 Posts
    Rep Power
    177
    To be honest I didn't get your question, but do you mean like this?

    Code:
    @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

  3. Thanks to Steve21 from:

    DT2 (22nd February 2013)

  4. #3
    DT2
    DT2 is offline
    DT2's Avatar
    Join Date
    May 2011
    Location
    Lakeville, Carleton County, New Brunswick
    Posts
    694
    Thank Post
    140
    Thanked 198 Times in 139 Posts
    Rep Power
    73
    Quote Originally Posted by Steve21 View Post
    To be honest I didn't get your question, but do you mean like this?

    Code:
    @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

  5. #4
    DT2
    DT2 is offline
    DT2's Avatar
    Join Date
    May 2011
    Location
    Lakeville, Carleton County, New Brunswick
    Posts
    694
    Thank Post
    140
    Thanked 198 Times in 139 Posts
    Rep Power
    73
    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

  6. #5

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,656
    Thank Post
    324
    Thanked 506 Times in 474 Posts
    Rep Power
    177
    Quote Originally Posted by DT2 View Post
    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
    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

  7. Thanks to Steve21 from:

    DT2 (22nd February 2013)

  8. #6
    DT2
    DT2 is offline
    DT2's Avatar
    Join Date
    May 2011
    Location
    Lakeville, Carleton County, New Brunswick
    Posts
    694
    Thank Post
    140
    Thanked 198 Times in 139 Posts
    Rep Power
    73
    So a moment of clarity, and I realise I'm monging it....



    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

  9. #7

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,656
    Thank Post
    324
    Thanked 506 Times in 474 Posts
    Rep Power
    177
    Quote Originally Posted by DT2 View Post
    So a moment of clarity, and I realise I'm monging it....



    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

  10. Thanks to Steve21 from:

    DT2 (22nd February 2013)

  11. #8
    DT2
    DT2 is offline
    DT2's Avatar
    Join Date
    May 2011
    Location
    Lakeville, Carleton County, New Brunswick
    Posts
    694
    Thank Post
    140
    Thanked 198 Times in 139 Posts
    Rep Power
    73
    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

SHARE:
+ Post New Thread

Similar Threads

  1. Help me find a driver for Ghost Cast Boot Disk
    By ninjabeaver in forum Hardware
    Replies: 6
    Last Post: 26th September 2005, 10:50 AM
  2. making passwords help
    By browolf in forum Scripts
    Replies: 7
    Last Post: 26th August 2005, 09:49 AM
  3. Exchange Go BANG ***HELP***
    By ICTNUT in forum Windows
    Replies: 9
    Last Post: 23rd August 2005, 11:33 AM
  4. Free Help!
    By mark in forum How do you do....it?
    Replies: 16
    Last Post: 27th July 2005, 03:50 PM
  5. EZ_GPO usage and helpfulness query
    By tarquel in forum Windows
    Replies: 6
    Last Post: 21st June 2005, 02:00 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •