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
 
Changes in [red]red[/red]

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

'Read in PCName to variable called strPCName
[red]
Set WSHNetwork = CreateObject("Wscript.Network")
strPCName = WSHNetwork.ComputerName
[/red]

Set objShell = CreateObject("WScript.Shell")

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

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
[red]
If Not objFSO.FileExists(strDirectory & strFile) Then
  Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
  'Wscript.Echo "Just created " & strDirectory & strFile
  objFile.Close
End If
[/red]

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
[red]'intNetLetter = IntNetLetter +1[/red]
strText = objPrinter.Item(intDrive) & "," & objPrinter.Item(intDrive +1) & "," & intDrive
objTextFile.WriteLine(strText)
Next

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

objTextFile.Close

WScript.Quit

I tested on Vista as a scheduled task and it wrote to the file without problem.

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.
 
Mark

Thank you.

I'm out of the office this afternoon, so I will test it tomorrow.

Cheers
Martin
 
Hi there Mark

I have seen the script above and am having a similar problem with the following script


'On Error Resume Next
Dim objNetwork, remUNCPrinter,
Dim strUNCPrinter, strUNCPrinter1, strUNCPrinter2, WSHNetwork

remuncprinter = "\\servername\Printer"

Set objNetwork = CreateObject("WScript.Network")
' Section which removes the network printer
objNetwork.RemovePrinterConnection remUNCPrinter

strUNCPrinter = "\\newservername\Printer"


Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection strUNCPrinter

'WScript.Echo "Check Printers folder NO: " & strUNCPrinter
Wscript.Quit



I mange to run it locally but when i schedule a task for it to run on another machine using any credentials it dosen't work. I assume i cannot use the same correction you used like "WSHNetwork"

Could please have a look and let me know where i am going wrong

Thanking you in advance


STORMRAGE
 
Hi there PHV

Internet Explorer 6 is installed on everybody's PC on the network and domain admins are part of local administrator group on individual PC's

Basically i don't want to run this from a server, i just want to run this from my XP Pro PC and schedule it to run on numerous Windows 2000 Pro SP4 PC's

The script work fine locally with any user account running the script, just can't run as a scheduled task

Thanks

STORMRAGE
 
Hi there everybody

Any news on this problem i am having? Please can you have a look at the code and see what i am missing

Your help would be appreciated

here is the code again


'On Error Resume Next
Dim objNetwork, remUNCPrinter,
Dim strUNCPrinter, strUNCPrinter1, strUNCPrinter2, WSHNetwork

remuncprinter = "\\servername\Printer"

Set objNetwork = CreateObject("WScript.Network")
' Section which removes the network printer
objNetwork.RemovePrinterConnection remUNCPrinter

strUNCPrinter = "\\newservername\Printer"


Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection strUNCPrinter

'WScript.Echo "Check Printers folder NO: " & strUNCPrinter
Wscript.Quit



My problem again is that i cannot run this script as a scheduled task, but can run it locally on my PC with just about every user account with no problems

Again your help would be appreciated


STORMRAGE
 
When you schedule a task, give it a definitive context of running under some user's credential (including password if so required.) Have you come across that part using the wizard? What os is it running against?
 
Schedule the script to run with AT.

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.
 
What do you mean by AT?
In a console window type AT /?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV

This script stuff is making me really annoyed, don't under stand why i can't run this script via scheduled task

I looked on the net to find another way and i found the WshNetwork command and have altered my script to look like this

Code:
Dim WSHNetwork

Set WshNetwork = CreateObject("WScript.Network") 
' Section which removes the network printer
WshNetwork.RemovePrinterConnection "\\oldserver\Printer"
WshNetwork.AddWindowsPrinterConnection "\\newserver\Printer"

Wscript.Quit



This also works locally but cannot get it to work via scheduled task AGAIN!!! I have realised that this script implies that there MUST be a network printer setup already on the client referencing the oldserver before the script can remove it otherwise it fails.

Please help

STORMRAGE
 
Have you set it up through schedule task wizard? That's a reference setup which must have run before examining alternative setup. In the wizard, at a stage, you are asked in what context the task is being run against (with password if necessary). The scheduled task should run as well even the user is not logged on at all. As to nothing is written to, it is not excluded that the context (might as well be local/system) actually has nothing to write about into it. Based on the fact that the file is created, the scheduled task seems to be running actually.
 
Tsuji

Try this on your computer and a remote computer.
Take the script below

Code:
Dim WSHNetwork

Set WshNetwork = CreateObject("WScript.Network") 
' Section which removes the network printer
WshNetwork.RemovePrinterConnection "\\oldserver\Printer"
WshNetwork.AddWindowsPrinterConnection "\\newserver\Printer"

Wscript.Quit

try run this on your PC as a scheduled task and just by double clicking on the vbs file and you will see it works fine

Now when i try make it a scheduled task i browse to the machine in Internet explorer by going \\pcname and pressing enter (i open internet explorer under a admin profile to view scheduled tasks on remote PC's)
Then i create a new task and then go to properties and tell the task to run script1.vbs as an eg from \\pcname\share\script1.vbs which is the path name of my PC locally (in other words, the script sits on my PC locally under a share and each scheduled task i create references the UNC path to my machine to run the script. The share has got full permissions to domain users, so it's not a problem with security)

Once all this is done i right click on the task on the remote machine through internet explorer and click run and it either stays in a running state or finishes but dosen't delete the old printer and add the new one


STORMRAGE
 
When you then log onto the \\pcname (not your now local), is the connection still there?
 
tsuji

When i log onto the remote pc by going to the desktop and login on as the user, the old connection for the printer will still be there...not the new one

STORMRAGE
 
>WshNetwork.RemovePrinterConnection "\\oldserver\Printer"
Try instead.
[tt]WshNetwork.RemovePrinterConnection "\\oldserver\Printer",true,true[/tt]
The setting of where/what/who seems quite unclear, but, it may be to me only.
 
Hi tsuji

I ran the script with the line you said i must alter.
It sits there still saying running. I am excuting this task via a admin login of Internet explorer and the in the same window browsing to \\pcname and clicking scheduled tasks and then selecting the task i made and clicking on run

So still no luck

The scheduled task as mentioned before is getting the file from my PC eg. \\sourcepc\share\vb.vbs file

Thanks

STORMRAGE
 
Martinshort, seems your thread has been hijacked. Did my earlier post resolve your issue?

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.
 
To:Stormrage
You can open a thread anew. You have to post the important configurations clearly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top