Link to home
Start Free TrialLog in
Avatar of Edward Crist
Edward CristFlag for United States of America

asked on

SCRIPT PROBLEM WITH VBS

So I found a vbs script to write information into the computer description field upon login

Here's the code and my error shows below the code.

Not sure how to fix this, especially since I'm a scripting novice

Option Explicit
Const ADS_PROPERTY_UPDATE = 2 
Dim objSysInfo, objUser, objNetwork, strComputer, strDescription

Set objSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" "&" objSysInfo.UserName)

Set objNetwork = CreateObject("Wscript.Network")
strComputer = objNetwork.ComputerName

strDescription = "Logged on to " "&" strComputer "&" " at " "&" Date "&" " " "&" Time

objUser.Put "description", strDescription
objUser.SetInfo

Open in new window


I want to run this as a PS1 file but I get the following errors

At C:\Users\ed.crist\Desktop\loguser.ps1:6 char:35
+ Set objUser = GetObject("LDAP://" "&" objSysInfo.UserName)
+                                   ~~~
Unexpected token '"&"' in expression or statement.
At C:\Users\ed.crist\Desktop\loguser.ps1:6 char:34
+ Set objUser = GetObject("LDAP://" "&" objSysInfo.UserName)
+                                  ~
Missing closing ')' in expression.
At C:\Users\ed.crist\Desktop\loguser.ps1:6 char:58
+ Set objUser = GetObject("LDAP://" "&" objSysInfo.UserName)
+                                                          ~
Unexpected token ')' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken
Avatar of Norie
Norie

Try this.
Const ADS_PROPERTY_UPDATE = 2
Dim objSysInfo, objUser, objNetwork, strComputer, strDescription

Set objSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objSysInfo.UserName)

Set objNetwork = CreateObject("Wscript.Network")
strComputer = objNetwork.ComputerName

strDescription = "Logged on to " & strComputer & "  at  " & Date & " " & Time

objUser.Put "description", strDescription
objUser.SetIn

Open in new window

Avatar of Edward Crist

ASKER

I get this....

At C:\Users\ed.crist\Desktop\descript.ps1:5 char:35
+ Set objUser = GetObject("LDAP://" & objSysInfo.UserName)
+                                   ~
Unexpected token '&' in expression or statement.
At C:\Users\ed.crist\Desktop\descript.ps1:5 char:34
+ Set objUser = GetObject("LDAP://" & objSysInfo.UserName)
+                                  ~
Missing closing ')' in expression.
At C:\Users\ed.crist\Desktop\descript.ps1:5 char:35
+ Set objUser = GetObject("LDAP://" & objSysInfo.UserName)
+                                   ~
The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double quotation marks ("&") to pass it as part of a string.
At C:\Users\ed.crist\Desktop\descript.ps1:5 char:56
+ Set objUser = GetObject("LDAP://" & objSysInfo.UserName)
+                                                        ~
Unexpected token ')' in expression or statement.
At C:\Users\ed.crist\Desktop\descript.ps1:10 char:34
+ strDescription = "Logged on to " & strComputer & "  at  " & Date & "  ...
+                                  ~
The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double quotation marks ("&") to pass it as part of a string.
At C:\Users\ed.crist\Desktop\descript.ps1:10 char:48
+ strDescription = "Logged on to " & strComputer & "  at  " & Date & "  ...
+                                                ~
The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double quotation marks ("&") to pass it as part of a string.
At C:\Users\ed.crist\Desktop\descript.ps1:10 char:59
+ strDescription = "Logged on to " & strComputer & "  at  " & Date & "  ...
+                                                           ~
The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double quotation marks ("&") to pass it as part of a string.
At C:\Users\ed.crist\Desktop\descript.ps1:10 char:66
+ ... scription = "Logged on to " & strComputer & "  at  " & Date & " " & T ...
+                                                                 ~
The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double quotation marks ("&") to pass it as part of a string.
At C:\Users\ed.crist\Desktop\descript.ps1:10 char:72
+ ... iption = "Logged on to " & strComputer & "  at  " & Date & " " & Time
+                                                                    ~
The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double quotation marks ("&") to pass it as part of a string.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken
Note that your script writes into the Description attribute of the User object, not the computer object.
That said, vbs is not even closely related to Powershell, so any attempt at renaming a vbs file to .ps1 will be futile.
If you want to run this exact script from a Powershell prompt, you have to do the same as in an old-fashioned command prompt:
cscript.exe C:\Temp\Whatever.vbs

Open in new window

If you want to translate the script into native Powershell:
$ADSystemInfo = New-Object -ComObject 'ADSystemInfo'
$UserCN = $ADSystemInfo.GetType().InvokeMember('Username', 'GetProperty', $Null, $ADSystemInfo, $Null)
$ADUser = [ADSI]"LDAP://$($UserCN)"
$ADUser.Description = "Logged on to $($ENV:ComputerName) at $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')"
[void]$ADUser.SetInfo()
[Runtime.InteropServices.Marshal]::FinalReleaseComObject($ADSystemInfo)

Open in new window

Hmmm.....thanks for the PS script....when I run it, I get no errors, but nothing is showing in the description field in AD either.
* Again, just in case: you need to look at the description of the user object in AD that you ran the script with.
* The user running the script needs to be a domain administrator, or have the right to change the Description attribute of the respective user object.
* If you're verifying that using the ADUC MMC: you need to manually refresh the user's OU (and re-open the properties of the user).
* There might be a replication delay, and the DC you're checking against might differ from the one that accepted the change. You can use the ADUC MMC to connect with a given DC to check the object's properties on the other DCs.
I went to ADUC and to the OU with the user...delegated 'write description' permissions to the user

Ran the logon script through a GPO

No change to the user's description

Verified using RSOP.MSC that the GPO was processed
Works perfectly fine here. Don't bother running it as logon script unless it works when the user runs it directly from a PS console.
This is more verbose and writes a log file into the user's %temp% folder.
It writes the Description and reads it immediately back.
Do not change anything except the first two lines.
$LogFile = "${ENV:Temp}\Logon.log"
$Description = "Logged on to $($ENV:ComputerName) at $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')"

Start-Transcript -Path $LogFile
$ADSystemInfo = New-Object -ComObject 'ADSystemInfo'
$UserCN = $ADSystemInfo.GetType().InvokeMember('Username', 'GetProperty', $Null, $ADSystemInfo, $Null)
[void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($ADSystemInfo)

"User CN: '$($UserCN)'"
$ADUser = [ADSI]"LDAP://$($UserCN)"
"Old description: '$($ADUser.Description)'"
"Setting new description '$($Description)'"
$ADUser.Description = $Description
[void]$ADUser.SetInfo()

Remove-Variable -Name ADUser
$ADUser = [ADSI]"LDAP://$($UserCN)"
"New description: '$($ADUser.Description)'"
Stop-Transcript

Open in new window

Thanks, but if I don't run it as a logon script, how do I get it out to the users??
I didn't say "don't run it as a logon script", I said "Don't bother running it as logon script unless it works when the user runs it directly from a PS console.".
So ... does it work when the user runs it directly from a PS console?
Running it on a laptop logged in as a student (non local admin)  I get this

PS C:\Users\test2021> C:\Users\test2021\Desktop\testscript.ps1
Transcript started, output file is C:\Users\test2021\AppData\Local\Temp\Logonscript.log
Cannot invoke method. Method invocation is supported only on core types in this language mode.
At C:\Users\test2021\Desktop\testscript.ps1:6 char:1
+ $UserCN = $ADSystemInfo.GetType().InvokeMember('Username', 'GetProper ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodInvocationNotSupportedInConstrainedLanguage
 
Cannot invoke method. Method invocation is supported only on core types in this language mode.
At C:\Users\test2021\Desktop\testscript.ps1:7 char:1
+ [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($ADSys ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodInvocationNotSupportedInConstrainedLanguage
 
User CN: ''
Old description: ''
Setting new description 'Logged on to PC007QFG-21 at 2017-11-22 06:55:49'
Exception setting "Description": "The following exception occurred while retrieving member "Description": "Unknown error (0x80005000)""
At C:\Users\test2021\Desktop\testscript.ps1:13 char:1
+ $ADUser.Description = $Description
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
    + FullyQualifiedErrorId : ExceptionWhenSetting
 
The following exception occurred while retrieving member "SetInfo": "Unknown error (0x80005000)"
At C:\Users\test2021\Desktop\testscript.ps1:14 char:1
+ [void]$ADUser.SetInfo()
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], ExtendedTypeSystemException
    + FullyQualifiedErrorId : CatchFromBaseGetMember
 
New description: '' f
Transcript stopped, output file is C:\Users\test2021\AppData\Local\Temp\Logonscript.log  f
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Indeed, I have AppLocker on these machines. Thanks for the clarity.