View Single Post
Old 05-27-2008   #6 (permalink)
Pegasus \(MVP\)


 
 

Re: HowTo run a vbscrit once?


"Nicola M" <NicolaM@xxxxxx> wrote in message
news:578FA128-8D2C-453B-8DE9-7B7F92B47B8E@xxxxxx
Quote:

> "Pegasus (MVP)" wrote:
>
Quote:

>> "Nicola M" <NicolaM@xxxxxx> wrote in message
>> news:0215124E-0898-4772-B7B8-3FA0BD9FEC65@xxxxxx
Quote:

>> > "Pegasus (MVP)" wrote:
>> >
>> >> "Nicola M" <NicolaM@xxxxxx> wrote in message
>> >> news:4BC1BF07-3DFE-4F70-972E-F98546DA5873@xxxxxx
>> >> > Hi all and sorry in advance for my English.
>> >> > I wrote a vbs script to manage network printers. This script deletes
>> >> > all
>> >> > network printers installed on a client and then install devices
>> >> > indicated in
>> >> > the script, only.
>> >> > It's possible that an user connect printers not listed in the
>> >> > script, manually.
>> >> > The script is distributed by group policy in a W2K Active Directory
>> >> > Environment so it runs at every user logon.
>> >> > So: to avoid deleting and installing printers at every logon what do
>> >> > i
>> >> > do to get script runs once?
>> >> >
>> >> > Thanks.
>> >> > Nicola M
>> >> >
>> >> Here are a couple options:
>> >> - You could invoke it via the RunOnce registry key.
>> >> - You could get the script to create a "semaphore" file
>> >> to signal that the script has run once before.
>> >
>> > The first one is more suitable. But... how can I put the invocation of
>> > the
>> > script in that registry Key?
>> > So, with vbs there's no a limit accessing registry? I use VBA also and
>> > I can
>> > write and read from HKEY_CURRENT_USER\Software\VB and VBA Program
>> > Settings\
>> > only.
>> > Thanks.
>> > Nicola M
>>
>> You could run this script, provided that the user has appropriate
>> access rights to his RunOnce key:
>> Set WshShell = WScript.CreateObject("WScript.Shell")
>> bKey =
>> "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce\PrinterInst"
>> WshShell.RegWrite bKey, "c:\Windows\PInstall.vbs"
>>
>> Although you may prefer this method, it is actually more
>> laborious than using a semaphore file, because it requires
>> two steps:
>> Step 1 - Put the key into the registry.
>> Step 2 - Let the user log on to execute the script.
>> It is also harder to test and debug.
>>
>> With a semaphore file you could do it in one fell swoop:
>> - Check if %UserProfile%\PrinterInst.txt exists.
>> - If it does, don't do anything.
>> - If it does not, create it and run the printer installation.
>> Furthermore, if the installation should fail then you could
>> delete the semaphore file from your console. Recreating
>> the HKCU\RunOnce key from your console is much harder.
>
> Wuf!! Pegasus! At this point I would like to see the other side of the
> moon
> ;-):
> 1) PrintersInst.txt is a file created by the script? I think it is so!
> 2) This file will contain all names and descriptions of the printer
> installed, it won't?
> 3) A little bit more of example code would be appreciate very very much...
>
> Thanks so much.
> Nicola M
This is a straight application of the KISS principle:

const sFName = "PrintersInst.log"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oWshShell = WScript.CreateObject("WScript.Shell")
sAppFolder = oWshShell.Environment("PROCESS")("AppData") & "\"
If Not oFSO.FileExists(sAppFolder & sFName) Then
Set oFile = oFSO.CreateTextFile(sAppFolder & sFName, True)
'Put any file write statements here if you wish
oFile.Close
'Put your printer installation code here.
End If

If your logon script is a batch file then you can do it like so:
@echo off
set Logfile=%AppData%\PrintersInst.log
if not exist "%Logfile%" (
echo Printer installed on %date% at %time% >> %LogFile%
rem Put your printer installation code here.
)

In both cases the printer installation will happen just once for this
particular user. To force it to happen again, delete
"%AppData%\PrintersInst.log", then rerun the script. To run
the script just once for every machine, change
%AppData%\PrintersInst.log to
%AllUsersProfile%\Application Data\PrintersInst.log


My System SpecsSystem Spec