Prefixing output text in batch

0

I am trying to create a remote, WMI query script that will query a bunch of machines and tell me the MemoryChip configuration information. This way, we can find out what machines physically have 1 chip or 2 chips and how big each one is and find out who has a 1x4GB configuration and get them upgraded. The base I am running is: wmic /node:"[ComputerName]" MEMORYCHIP get BankLabel,DeviceLocator,Capacity,Tag > memory.txt

The problem is that this just kicks out the following, which provides no ability to reference the computer being queried: BankLabel Capacity DeviceLocator Tag
4294967296 DIMM A Physical Memory 0
4294967296 DIMM B Physical Memory 1

You run this 50x, and there's not a quick way to find out which machine has what. What I am trying to do is have the script Echo the "ComputerName" to the line preceding the text or append it at the next line. All of my efforts to do this have not produced viable results, so I am hoping at least one person who still uses batch (for things, quite frankly, I should learn powershell to execute) can help me with this.

SplatteredShrapnel

Posted 2015-06-12T15:53:05.120

Reputation: 43

What are you passing to /node - a list of machine IDs or a single ID? – Karan – 2015-06-12T16:03:42.937

Right now, it is 1 machine per line. So each time we query a new machine, we start a new WMIC line.

TL;DR - One machine per /Node switch – SplatteredShrapnel – 2015-06-12T16:53:50.597

Ok, so why not echo each machine ID to the text file before redirecting WMIC's output to the same file? – Karan – 2015-06-12T16:57:23.013

@Karan I tried that. echo ... > file followed by wmic ... >> file sticks a load of NULs in file. Example "[ComputerName] ÿþB a n k L a b e l ", etc where the spaces are NULs. Hence my answer using wmic twice. – DavidPostill – 2015-06-12T17:03:01.977

echo and wmic together redirecting to the same file seems to be a no no. Something to do with character sets maybe? – DavidPostill – 2015-06-12T17:04:36.933

echo output is "ASCII text, with CRLF line terminators" and wmic output is "Little-endian UTF-16 Unicode text, with CRLF line terminators" – DavidPostill – 2015-06-12T17:23:29.917

Answers

2

What I am trying to do is have the script echo the "ComputerName"

Try the following:

wmic /node:"[ComputerName]" OS get csname > memory.txt
wmic /node:"[ComputerName]" MEMORYCHIP get BankLabel,DeviceLocator,Capacity,Tag >> memory.txt

Output here (not including the /node option as I can't test it):

CSName
HAL
BankLabel  Capacity    DeviceLocator  Tag
Bank 0     4294967296  SODIMM1        Physical Memory 0

Where HAL is my computer name.

DavidPostill

Posted 2015-06-12T15:53:05.120

Reputation: 118 938

1the "csname" works at getting the current machine's computer name, but I'm using the "/node:" switch to do a remote computer. So basically, if I enter the "wmic OS get csname" above each line, it gives the same output each time it is run. Perhaps if I use the "/node" with the "WMIC OS" I'll get the remote machine as well... – SplatteredShrapnel – 2015-06-12T17:25:25.580

Did the remote csname work as expected? If so I will update my answer. – DavidPostill – 2015-06-12T17:34:46.247

So typing the following produces the closest to my desired result as I expect to get from batch:

wmic /node:"[ComputerName]" OS get csname > memory.txt inline code in backticks wmic /node:"ComputerName" MemoryChip get BankLabel, Capacity, MemoryType, TypeDetail, Speed >> memory.txt – SplatteredShrapnel – 2015-06-12T17:34:48.560

1

Just use something like this:

@echo off
echo "ComputerName" >> Memory.txt
wmic /append:Memory.txt /node:"ComputerName" MemoryChip get BankLabel,DeviceLocator,Capacity,Tag

Karan

Posted 2015-06-12T15:53:05.120

Reputation: 51 857

Sneaky way to get around the echo and wmic redirection problem. I will remember this one ;) – DavidPostill – 2015-06-12T17:31:50.267