Solved

Execute vbscript variable as code

Posted on 2013-12-20
5
433 Views
Last Modified: 2013-12-20
Experts,

This should be relatively straight forward, but I'm drawing a blank.

I'm trying to execute some dynamic code in vbscript from a variable, but I can't seem to get it right:

Sub subAddDic(strDicObj, strDicKey, strDicValue)
	If strDicObj <> "" And strDicKey <> "" And strDicValue <> "" Then
		Set WshShell = CreateObject("WScript.Shell")
		strAddCmd = _
			"If Not " & strDicObj & ".Exists(" & fncQuote(strDicKey) & ") Then" & vbCrLf & _
				strDicObj & ".Add " & fncQuote(strDicKey) & " " & fncQuote(strDicValue) & vbCrLf & _
			"End If"
		WshShell.Run(strAddCmd)
		Set WshShell = Nothing
	End If
End Sub

Function fncQuote(strData)
	If strData <> "" Then
		fncQuote = Chr(34) & strData & Chr(34)
	Else
		fncQuote = ""
	End If
End Function

Open in new window


I'm erroring on this line "WshShell.Run(strAddCmd)" - where it's attempting to look for a file on the filesystem, and tells me that it's not found.

Anyone know how I can execute the inline variable as just vbs?
0
Comment
Question by:usslindstrom
  • 3
  • 2
5 Comments
 
LVL 13

Assisted Solution

by:Alexander Eßer [Alex140181]
Alexander Eßer [Alex140181] earned 500 total points
ID: 39731782
Obviously, WshShell.Run runs an executable file on the OS ;-)
So it tries to run "If"...
0
 
LVL 13

Accepted Solution

by:
Alexander Eßer [Alex140181] earned 500 total points
ID: 39731784
0
 
LVL 5

Author Closing Comment

by:usslindstrom
ID: 39731842
That did it, thanks guys.

The 'Execute' method works great.  I noticted I was missing a comma in my previous script, which could have been causing me a few headaches too.

Not much different, but here's the working version if anyone's interested:

Sub subAddDic(strDicObj, strDicKey, strDicValue)
	If strDicObj <> "" And strDicKey <> "" And strDicValue <> "" Then
		strAddCmd = _
			"If Not " & strDicObj & ".Exists(" & fncQuote(strDicKey) & ") Then" & vbCrLf & _
				strDicObj & ".Add " & fncQuote(strDicKey) & ", " & fncQuote(strDicValue) & vbCrLf & _
			"End If"
		Execute strAddCmd
	End If
End Sub

Function fncQuote(strData)
	If strData <> "" Then
		fncQuote = Chr(34) & strData & Chr(34)
	Else
		fncQuote = ""
	End If
End Function

Open in new window


Much appreciated - you guys rock!
0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 39731921
Actually I am just ONE guy, but anyways...
Thanks a lot for sharing your solution with the EE community ;-)
0
 
LVL 5

Author Comment

by:usslindstrom
ID: 39731955
lol - I just noticed that it was you that answered twice.  Apologies, I wasn't paying attention to the usernames.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now