Reading REG_QWORD with VBScript? - vbscript

I think the question speaks for itself. I have trouble getting some values out of the registry, and I was hoping someone around here might help me.
I'm stuck at IE9, as it is the only one which has some reasonable CSS capabilities, and does support GetObject().
So right now, lets say I'm trying to retrieve the memory size of a GPU at "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}\0000\HardwareInformation.qwMemorySize" (as far as I know, this should be a universal path & key).
This is where the problem begins. Either I get no output, or some error saying something is different, or what (my system is running in a different language so I cant offer the right translation).
After some research, I seem to have found the issue - the value I'm trying to read is REG_QWORD, and unfortunately I was only able to find very little covering this topic, and most of the solutions did not work for me.
So right now, I am with this code, which, unsurprisingly, also does not work (the code I had since like the beginning):
for Each oItem in colGPUs
memory = oItem.AdapterRAM / 1048576
If memory < 0 Then
If InStr(oItem.Name, "NVIDIA") Then
Set wssx = CreateObject("WScript.Shell")
msgbox CStr(wssx.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}\000" + GPUID + "\HardwareInformation.qwMemorySize"))
End If
End If

Unfortunatelly it seems like there is no direct way of retrieving the value - within HTA itself.
I was able to get the value, however I did it using Powershell, executed the command, set its output to a specific file and read it.
Anyways, here is the actual solution I came up with specifically for this issue
wshell.Run "powershell (Get-ItemPropertyValue 'HKLM:\SYSTEM\ControlSet001\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}\0000' 'HardwareInformation.qwMemorySize') | Out-File -FilePath C:\temp\gpu_mem.txt", 0, true
Set f = fso.OpenTextFile("C:\temp\gpu_mem.txt", 1, False, -1)
gpu_mem = CStr(f.ReadAll)
With this method Im directly obtaining the integer and passing it to the VBS

Related

SAS Enterprise Guide with VBScript. Looping through SAS programs get stuck

I'm facing a random problem. When executing SAS programs with VBScript and the SASEGObjectModel.Application.7.1, looping through CodeCollection get stuck sometimes, even if the program execution was succeeded (the final data bases are correctly created in our server). The script simple doesn't go to the next program of CodeCollection (the prompt executing the script still open... ad infinitum). The SAS program It happens is random, also the frequency. I'm going with something like this:
Dim oSasApp
Set oSasApp = CreateObject("SASEGObjectModel.Application.7.1")
oSasApp.SetActiveProfile("some-profile")
Dim oSasProj
Set oSasProj = oSasApp.Open("some-project.egp", "")
Dim oProgramList
Set oProgramList = oSasProj.CodeCollection
Dim programOrder
Set programOrder = ...here I assign the SAS programs order array reading from a .txt...
For Each program in programOrder
For Each sasProgram in oProgramList
If sasProgram.Name = program Then
sasProgram.Run
sasProgram.Log.SaveAs "some-folder/" & sasProgram.Name & ".txt"
End If
Next
Next
oSasProj.Close
oSasApp.Quit
The problem is not the Log saving, as the log txt file of the stucked program is also correctly created.
Any idea? Maybe problems in our SAS server? Should I declare some kind of options?
SAS Guide version: 7.15
Windows: 10
Tks
So... for people facing the same problem. As I commented above, if I press enter on prompt the script flows again. So it is waiting for my input, for reasons I can't tell. I did 2 things to get around it. Not sure if all of them are necessary or if only one solves it, but here it goes:
First, by VBScript I turned off a list of generations and I applied a delay after the SAS program runs:
For Each program in programOrder
For Each sasProgram i oProgramList
If sasProgram.Name = program Then
sasProgram.GenSasReport = False
sasProgram.GenHTML = False
sasProgram.GenListing = False
sasProgram.GenPDF = False
sasProgram.GenRTF = False
sasProgram.Run
WScript.Sleep(2000)
sasProgram.Log.SaveAs "some-folder/" & sasProgram.Name & ".txt"
End If
Next
Next
Them, in my batch file, wich I use to call the VBScript with the "cscript" command, I set it to apply "y" to every single message the VBScript could ask:
cd ./script-folder
echo y | cscript script-file-name.vbs
And that is it.

Scripting Word from vbs

I'm trying to get Word to fill in cells in a table. The script works when run as a macro from within Word, but fails when saved as a .vbs file and double-clicked, or run with wscript. This is a part of it.
set obj = GetObject(,"Word.Application)
With obj
With .Selection
MsgBox .text
If (.Information(wdWithInTable) = True) Then
.Collapse Direction:=wdCollapseStart
tCols = .Tables(1).Columns.Count
tRow = .Information(wdStartOfRangeRowNumber)
tCol = .Information(wdStartOfRangeColumnNumber)
For I = 2 To 5
.Tables(1).Cell(tRow, I).Range.Text = "fred" & Str(I)
Next
` now make new row
For I = 1 To tCols - tCol + 1
.MoveRight unit:=wdCell
Next
End If
End With
End With
I have three problems. First, it won't compile unless I comment out the .Collapse and .MoveRight lines. Second, although the MsgBox .text displays the selected text, I get "out of range" errors if I try to access any .Information property.
I'm sure I'm missing something very simple: I usually write software for Macs, and I'd do this using AppleScript. This is my first attempt at getting anything done under Windows.
VBScript and VBA are different languages.
They are a bit similar, but not very. Moreover, VBScript is not like AppleScript; it doesn't let you easily interface with running programs.
The interfaces you'll get from VBScript can behave subtly differently in VBA and VBScript. However, I think you've got two problems here:
:= is invalid syntax in VBScript; you'll need to find an alternative way of calling the function. Try just using positional arguments.
You've no guarantee that this will open the expected file; there could be another instance of Word that it's interacting with instead.
Since your code is not running within the Word environment it would require a reference to the Word object library in order to use enumeration constants (those things that start with wd).
VBScript, however, cannot work with references, which means the only possibility is to use the long value equivalents of the enumerations. You'll find these in the Word Language References. Simplest to use is probably the Object Browser in Word's VBA Editor. (In Word: Alt+F11 to open the VBA Editor; F2 to start the Object Browser; type in the term in the "Search" box, click on the term, then look in the bottom bar.)
The code in the question uses, for example:
wdWithInTable
wdCollapseStart
wdStartOfRangeRowNumber
wdStartOfRangeColumnNumber
wdCell
The reason you get various kinds of errors depends on where these are used.
Also, VBScript can't used named parameters such as Unit:=. Any parameters must be passed in comma-delimited format, if there's more than one, in the order specified by the method or property. If there are optional parameters you don't want to use these should be left "blank":
MethodName parameter, parameter, , , parameter

VBScript Nothing returns garbage

We have to use VBScript as an embedded script engine in our Hospital Information System.
When we use nothing to set the value of a control (textbox/checkbox/...) it worked always fine. Since somepoint it sets now the textbox to "?>".
item("TEXTBOX").value = nothing ' Leads to -> "?>"
It is not completly clear what causes this, maybe a windows update is responsible, every rollup ~ since KB3212646 Win7 2017-01 seems to cause this error.
My Question is now, has someone else also seem this error, so that it is clear that MS causes this error or is our HIS publisher responsible for not handling nothing correct.
I know setting a textbox to Nothing is not best practice instead "" should be better, but since the item object could be more the just a textbox e.g. a combobox/checkbox this seems, from an objectoriented perpsective, better. Or am I completly wrong?
Following #Ansgar comment, you should apparently change everywhere you have = nothing to = "" in your example
item("TEXTBOX").value = ""
Beware to keep the nothing if you have the Set keyword in left
Set some_object = Nothing

What is the proper syntax for ".Stop" method for "Schedule.Service"?

I have some tasks that need to be removed. Some users have installed Chrome under their own profile and the only way to remove it is to "nuke" it since it is not feasible to log in with their account and do the uninstall the proper way. The scheduled tasks have a user sid added to the end of the name, so I have to search for a task with "GoogleUpdate" in the string. When I remove a service, I like to stop the service, disable the service, then delete it. I want to do the same thing with the scheduled tasks. After a lot of scouring of the web, I have found the syntax to disable, delete and that works perfect, but not how to stop. I would prefer not to shell out and use schtasks since I have come this far with a 200 + line script using all VbScript.
Here is what I have so far. I need help figuring out the syntax for the ".Stop" method. If anyone has an example of the stop method, it would be much appreciated.
Set objTaskService = CreateObject("Schedule.Service")
Call objTaskService.Connect()
Set objTaskFolder = objTaskService.GetFolder("\")
Set colTasks = objTaskFolder.GetTasks(0)
For Each objTask In colTasks
With objTask
If InStr(.Name, "GoogleUpdate") Then
objTask.Stop() << Wrong number of args or invalid property assignment
WScript.Sleep(10000)
objTask.Enabled = False << Working properly
objTaskFolder.DeleteTask objTask.Name,0 << Working properly
End If
End With
Next
The official documentation is located here: https://msdn.microsoft.com/en-us/library/windows/desktop/aa382098(v=vs.85).aspx
RegisteredTask.Stop(ByVal flags)
Parameters
flags [in]
Reserved.
Must be zero.
Hence, it should be
objTask.Stop 0

Insert Property of Picture Class

I have read all of the questions on here about this topic and none of them provided me with a workable solution, so I'm asking this one.
I am running a legitimate copy of Excel 2013 in Windows 7. I record a macros where I insert a picture, and in the open file dialog I paste this URL: http://ecx.images-amazon.com/images/I/41u%2BilIi00L._SL160_.jpg (simply a picture of a product on Amazon). This works as expected.
The resulting macros looks like this:
Sub insertImage()
'
' percent Macro
'
'
ActiveSheet.Pictures.Insert( _
"http://ecx.images-amazon.com/images/I/41u+ilIi00L._SL160_.jpg").Select
End Sub
However, when I attempt to run this, the Insert line breaks with the following error:
Run-time error '1004':
Unable to get the Insert property of the Picture class
I am trying to insert a number of pictures into an excel document and I am using the ActiveSheet.Pictures.Insert method to do this. I have been experiencing this issue there, so I recreated it in a way others could replicate to facilitate getting an answer...
An interesting thing to note is:
http://ecx.images-amazon.com/images/I/41u%2BilIi00L._SL160_.jpg 'This is what I pasted
http://ecx.images-amazon.com/images/I/41u+ilIi00L._SL160_.jpg 'This is what the recorded macros recorded
It looks like Excel automatically resolved the %2B to a +. I tried making that change, but to no success.
Another interesting thing to note is that sometimes this does work and sometimes it doesn't. This url is a case where it does not work. Here's one where it does: http://ecx.images-amazon.com/images/I/51mXQ-IjigL._SL160_.jpg
Why would Excel generate a macros it can't run? More importantly, how can I avoid this error and get on with my work!? Thanks!
Try this workaround:
Sub RetrieveImage()
Dim wsht As Worksheet: Set wsht = ThisWorkbook.ActiveSheet
wsht.Shapes.AddPicture "http://ecx.images-amazon.com/images/I/41u+ilIi00L._SL160_.jpg", _
msoFalse, msoTrue, 0, 0, 100, 100
End Sub
All fields are required, which is kind of a bummer since you cannot get the default size. The location offsets and sizes are in pixels/points. Also, the % turning to + is just alright, as % would cause it to be not recognized (dunno why).
Result:
Let us know if this helps.
I'm experiencing the same issue.
After some digging I found out Excel does a HTTP HEAD request before getting the image.
If this HEAD request is unsuccessful Excel will return the exact error messages mentioned in this discussion.
Your could easily test this using Fiddler.

Resources