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
ejcristAsked:
Who is Participating?
 
oBdAConnect With a Mentor Commented:
Not a script issue. You probably have AppLocker or a Software Restriction Policy in place.
About Language Modes
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_language_modes
PowerShell ♥ the Blue Team
https://blogs.msdn.microsoft.com/powershell/2015/06/09/powershell-the-blue-team/
0
 
NorieVBA ExpertCommented:
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

0
 
ejcristAuthor Commented:
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
0
Worried about phishing attacks?

90% of attacks start with a phish. It’s critical that IT admins and MSSPs have the right security in place to protect their end users from these phishing attacks. Check out our latest feature brief for tips and tricks to keep your employees off a hackers line!

 
oBdACommented:
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

0
 
ejcristAuthor Commented:
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.
0
 
oBdACommented:
* 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.
0
 
ejcristAuthor Commented:
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
0
 
oBdACommented:
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

0
 
ejcristAuthor Commented:
Thanks, but if I don't run it as a logon script, how do I get it out to the users??
0
 
oBdACommented:
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?
0
 
ejcristAuthor Commented:
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
0
 
ejcristAuthor Commented:
Indeed, I have AppLocker on these machines. Thanks for the clarity.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.