Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBSCRIPT and WMI for serial or Batchfile? 1

Status
Not open for further replies.

scanjam

Technical User
Oct 30, 2003
244
GB
I have a this batchfile that is working but running to many times.

for /f "skip=1%%1 in ('xxxx csproduct get identifyingnumber') do \\server\postimage$\dnscmd.exe DNSSERVER /RecordAdd test1.com ixx%computername% CNAME ixx%%1.test1.com

Effectivly this just needs to extract the servers serial number as a variable then use it in a DNSCMD.exe to create an alias in DNS..

THe for loop runs twice 1 with the correct variable - secondly with a blank line - which then overwrites the correct first run sequence...

Its driving me nuts - can this easily be done in Scripting host? or....

Hope someone can help!
Cheers
James
 
Have you tried putting all of the serial numbers into a file and reading it in from there? See example below:

for /f %%1 in ('type serialNumbers.txt')
 
wmic bios get serialnumber > c:\serial.txt

returns

SerialNumber
CZJ12xxxxx

I have to skip the first line.

But the read of this file isn't working....

Its just coming back blank...

i'm using


for /f "skip=1" %1 in (c:\serial.txt) do "\\server\c$\dnscmd.exe server2 /RecordAdd test.xx.xxx.xxx.co.uk ixx%computername% CNAME ixx%1.test.xx.xxx.xxx.co.uk"

Its just coming back blank..

James
 
>for /f "skip=1" %1 in (c:\serial.txt) do "\\server\c$\dnscmd.exe server2 /RecordAdd test.xx.xxx.xxx.co.uk ixx%computername% CNAME ixx%1.test.xx.xxx.xxx.co.uk"

[1] If that's inside the batch file, it is %%1 like the first post?!

[2] Do not quote the do command part like that which includes the arguments within - that would be incorrect.

[tt]for /f "skip=1" %%1 in (c:\serial.txt) do \\server\c$\dnscmd.exe server2 /RecordAdd test.xx.xx.xxx.co.uk ixx%computername% CNAME ixx%%1.test.xx.xxx.xxx.co.uk[/tt]
 
the entire thing works perfectly if i cut and paste the serial number from the command 'wmic... etc'

TO a test file, but not if i export the output of the command to a file first adn then run the dnscmd from the file.


The wmic bios get serialnumber does 2 things.

It outputs the frist line as the heading, then seems to export another blank line after the command which the for/f is interpreting as another loop hence, when automated, writes directly over the top of the first run, and relpaces it with the second regardless of whether its written to a file or outputted direct....

I even tried filtering the command to EXCLUDE serial with

cmd.exe /c wmic get bios serialnumber | find /v "serial" > c:\serial.txt

- but thats still outputting the entire result even though the find feature only lists the serial number....

GRRRRRRRRRRRrrrrrrrrrrrrr - i cant believe this could be so painful...

is there a way where i can read this first txt file with VB and THEN run the command from the?

ie. open serial.txt - read line 2 only -
run command \\server$\dnscmd.exe bbcnsn001 /RecordAdd london.com ilo%computername% CNAME ilo%%1.london.com


....
thanks again for your replies.. :)
 
[1] You don't keep the continuity of messages. Nobody knows what has been corrected and what is not. You cmd batch was wrong, has it been correct? If you have profitted from the correction advised, acknowlege it so that people know what has changed. For now, it looks like you are monologing.

[2] Then you another command to prepare the file does not look right. This may look more reasonable.
[tt] wmic bios get serialnumberp[/tt]
Before "get", nothing in yours?!

[3] And then, the batch file. Blank lines are automatically skipped. Make sure you "interpretation" is not out of pure imagination but supported by your reading of for /f documentation.
 
[4] If you find it hard to take my criticism - that does not surprise me, I am veteran in this forum and know how people react, I can show you an example.
[tt]
for /f "usebackq skip=1" %%v in(`wmic process name`) echo %%v
[/tt]
[4.1] It should be appeared in a batch file. If it is directly entered at command prompt, replace %%v by %v.
 
amendment
[4.2] There is a typo in the corresponding line in [4]. It should read like this.
[tt] for /f "usebackq skip=1" %%v in(`wmic process name`) [red]do[/red] echo %%v
[/tt]
 
no offence taken - your exactly correct about my posts being inaccurate and misleading.

Im just trying the last command you posted now - ... :)
Thanks again!
 
Without running it as a batch file (ie just cutting and pasting manually)

here is the Command im using: (its wrapped the text)

for /f "usebackq skip=1" %v in (`wmic bios get serialnumber`) do \\server\c$\dnscmd.exe dnsserver /RecordAdd london.com ilo%computername% CNAME ilo%v.london.com

(im aware that if i run as a batch file it must have %% - thanks for the tip!)

The result is - it runs twice - regardless if its done manually or as a batch file

Heres the output from CMD.exe

C:\Documents and Settings\Administrator>for /f "usebackq skip=1" %v in (`wmic bi
os get serialnumber`) do \\server\postimage$\dnscmd.exe dnsserver /RecordAdd
london.com ilo%computername% CNAME ilo%v.london.com

C:\Documents and Settings\Administrator>\\server\postimage$\dnscmd.exe dnsserver /RecordAdd london.com iloMYSERVER CNAME iloCZJ847074R.london.com
Add CNAME Record for iloMYSERVER.london.com at london.com
Command completed successfully.

C:\Documents and Settings\Administrator>\\server\postimage$\dnscmd.exe server
.london.com iloMYSERVER CNAME ilo Add CNAME Record for iloMYSERVER.london.com at london.com
Command completed successfully.

C:\Documents and Settings\Administrator>


With the final entry in DNS as

iloMYSERVER ALIAS (CNAME) ilo.london.com


wmic is passing a second blank line to the command and erasing the first 'FOR' loop.

Sigh - thanks again for your help but i really think i have to look for some other alternative method - possibly VBSCRIPT to get 'just the serial number' and then pass that to the dnscmd as a variable instead.... (though i have no idea how... :)

Cheers
 
actually never mind - i managed to write this in VBSCRIPT


heres the final working solution - so much for easy command prompt... ha


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colBIOS = objWMIService.ExecQuery _
("Select * from Win32_BIOS")
For each objBIOS in colBIOS
strSerial = objBIOS.SerialNumber
Next

Set WshNetwork=WScript.CreateObject("WScript.Network")
'WScript.Echo "Your Computer Name is = " & WshNetwork.ComputerName
'set the comptuername variable
COMPUTERNAME=WshNetwork.ComputerName
'WScript.Echo computername


set oShell=createobject("wscript.shell")
oshell.run "c:\local\dnscmd.exe DNSSERVERNAME /RecordAdd DNSZONE ilo"& COMPUTERNAME &" CNAME ilo"& TRIM(strSerial) & ".DNSZONE"
set oShell=nothing


This definitely works and TRIM was requried as the serial number was coming out as CZJ1234566 + about 7 spaces - of course killing the command....


Thanks again for all your help and advice.
 
[5] That it can be accomplished via vbscript, I have no doubt and it goes as a matter of course. (All the same, I am glad you make it that way.)

[6] But, I actually do not agree on the statements on for /f ... as it leaves the impression there is something wrong there with the command line approach. And I fear the general readers could be misled.

[6.1] If you put this line in a batch, I bet you will see the output redirected file (arbitrarily named, say serialnumber_out.txt) showing only one "output:xxx". If the blank line at the end of wmic stream is process, but not skipped automatically (and by default), you would see two lines with the second like "output:"! I put some prefix "output:" so that if the blank line were in fact processed (not skipped), the output will show non empty message.

[tt] for /f "usebackq skip=1" %%v in (`wmic bios get serialnumber`) do echo output:%%v > serialnumber_out.txt[/tt]

[6.2] As to what happens to your observation, I cannot say for sure. Many things can go wrong elsewhere or nowhere!
 
Thanks for the messages!
Batchfiles might work - but I gave up in the end - the vbscript attached above will do exactly what i was trying to do with batchfiles for the past 3 days...

Thanks for the messages and help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top