Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - help needed with VBScript "Replace"

Reply
 
Old 06-01-2009   #1 (permalink)
Cary Shultz


 
 

help needed with VBScript "Replace"

Good afternoon!

I have a script that enumerates all of the servers (from an input .txt file)
in a Domain and then - for each of the servers - lists out several things
(using the WIN32_OperatingSystem WMI Class). The output is a simple .txt
file. In the case where Windows Server 2008 is the Operating System we have
those nice 'misspelled' words. I am trying to use the Replace function to
'correct' this but am having a hard time. If I might ask for assistance?

Here is part of the code:
==========================================================================================

Do Until objTextFile.AtEndOfStream
strcomputer = objTextFile.Readline

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServers = objWMIService.ExecQuery ("Select * from
Win32_OperatingSystem",,48)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTS = objFSO.CreateTextFile (strOutputFIle,ForWriting,False)

For Each objServer in colServers
objTS.WriteLine "Server Name: " & objServer.CSName
objTS.WriteLine "Server OS: " & objServer.Caption
objTS.WriteLine "Server SP Level: " & objServer.CSDVersion
objTS.WriteLine "Additional Info: " & objServer.Name
objTS.WriteLine "DC or Member Server: " & objServer.ProductType
objTS.WriteLine
"......................................................................"
Next

LOOP

===========================================================================================

The part where I am having issues is with the second objTS.Write line entry
(objServer.Caption)....

With Windows Server 2008 we get "Microsoftr Windows Serverr 2008 xxxxxx". I
would like to replace "Microsoftr" with "Microsoft (R)" and "Serverr" with
"Server (R)".

I do not believe that I have too much trouble with the code for
Replace.....I just do not know where to put it!

I have the following:

strCaption = "Microsoftr Windows Serverr 2008"
If Instr(strCaption, "Microsoftr") Then
strCaption = Replace(strCaption, "Microsoftr", "Microsoft (R)")
End If
If Instr(strCaption, "Serverr") Then
strCaption = Replace(strCaption, "Serverr", "Server (R)")
End If



This is something that I found on the Internet but can actually come up with
on my own. The things that confuse me:

(A) strCaption is being used as the variable. For the code that I provided
above is that the best choice?

(B) Assuming that the "replace" code just above is correct where in the
world do I place it?

Any and all help is appreciated!

Thanks,

Cary

PS. Going to be doing the same thing with objServer.ProductType (2=Domain
Controller; 3=Member Server).


My System SpecsSystem Spec
Old 06-01-2009   #2 (permalink)
Pegasus [MVP]


 
 

Re: help needed with VBScript "Replace"


"Cary Shultz" <cshultz@xxxxxx> wrote in message
news:O78D8Lv4JHA.1416@xxxxxx
Quote:

> Good afternoon!
>
> I have a script that enumerates all of the servers (from an input .txt
> file) in a Domain and then - for each of the servers - lists out several
> things (using the WIN32_OperatingSystem WMI Class). The output is a
> simple .txt file. In the case where Windows Server 2008 is the Operating
> System we have those nice 'misspelled' words. I am trying to use the
> Replace function to 'correct' this but am having a hard time. If I might
> ask for assistance?
>
> Here is part of the code:
> ==========================================================================================
>
> Do Until objTextFile.AtEndOfStream
> strcomputer = objTextFile.Readline
>
> Set objWMIService = GetObject("winmgmts:" _
> & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
> Set colServers = objWMIService.ExecQuery ("Select * from
> Win32_OperatingSystem",,48)
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objTS = objFSO.CreateTextFile (strOutputFIle,ForWriting,False)
>
> For Each objServer in colServers
> objTS.WriteLine "Server Name: " & objServer.CSName
> objTS.WriteLine "Server OS: " & objServer.Caption
> objTS.WriteLine "Server SP Level: " & objServer.CSDVersion
> objTS.WriteLine "Additional Info: " & objServer.Name
> objTS.WriteLine "DC or Member Server: " & objServer.ProductType
> objTS.WriteLine
> "......................................................................"
> Next
>
> LOOP
>
> ===========================================================================================
>
> The part where I am having issues is with the second objTS.Write line
> entry (objServer.Caption)....
>
> With Windows Server 2008 we get "Microsoftr Windows Serverr 2008 xxxxxx".
> I would like to replace "Microsoftr" with "Microsoft (R)" and "Serverr"
> with "Server (R)".
>
> I do not believe that I have too much trouble with the code for
> Replace.....I just do not know where to put it!
>
> I have the following:
>
> strCaption = "Microsoftr Windows Serverr 2008"
> If Instr(strCaption, "Microsoftr") Then
> strCaption = Replace(strCaption, "Microsoftr", "Microsoft (R)")
> End If
> If Instr(strCaption, "Serverr") Then
> strCaption = Replace(strCaption, "Serverr", "Server (R)")
> End If
>
>
>
> This is something that I found on the Internet but can actually come up
> with on my own. The things that confuse me:
>
> (A) strCaption is being used as the variable. For the code that I
> provided above is that the best choice?
>
> (B) Assuming that the "replace" code just above is correct where in the
> world do I place it?
>
> Any and all help is appreciated!
>
> Thanks,
>
> Cary
>
> PS. Going to be doing the same thing with objServer.ProductType (2=Domain
> Controller; 3=Member Server).
How about this one-liner:
objTS.WriteLine "Server OS: " &
replace(replace(objServer.Caption,"Microsoftr", "Microsoft (R)"), "Serverr",
"Server (R)")


My System SpecsSystem Spec
Old 06-01-2009   #3 (permalink)
Cary Shultz


 
 

Re: help needed with VBScript "Replace"

I will test (but I know that this will work if you are suggesting it).

Looks like I was trying to make this more difficult than it is......I get on
my colleagues for doing that!

Thanks,

Cary

"Pegasus [MVP]" <news@xxxxxx> wrote in message
news:%233e2%23xv4JHA.5728@xxxxxx
Quote:

>
> "Cary Shultz" <cshultz@xxxxxx> wrote in message
> news:O78D8Lv4JHA.1416@xxxxxx
Quote:

>> Good afternoon!
>>
>> I have a script that enumerates all of the servers (from an input .txt
>> file) in a Domain and then - for each of the servers - lists out several
>> things (using the WIN32_OperatingSystem WMI Class). The output is a
>> simple .txt file. In the case where Windows Server 2008 is the Operating
>> System we have those nice 'misspelled' words. I am trying to use the
>> Replace function to 'correct' this but am having a hard time. If I might
>> ask for assistance?
>>
>> Here is part of the code:
>> ==========================================================================================
>>
>> Do Until objTextFile.AtEndOfStream
>> strcomputer = objTextFile.Readline
>>
>> Set objWMIService = GetObject("winmgmts:" _
>> & "{impersonationLevel=impersonate}!\\" & strComputer &
>> "\root\cimv2")
>> Set colServers = objWMIService.ExecQuery ("Select * from
>> Win32_OperatingSystem",,48)
>>
>> Set objFSO = CreateObject("Scripting.FileSystemObject")
>> Set objTS = objFSO.CreateTextFile (strOutputFIle,ForWriting,False)
>>
>> For Each objServer in colServers
>> objTS.WriteLine "Server Name: " & objServer.CSName
>> objTS.WriteLine "Server OS: " & objServer.Caption
>> objTS.WriteLine "Server SP Level: " & objServer.CSDVersion
>> objTS.WriteLine "Additional Info: " & objServer.Name
>> objTS.WriteLine "DC or Member Server: " &
>> objServer.ProductType
>> objTS.WriteLine
>> "......................................................................"
>> Next
>>
>> LOOP
>>
>> ===========================================================================================
>>
>> The part where I am having issues is with the second objTS.Write line
>> entry (objServer.Caption)....
>>
>> With Windows Server 2008 we get "Microsoftr Windows Serverr 2008 xxxxxx".
>> I would like to replace "Microsoftr" with "Microsoft (R)" and "Serverr"
>> with "Server (R)".
>>
>> I do not believe that I have too much trouble with the code for
>> Replace.....I just do not know where to put it!
>>
>> I have the following:
>>
>> strCaption = "Microsoftr Windows Serverr 2008"
>> If Instr(strCaption, "Microsoftr") Then
>> strCaption = Replace(strCaption, "Microsoftr", "Microsoft (R)")
>> End If
>> If Instr(strCaption, "Serverr") Then
>> strCaption = Replace(strCaption, "Serverr", "Server (R)")
>> End If
>>
>>
>>
>> This is something that I found on the Internet but can actually come up
>> with on my own. The things that confuse me:
>>
>> (A) strCaption is being used as the variable. For the code that I
>> provided above is that the best choice?
>>
>> (B) Assuming that the "replace" code just above is correct where in the
>> world do I place it?
>>
>> Any and all help is appreciated!
>>
>> Thanks,
>>
>> Cary
>>
>> PS. Going to be doing the same thing with objServer.ProductType
>> (2=Domain Controller; 3=Member Server).
>
> How about this one-liner:
> objTS.WriteLine "Server OS: " &
> replace(replace(objServer.Caption,"Microsoftr", "Microsoft (R)"),
> "Serverr", "Server (R)")
>
>
My System SpecsSystem Spec
Old 06-01-2009   #4 (permalink)
Cary Shultz


 
 

Re: help needed with VBScript "Replace"

Pegasus,

It looks like your one-liner works.

It also works for the objServer.ProductType where the possible outcome (in
this case) is either 2 (Domain Controller) or 3 (Member Server). I am not
sure that I understand how it works (I did not think that it would) but I
will look this up.....

Thanks!

Cary

"Pegasus [MVP]" <news@xxxxxx> wrote in message
news:%233e2%23xv4JHA.5728@xxxxxx
Quote:

>
> "Cary Shultz" <cshultz@xxxxxx> wrote in message
> news:O78D8Lv4JHA.1416@xxxxxx
Quote:

>> Good afternoon!
>>
>> I have a script that enumerates all of the servers (from an input .txt
>> file) in a Domain and then - for each of the servers - lists out several
>> things (using the WIN32_OperatingSystem WMI Class). The output is a
>> simple .txt file. In the case where Windows Server 2008 is the Operating
>> System we have those nice 'misspelled' words. I am trying to use the
>> Replace function to 'correct' this but am having a hard time. If I might
>> ask for assistance?
>>
>> Here is part of the code:
>> ==========================================================================================
>>
>> Do Until objTextFile.AtEndOfStream
>> strcomputer = objTextFile.Readline
>>
>> Set objWMIService = GetObject("winmgmts:" _
>> & "{impersonationLevel=impersonate}!\\" & strComputer &
>> "\root\cimv2")
>> Set colServers = objWMIService.ExecQuery ("Select * from
>> Win32_OperatingSystem",,48)
>>
>> Set objFSO = CreateObject("Scripting.FileSystemObject")
>> Set objTS = objFSO.CreateTextFile (strOutputFIle,ForWriting,False)
>>
>> For Each objServer in colServers
>> objTS.WriteLine "Server Name: " & objServer.CSName
>> objTS.WriteLine "Server OS: " & objServer.Caption
>> objTS.WriteLine "Server SP Level: " & objServer.CSDVersion
>> objTS.WriteLine "Additional Info: " & objServer.Name
>> objTS.WriteLine "DC or Member Server: " &
>> objServer.ProductType
>> objTS.WriteLine
>> "......................................................................"
>> Next
>>
>> LOOP
>>
>> ===========================================================================================
>>
>> The part where I am having issues is with the second objTS.Write line
>> entry (objServer.Caption)....
>>
>> With Windows Server 2008 we get "Microsoftr Windows Serverr 2008 xxxxxx".
>> I would like to replace "Microsoftr" with "Microsoft (R)" and "Serverr"
>> with "Server (R)".
>>
>> I do not believe that I have too much trouble with the code for
>> Replace.....I just do not know where to put it!
>>
>> I have the following:
>>
>> strCaption = "Microsoftr Windows Serverr 2008"
>> If Instr(strCaption, "Microsoftr") Then
>> strCaption = Replace(strCaption, "Microsoftr", "Microsoft (R)")
>> End If
>> If Instr(strCaption, "Serverr") Then
>> strCaption = Replace(strCaption, "Serverr", "Server (R)")
>> End If
>>
>>
>>
>> This is something that I found on the Internet but can actually come up
>> with on my own. The things that confuse me:
>>
>> (A) strCaption is being used as the variable. For the code that I
>> provided above is that the best choice?
>>
>> (B) Assuming that the "replace" code just above is correct where in the
>> world do I place it?
>>
>> Any and all help is appreciated!
>>
>> Thanks,
>>
>> Cary
>>
>> PS. Going to be doing the same thing with objServer.ProductType
>> (2=Domain Controller; 3=Member Server).
>
> How about this one-liner:
> objTS.WriteLine "Server OS: " &
> replace(replace(objServer.Caption,"Microsoftr", "Microsoft (R)"),
> "Serverr", "Server (R)")
>
>
My System SpecsSystem Spec
Old 06-01-2009   #5 (permalink)
Pegasus [MVP]


 
 

Re: help needed with VBScript "Replace"


"Cary Shultz" <cshultz@xxxxxx> wrote in message
news:uZCmhSw4JHA.4116@xxxxxx
Quote:

> Pegasus,
>
> It looks like your one-liner works.
>
> It also works for the objServer.ProductType where the possible outcome (in
> this case) is either 2 (Domain Controller) or 3 (Member Server). I am not
> sure that I understand how it works (I did not think that it would) but I
> will look this up.....
>
> Thanks!
>
> Cary
It's very simple to understand. Let's assume that we have a string variable
called sName. Let's set this variable is currently set to "Shultz". Here we
go:

sName1 = "Shultz"
wscript.echo sName1 [writes Shultz to the screen]
wscript.echo replace(sName1, "Shultz", "Schultz") [writes Schultz to the
screen]
sName2 = replace(sName, "Shultz", "Schultz") [sets sName2 to Schultz]
wscript.echo sName2 [writes Schultz to the screen]
wscript.echo replace(sName2, "Schultz", "Cary Schults") [writes Cary Schults
to the screen]

I could nest the two "replace" functions like so:
wscript.echo replace(replace(sName1, "Shultz", "Shultz"), "Schultz", "Cary
Schults")

I recommend you play around with "replace" to get a feel for the technique.


My System SpecsSystem Spec
Old 06-01-2009   #6 (permalink)
Cary Shultz


 
 

Re: help needed with VBScript "Replace"

Pegasus,

Will start playing with.....start small (simple) and work my way up. Thanks
for the example.

I mean, I get the basics of this. It was the nesting that confused me. I
will play.

Thanks again,

Cary

"Pegasus [MVP]" <news@xxxxxx> wrote in message
news:us%23kzsw4JHA.5048@xxxxxx
Quote:

>
> "Cary Shultz" <cshultz@xxxxxx> wrote in message
> news:uZCmhSw4JHA.4116@xxxxxx
Quote:

>> Pegasus,
>>
>> It looks like your one-liner works.
>>
>> It also works for the objServer.ProductType where the possible outcome
>> (in this case) is either 2 (Domain Controller) or 3 (Member Server). I
>> am not sure that I understand how it works (I did not think that it
>> would) but I will look this up.....
>>
>> Thanks!
>>
>> Cary
>
> It's very simple to understand. Let's assume that we have a string
> variable called sName. Let's set this variable is currently set to
> "Shultz". Here we go:
>
> sName1 = "Shultz"
> wscript.echo sName1 [writes Shultz to the screen]
> wscript.echo replace(sName1, "Shultz", "Schultz") [writes Schultz to the
> screen]
> sName2 = replace(sName, "Shultz", "Schultz") [sets sName2 to Schultz]
> wscript.echo sName2 [writes Schultz to the screen]
> wscript.echo replace(sName2, "Schultz", "Cary Schults") [writes Cary
> Schults to the screen]
>
> I could nest the two "replace" functions like so:
> wscript.echo replace(replace(sName1, "Shultz", "Shultz"), "Schultz", "Cary
> Schults")
>
> I recommend you play around with "replace" to get a feel for the
> technique.
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
A "global" FIND & REPLACE on the Registry? Vista General
How do I replace all characters in the "Now" date? VB Script
Turn off "replace wireless mouse batter" alert Vista performance & maintenance
Use of matching subexpressions in "-replace" operator PowerShell
"People came back and said, 'Please, will you take this off and replace it with XP?'" Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46