Windows Vista Forums

VBscript problem with Windows 7
  1. #1


    Mario - Roma Guest

    VBscript problem with Windows 7

    We use the following VBscript code to get the username of the user:

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from
    Win32_ComputerSystem")
    For Each objItem In colItems
    strNomeUtente = objItem.UserName
    If strNomeUtente = "" or IsNull (strNomeUtente) Then
    Wscript.Echo "INVALID User Name"
    Else
    Wscript.Echo "The User Name is: " & strNomeUtente
    End If
    Next

    The script works fine in Windows XP and Windows Server 2003.
    In Windows 7 many times the script fails an reports a null value.
    Is there any relevant change in the way VBscript works in windows 7?
    Is there any alternate solution to get the username in Windows 7?
    Regards
    Marius





      My System SpecsSystem Spec

  2. #2


    Pegasus [MVP] Guest

    Re: VBscript problem with Windows 7



    "Mario - Roma" <mario@newsgroup> wrote in message
    news:eQZT8kf7KHA.5412@newsgroup

    > We use the following VBscript code to get the username of the user:
    >
    > strComputer = "."
    > Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    > Set colItems = objWMIService.ExecQuery("Select * from
    > Win32_ComputerSystem")
    > For Each objItem In colItems
    > strNomeUtente = objItem.UserName
    > If strNomeUtente = "" or IsNull (strNomeUtente) Then
    > Wscript.Echo "INVALID User Name"
    > Else
    > Wscript.Echo "The User Name is: " & strNomeUtente
    > End If
    > Next
    >
    > The script works fine in Windows XP and Windows Server 2003.
    > In Windows 7 many times the script fails an reports a null value.
    > Is there any relevant change in the way VBscript works in windows 7?
    > Is there any alternate solution to get the username in Windows 7?
    > Regards
    > Marius
    The VB Script Shell gives you a much simpler solution, one that relies on
    predefined environmental variables:

    Set oWshShell = CreateObject("WScript.Shell")
    WScript.Echo oWshShell.ExpandEnvironmentStrings("The User Name is:
    %ComputerName%\%UserName%")



      My System SpecsSystem Spec

  3. #3


    Richard Mueller [MVP] Guest

    Re: VBscript problem with Windows 7


    "Pegasus [MVP]" <news@newsgroup> wrote in message
    news:OK7%23wyf7KHA.3880@newsgroup

    >
    >
    > "Mario - Roma" <mario@newsgroup> wrote in message
    > news:eQZT8kf7KHA.5412@newsgroup

    >> We use the following VBscript code to get the username of the user:
    >>
    >> strComputer = "."
    >> Set objWMIService = GetObject("winmgmts:\\" & strComputer &
    >> "\root\cimv2")
    >> Set colItems = objWMIService.ExecQuery("Select * from
    >> Win32_ComputerSystem")
    >> For Each objItem In colItems
    >> strNomeUtente = objItem.UserName
    >> If strNomeUtente = "" or IsNull (strNomeUtente) Then
    >> Wscript.Echo "INVALID User Name"
    >> Else
    >> Wscript.Echo "The User Name is: " & strNomeUtente
    >> End If
    >> Next
    >>
    >> The script works fine in Windows XP and Windows Server 2003.
    >> In Windows 7 many times the script fails an reports a null value.
    >> Is there any relevant change in the way VBscript works in windows 7?
    >> Is there any alternate solution to get the username in Windows 7?
    >> Regards
    >> Marius
    >
    > The VB Script Shell gives you a much simpler solution, one that relies on
    > predefined environmental variables:
    >
    > Set oWshShell = CreateObject("WScript.Shell")
    > WScript.Echo oWshShell.ExpandEnvironmentStrings("The User Name is:
    > %ComputerName%\%UserName%")
    >
    >
    I prefer to use the wshNetwork object. For example:

    Set objNetwork = CreateObject("Wscript.Network")
    strUser = objNetwork.UserName
    Wscript.Echo "Current user is: " & strUser

    --
    Richard Mueller
    MVP Directory Services
    Hilltop Lab - http://www.rlmueller.net
    --



      My System SpecsSystem Spec

VBscript problem with Windows 7 problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
are VBscript on Windows server 2003 and VBscript on WS2008 compatible? Francois Lafont VB Script 9 26 Apr 2010
Problem with web page created on the fly with VBScript Mark Brown VB Script 3 27 Jan 2010
Re: HTML & VBScript -> security problem!? carbooter VB Script 0 17 Oct 2009
problem download HTML in VBScript captain kris VB Script 9 04 Sep 2009
Weird problem with VBscript. Alexis Faren VB Script 2 11 Apr 2009