Announcement

Collapse
No announcement yet.

Use if FOR loops

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Use if FOR loops

    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





  • #2
    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.
    *RicklesP*
    MSCA (2003/XP), Security+, CCNA

    ** Remember: credit where credit is due, and reputation points as appropriate **

    Comment


    • #3
      Thank you-- I caught the "node" shortly after- regardless appreciate.


      (for /f %a in (c:\pc.txt) do Echo 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!!



      Comment


      • #4
        This is much cleaner-- but probably "old school"

        If anyone has formatting suggestions, I will take them.


        (for /f %%a in (pc.txt)do Echo 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?





        Comment


        • #5
          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/...ws-powershell/

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

          https://stackoverflow.com/questions/...in-wmic-output

          Good luck.
          *RicklesP*
          MSCA (2003/XP), Security+, CCNA

          ** Remember: credit where credit is due, and reputation points as appropriate **

          Comment

          Working...
          X