Avatar of syntacticalerror
syntacticalerror

asked on 

Cannot write collection item properties to text file

to all,

good day.......i am starting wmi scripting and i am practicing to write property results into a text file.  With the WSH objects i had no problems, however, there are two issues i am having with code.

If i use a monker string (as in snippet) to connect to the wmi service, while processing the script it stops at "objtext.writeline objfolder.name" with invalid call or procedure.

If i use a regular string, GetObject("winmgmts:\\" & strComputer & "\root\CIMV2"), to connect to the wmi service, the text does not contain all the folder names, it fact it stops way short.  Connected computer is locate.......tried with and without "Impersonate"

I know its retrieving all the folder names because if i use echo the names in the list is comprehensive.

any ideas?  thx
dim fso, objwmiservice, objtext, colfolders, objfolders
const Forwriting = 2
 
Set objWMIService = GetObject("winmgmts://Somecomputername/root/cimv2")
Set colFolders = objWMIService.ExecQuery("SELECT * FROM Win32_Directory")
 
set fso = createobject("scripting.filesystemobject")
set objtext = fso.opentextfile("c:\folders.txt", Forwriting, true)
 
For Each objFolder in colFolders
	writetext.Writeline objfolder.name
Next
 
objtext.close

Open in new window

VB ScriptScripting Languages

Avatar of undefined
Last Comment
syntacticalerror
Avatar of purplepomegranite
purplepomegranite
Flag of United Kingdom of Great Britain and Northern Ireland image

What is writetext?  Try the below.
dim fso, objwmiservice, objtext, colfolders, objfolders
const Forwriting = 2
 
Set objWMIService = GetObject("winmgmts://Somecomputername/root/cimv2")
Set colFolders = objWMIService.ExecQuery("SELECT * FROM Win32_Directory")
 
set fso = createobject("scripting.filesystemobject")
set objtext = fso.opentextfile("c:\folders.txt", Forwriting, true)
 
For Each objFolder in colFolders
	WScript.Writeline objfolder.name
Next
 
objtext.close

Open in new window

Sorry, not that...
dim fso, objwmiservice, objtext, colfolders, objfolders
const Forwriting = 2
 
Set objWMIService = GetObject("winmgmts://Somecomputername/root/cimv2")
Set colFolders = objWMIService.ExecQuery("SELECT * FROM Win32_Directory")
 
set fso = createobject("scripting.filesystemobject")
set objtext = fso.opentextfile("c:\folders.txt", Forwriting, true)
 
For Each objFolder in colFolders
	objtext.Writeline objfolder.name
Next
 
objtext.close

Open in new window

Avatar of syntacticalerror
syntacticalerror

ASKER

originally it was writetext as the object, i changed it to objtext before the post, but still same result, sorry for inconsistency.  any other ideas?
Interesting.  I ran the code below (note aimed at local computer), and it ran as expected.  All folders listed, no error.
Try the below, which is error-trapped.  If it can't write a folder, it will attempt to write it as an echo (so you can identify the folder name).  I am wondering if a strange folder name or similar could be causing problems...

dim fso, objwmiservice, objtext, colfolders, objfolders
const Forwriting = 2
 
Set objWMIService = GetObject("winmgmts://./root/cimv2")
Set colFolders = objWMIService.ExecQuery("SELECT * FROM Win32_Directory")
 
set fso = createobject("scripting.filesystemobject")
set objtext = fso.opentextfile("c:\folders.txt", Forwriting, true)
 
on error resume next
For Each objFolder in colFolders
	objtext.Writeline objfolder.name
	If Err.Number<>0 then
		Err.Clear
		WScript.Echo "Error occurred: " & objfolder.name
		if Err.Number<>0 then Err.Clear
	end if
Next
 
objtext.close

Open in new window

Avatar of syntacticalerror

ASKER

yea i suspect it was the files with chinese characters.........i have language settings in windows to be able to display chinese.........can i somehow alter the script to use unicode or something? thx
ASKER CERTIFIED SOLUTION
Avatar of purplepomegranite
purplepomegranite
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of syntacticalerror

ASKER

Spot on, i just need to get aquainted with more available options............thanks for the insight
VB Script
VB Script

VBScript (Visual Basic Scripting Edition) is an interpreted scripting language developed by Microsoft that is modeled on Visual Basic, but with some important differences. VBScript is commonly used for automating administrative and other tasks in Windows operating systems (by means of the Windows Script Host) and for server-side scripting in ASP web applications. It is also used for client-side scripting in Internet Explorer, specifically in intranet web applications.

39K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo