Solved

Execute vbscript variable as code

Posted on 2013-12-20
5
440 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
Let's recap what we learned from yesterday's Skyport Systems webinar.
The viewer will learn how to dynamically set the form action using jQuery.
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 …

615 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