From: Mario - Roma on
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


From: Pegasus [MVP] on


"Mario - Roma" <mario(a)nospam.local> wrote in message
news:eQZT8kf7KHA.5412(a)TK2MSFTNGP06.phx.gbl...
> 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%")


From: Richard Mueller [MVP] on

"Pegasus [MVP]" <news(a)microsoft.com> wrote in message
news:OK7%23wyf7KHA.3880(a)TK2MSFTNGP04.phx.gbl...
>
>
> "Mario - Roma" <mario(a)nospam.local> wrote in message
> news:eQZT8kf7KHA.5412(a)TK2MSFTNGP06.phx.gbl...
>> 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
--