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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change CSCRIPT background and foreground colours 3

Status
Not open for further replies.

JPJeffery

Technical User
May 26, 2006
600
GB
In Batch files I could do
Code:
color 9f
to get white text on a bright blue background.

Is there an equivalent when running a .vbs with cscript?

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
the following example will open a comand prompt, set the colors you specify and perform a DIR command.

Code:
Set WshShell = CreateObject("Wscript.Shell")
WshShell.Run ("CMD.EXE /K color 9f & dir")

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
But markdmac that's a mere workaround! ;-)

Still, I could edit my 'RunFromCScriptDOSBox' sub to use the syntax you've suggest above.

But does this mean there's no 'native' way to achieve the same result?

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
In most cases you don't even see a box, unless you are working with CScript as your default engine. And even then you are looking at a command prompt which you can by default change the look of permanently.

VBScript is designed for function and not to look pretty. Under the majority of circumstances the only one looking at it is an admin and not an end user.

You might want to look at PowerShell since that has a colored background you can change permanently. PowerShell can run your VBScripts as well.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
I had a play around and came up with this (some code pasted, from msdn):
Code:
ChangeCScriptWindowColours
CheckEngine ' to force the script to run in a 'DOS' box

'---------------------------------------------------------
Sub ChangeCScriptWindowColours
  Const HKEY_CURRENT_USER 	= &H80000001

  strComputer = "."
  Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\"&_
      strComputer & "\root\default:StdRegProv")

  strKeyPathShort = "Console\%SystemRoot%_system32_CSCRIPT.EXE"
  strValueName = "ScreenColors"
  strValue = "31" ' Bright white text on a dark blue background
  Return = objReg.SetDWordValue &_
     (HKEY_CURRENT_USER,strKeyPathShort,strValueName,strValue)
End Sub
'---------------------------------------------------------
Sub CheckEngine
    pcEngine = LCase(Mid(WScript.FullName, InstrRev(WScript.FullName,"\")+1))
    If Not pcEngine="cscript.exe" Then
        'wscript.echo " Engine = " & pcengine
        Set objWshShell = CreateObject("WScript.Shell")
        objWshShell.Run "CSCRIPT.EXE /nologo """ & WScript.ScriptFullName & """"
        WScript.Quit
    End If
End Sub

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
>objWshShell.Run "CSCRIPT.EXE /nologo """ & WScript.ScriptFullName & """"
[tt]objWshShell.Run "CSCRIPT.EXE [highlight]//[/highlight]nologo """ & WScript.ScriptFullName & """"[/tt]
 
It seems to work without the single / instead of //...

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
Well, I'm not getting an error which would stop it working at all. Since it's a string literal surely it doesn't need a preceding '/'?

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
:)

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
Well, it works with // and it works with / but I'm going to go with // as per your suggestion because it just feels right...

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
Type cscript /? for full syntax of cscript. You will find that // is correct.
 
Good point. Well made.

And yet it works with just /.

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
There is story on why they design this wscript specific switch with double slash. It is that the vbs itself can be fed with arguments of its own. If you stick /nologo immediately following cscript.exe, by all means do it as you like. You can attach wscript switch anywhere you like with double-slash and that is the design story behind it.
 
Ah! I see.
Code:
cscript logonscript.vbs //nologo
is equivalent to
Code:
cscript /nologo logonscript.vbs
Nice.

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
So, in response to my original question, here's how to change the cscript 'DOS' box colours (this assumes the current user has rights to edit the registry):
Code:
' Registry entries for forcing the colours in the cscript box:
Const HKEY_CLASSES_ROOT   = &H80000000
Const HKEY_CURRENT_USER   = &H80000001
Const HKEY_LOCAL_MACHINE  = &H80000002
Const HKEY_USERS          = &H80000003
Const HKEY_CURRENT_CONFIG = &H80000005

'Create and populate a dictionary object containing the registry settings
'required to create a cscript 'DOS' box of bright white text on a blue background.
Set RegValues = CreateObject("Scripting.Dictionary")
    RegValues.Add "ScreenBufferSize", "65470584"
    RegValues.Add "FontSize", "786440"
    RegValues.Add "FontFamily", "48"
    RegValues.Add "FontWeight", "400"
    RegValues.Add "HistoryNoDup", "0"
    RegValues.Add "ScreenColors", "31"

ChangeCScriptWindowColours

Sub ChangeCScriptWindowColours
    strComputer = "."
    Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\"&_
        strComputer & "\root\default:StdRegProv")

    strKeyPathShort = "Console\%SystemRoot%_system32_CSCRIPT.EXE"

    ' Create key to use
    Return = objReg.CreateKey(HKEY_CURRENT_USER, strKeyPathShort)

    If (Return = 0) And (Err.Number = 0) Then
        'Wscript.Echo "  HKEY_CURRENT_USER\" & strKeyPathShort & " created"
    Else
        Wscript.Echo "  CreateKey failed. Error = '" & Err.Number & "' and Return = '" & Return & "'"
    end if

    Err.Clear
    Return = ""

    RegValueKeys = RegValues.keys 'Get the keys
    RegValueItems = RegValues.Items 'Get the keys

    'For Each value1 In RegValueKeys
    For i = 0 To RegValues.Count -1
        strValueName = RegValueKeys(i) 'Print key
        strValue = RegValueItems(i) 'Print item
        Return = AddCScriptColourValues(strValueName,strValue)
    Next

End Sub
'---------------------------------------------------------
Function AddCScriptColourValues(strValueName,strValue)
    Return = objReg.SetDWordValue(HKEY_CURRENT_USER,strKeyPathShort,strValueName,strValue)
    If (Return = 0) And (Err.Number = 0) Then
        'Wscript.Echo "  HKEY_CURRENT_USER\" & strKeyPathShort & "\" & strValueName &_
                      "\" & strValue & " created"
    Else
        Wscript.Echo "  SetDWordValue failed. Error = '" & Err.Number & "' and Return = '" & Return & "'"
    end if
End Function

Now, if I could just get the voice right, I mean, if I could just work out how to change the title of the box as well...

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
That doesn't do what the [tt]color[/tt] command does either. It just changes the default colors for any command prompt opened in the future with the same window title.
 
Well, for any CSCRIPT window opened in the future rather than a command prompt.

Still, code could be added to the .vbs to remove the entries afterwards to leave future instances with the default colours. Or other scripts could have the same code but with different values to produce different colours.

It's not nearly as dynamic as the [tt]color[/tt] command which could change the colours on the fly (whereas with this, you're stuck with the colours specified) and of course the change won't take effect immediately for the same reason.

In our case, this is for our logon script, which is started by the wscript.exe, makes the registry changes, then checks to see if it's being run by cscript. If it isn't, it calls 'itself' with the cscript executable and then closes leaving the cscript instanmce of the logon script running in a blue box with white text.

It does the job.

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
Why don't you make the registry changes via GPO instead of login script?

Just install the free AD GPO add in that allows you to set registry settings:


Note that Microsoft recently purchased Desktop Standard, so this is a trusted source.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top