1

I'm trying to use VBScript in HTML to write text into what will become a powershell script. I am doing this to avoid having to statically code into my HTA the location of these powershell scripts.

My problem becomes working around Powershell's " ( and )

An example, I'm just not sure how to wrap the characters in order to keep VBS happy.

Dim filesys, filetxt
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set filesys = CreateObject("Scripting.FileSystemObject")
Set filetxt = filesys.OpenTextFile("c:\Temp\somefile.txt", ForAppending, True)
filetxt.WriteLine ("Param([Parameter(Mandatory=$true)]")
filetxt.WriteLine ("[string]$Str)")
filetxt.WriteLine ("# Create the IE com object")
filetxt.WriteLine ("$ie = new-object -comobject InternetExplorer.Application")
filetxt.WriteLine ("#Navigate to www.")
filetxt.WriteLine ("$ie.navigate("http://www.page.com")")

1 Answer 1

2

Assuming that last line is your only issue, you can escape the quotes either by doubling them up or by using Chr(34) to programatically insert them.

filetxt.WriteLine ("$ie.navigate(""http://www.page.com"")")

filetxt.WriteLine ("$ie.navigate(" & Chr(34) & "http://www.page.com" & Chr(34) & ")")
Sign up to request clarification or add additional context in comments.

4 Comments

I knew it was something simple! Ended up using filetxt.WriteLine ("$ie.navigate(" & Chr(34) & "http://www.website.com" & Chr(34) & ")") But now I'm stuck on trying to delete the temporary script created. VBS is telling me Permission denied for filesys.DeleteFile "c:\Temp\somefile.txt"
Did you try the optional force argument on the DeleteFile command: filesys.DeleteFile "c:\temp\somefile.txt", True? Make sure you're closing the filestream first with filetxt.Close if you're performing this within the same script. Otherwise, it may be a permissions issue, in which case you could try to elevate the permissions within VBScript using the ShellExecute/runas method here: ss64.com/vb/shellexecute.html
Ended up using filedelete.DeleteFile "c:\temp\temp.ps1" which works. Thanks again!
Just for follow up, I'm using filesys.DeleteFile (filesys.GetSpecialFolder(2) & "\FileName.ps1") where Set filesys = CreateObject("Scripting.FileSystemObject")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.