Use if FOR loops

Home Forums Scripting DOS Command Shell Use if FOR loops

This topic contains 4 replies, has 3 voices, and was last updated by  ljgodzilla 11 months, 3 weeks ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts

  • LIC
    Member
    #167358

    I have avoided using For loops, as I have a very hard time understanding the mechanics.

    Two part question.
    1. I would like to run a wmic cmd agsinst a list of computers.

    for /f %a in (c:pc.txt) do (wmic bios get serial number)

    the output I get = the number of items in the c:pc.txt file, however the wmic output is from the local pc, not the computers in the txt.

    i have read many sites/ examples, but none help solve the issue.

    Please keep your answers as basic as possible- using one liners.
    Please offer assistance with DOS (do not wish to confuse myself further)

    2. Can you provide a good site that does NOT get so esoteric
    in the usage?

    thanks in advance


    Anonymous
    #372078

    Is your wmic cmd line including the call to the specific PC name in question? If not, it will always call the local PC the script runs on, which is exactly what you’re getting. Reading soem on-line articles, you should include the ‘/node’ parameter in the wmic cmd to call out the specific remote PC name. Here at Petri there is an article you may not have seen which calls this out, have a read here:
    https://www.petri.com/command-line-wmi-part-2

    Even in Powershell when you use a ForEach loop, you specify a variable which includes one object out of the array of all objects you want to test. Inside that loop, you call out that variable name for any command you want actioned against that object. The same is true with WMIC, whether you call it using Powershell or by using a DOS cmd line script. Reading thru the referenced article, it appears it’s even simpler than I thought: read thru the section titled ‘Querying Remote Computers’. Pay particular attention to the last code example before the next section ‘Alternate Credentials’–it refers to using the wmic cmd itself to run commands against a list of computers from a text file, without having to put it into a loop. It appears WMIC can do the ‘loop’ thing all by itself.

    Let us know how you get on.


    ljgodzilla
    Member
    #391259

    Thank you– I caught the “node” shortly after- regardless appreciate.

    (for /f %a in (c:pc.txt) do [USER=”68995″]Echo[/USER] PC_Name: \%a && wmic /node:”%a” nicconfig where “ipenabled=’true'” get macaddress && wmic /node:”%a” nicconfig where “ipenabled=’true'” get IpAddress |more >> out.txt)

    This works great with exceptions

    1. It only outputs the final wmic result (the IP) – so now my goal is to work through that.

    2. As far as scripts go- I am most familiar in using on-liners.
    Would be great to format this in a more typical scripting pattern

    Thoughts?

    Thanks again!!


    ljgodzilla
    Member
    #391260

    This is much cleaner– but probably “old school” :)

    If anyone has formatting suggestions, I will take them.

    (for /f %%a in (pc.txt)do [USER=”68995″]Echo[/USER] Node: \%%a >>^
    C:log.txt ^
    && wmic /node:”%%a” nicconfig where “ipenabled=’true'” get macaddress |more >>^
    C:log.txt ^
    && wmic /node:”%%a” nicconfig where “ipenabled=’true'” get IpAddress |more >> ^
    C:log.txt ^

    )

    I have been working with the various output options. .csv and txt are a good choice for my use.

    The hitch to the output is that WMIC utilizes Unicode (causing undesirable spacing)

    Pipe to | more is supposed to take care of it… but it does not.

    Suggestions?


    Anonymous
    #372080

    Glad to see you are at least accessing the entire PC list now using ‘/node:’, but that’s at the limit of what I already know. Like so many, I tend to use Powershell for scripting, but haven’t played that much with WMIC, in part because I’d read somewhere about a year ago that it’s only MS’s version of the CIM model, and the industry is not using WMIC as much anymore. Have a read at this link:

    https://blogs.technet.microsoft.com/heyscriptingguy/2016/02/08/should-i-use-cim-or-wmi-with-windows-powershell/

    By the way, this link may help you with your spacing issue. Found it with, you guessed it, :google:, searched for ‘wmic output unicode extra spacing’:

    https://stackoverflow.com/questions/28672210/get-rid-of-spaces-and-tabs-in-wmic-output

    Good luck.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.