Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Execute vbscript variable as code

Posted on 2013-12-20
5
Medium Priority
?
448 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 14

Assisted Solution

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

Accepted Solution

by:
Alexander Eßer [Alex140181] earned 2000 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 14

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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

A hard and fast method for reducing Active Directory Administrators members.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

824 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