Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script not writing to file when run via Scheduled Task

Status
Not open for further replies.

martinshort

Technical User
Sep 26, 2007
3
GB
Hi all

I'm having problems writing out to a csv. This works perfectly if I run it from the local PC, but as soon as I run it from a scheduled task, the output file is created and correctly named but no data is output; the file being 0 Kb.

I have tried running this as a local user, local admin and domain admin to no avail.

Any thoughts from anyone as to what I am doing wrong.

Many thanks
Martin
(One confused VBS newbie!)

Code:
Option Explicit
Dim objFSO, objFolder, objShell, objTextFile, objFile
Dim objNetwork, objPrinter, intDrive, intNetLetter
Dim strDirectory, strFile, strText, strPCName
Dim regComputerName

'Read in PCName to variable called strPCName

regComputerName = "HKLM\SYSTEM\CurrentControlSet\Control" & 

"\ComputerName\ComputerName\ComputerName"
Set objShell = CreateObject("WScript.Shell")
strPCName = objShell.RegRead(regComputerName)

'-----------------------------------------------------------

strDirectory = "C:"
strFile = "\" & strPCName & ".csv"

' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Check that the strDirectory folder exists
If objFSO.FolderExists(strDirectory) Then
  Set objFolder = objFSO.GetFolder(strDirectory)
Else
  Set objFolder = objFSO.CreateFolder(strDirectory)
  'WScript.Echo "Just created " & strDirectory
End If

If objFSO.FileExists(strDirectory & strFile) Then
  Set objFolder = objFSO.GetFolder(strDirectory)
Else
  Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
  'Wscript.Echo "Just created " & strDirectory & strFile
End If

set objFile = nothing
set objFolder = nothing


' ---------------------------------------------------------------' 

' OpenTextFile Method needs a Const value
' ForAppending = 8 ForReading = 1, ForWriting = 2
Const ForAppending = 8
Const ForWriting = 2

Set objTextFile = objFSO.OpenTextFile _
(strDirectory & strFile, ForWriting, True)

' ---------------------------------------------------------------' 

Set objNetwork = CreateObject("WScript.Network") 
Set objPrinter = objNetwork.EnumPrinterConnections

For intDrive = 0 To (objPrinter.Count -1) Step 2
intNetLetter = IntNetLetter +1
strText = objPrinter.Item(intDrive) & "," & objPrinter.Item(intDrive +1) & "," & intDrive
objTextFile.WriteLine(strText)
Next

' ---------------------------------------------------------------' 

objTextFile.Close

WScript.Quit
 
hi Markdmac

I work in the same company as martin. The script you helped him with did work but the script i have above which i want to use to remove printers and add printers dosen't seem to work. Just says running and dosen't finish running and nothing happens to the client PC

If you could help it would be appreciated

STORMRAGE
 
Take a look at my login script FAQ which has sample code to do what you are looking for. faq329-5798

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Hi Mark

Sorry for not responding sooner.

I had a suspicion that I knew who Stormrage was on the grounds of the subject matter - in fact I started this thread to help him out.

However, "Stormrage" and I have failed to be in the office at the same time for over a week now and still haven't spoken face to face. I wanted to wait until he got back from vacation before I responded, so I could confirm said suspicion & identity! :)

All the help you have given has been first class, it's just that when I started this, I was only aware of the first objective.

Many thanks
Martin
 
Tek-tips...the old Facebook...

;)

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top