Solved

Execute vbscript variable as code

Posted on 2013-12-20
5
437 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Last week, our Skyport webinar on “How to secure your Active Directory” (https://www.experts-exchange.com/videos/5810/Webinar-Is-Your-Active-Directory-as-Secure-as-You-Think.html?cid=Gene_Skyport) provided 218 attendees with a step-by-step guide for…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

820 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