Discussion:
batch file that needs the default printer name as a variable
(too old to reply)
opshn
2006-10-23 19:11:13 UTC
Permalink
I have many computers here at work that varies from win2k to win XP and
I need to run a net use command every time the computers boot up.

The command is:

net use lpt1: \\pcname\printername /persistent:yes

The pcname is the name of the pc that the command runs on. So I could
use %computername% for that.
The printername is where I have my first problem. I want this to be
whatever the default printer is for the pc. Something like:
cscript prnmngr.vbs -g

Which I have tried outputting to a file and then trying to get my
printer name out of the file with a FIND command. Unsuccessfully.

The second issue is that each computer will be logged in as a user with
restricted access and I believe that the net use command needs to be
run by the local administrator. Which if I put this batch file in the
startup folder then it would not run as the administrator. I have found
some examples of passing a user name and password but I would rather
not do that in a batch file that any user could read.

In short I want two things:
1. The default printer name stored to a variable to use on a net use
command.
2. My batch file to run as administrator but without someone being able
to figure out what the password is.
billious
2006-10-23 19:52:42 UTC
Permalink
Post by opshn
I have many computers here at work that varies from win2k to win XP and
I need to run a net use command every time the computers boot up.
net use lpt1: \\pcname\printername /persistent:yes
The pcname is the name of the pc that the command runs on. So I could
use %computername% for that.
The printername is where I have my first problem. I want this to be
cscript prnmngr.vbs -g
Which I have tried outputting to a file and then trying to get my
printer name out of the file with a FIND command. Unsuccessfully.
The second issue is that each computer will be logged in as a user with
restricted access and I believe that the net use command needs to be
run by the local administrator. Which if I put this batch file in the
startup folder then it would not run as the administrator. I have found
some examples of passing a user name and password but I would rather
not do that in a batch file that any user could read.
1. The default printer name stored to a variable to use on a net use
command.
2. My batch file to run as administrator but without someone being able
to figure out what the password is.
Good question.

First matter - alt.msdos.batch.nt is a group dedicated to NT+ methods.
Second, prnmngr.vbs does not appear to be a standard Microsoft-supplied
script. If you were to say what the output format was, no doubt we'd be able
to wangle a way to get what you want into a variable.
And last - if your users would examine the logonscript to find the
passwords, what would stop them from examining your proposed script to find
how to run a job with admin privileges?
opshn
2006-10-23 21:32:01 UTC
Permalink
In retrospect this question did belong in the .nt section.

But since I didn't do that I will try to be more specific.
If I run the following from a command prompt -

cscript prnmngr.vbs -g >> %temp%\$net.txt

The result is a file that looks like this:
__________________________________________________________
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

The default printer is hp deskjet 5600
__________________________________________________________

I want to store "hp deskjet 5600" to a variable.
billious
2006-10-23 21:48:59 UTC
Permalink
Post by opshn
In retrospect this question did belong in the .nt section.
But since I didn't do that I will try to be more specific.
If I run the following from a command prompt -
cscript prnmngr.vbs -g >> %temp%\$net.txt
__________________________________________________________
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
The default printer is hp deskjet 5600
__________________________________________________________
I want to store "hp deskjet 5600" to a variable.
[untested, since .vbs not on my system]

for /f "tokens=4*" %%i in ( ' cscript prnmngr.vbs -g ^|find "default
printer" ' ) do set ydp=%%j

where
* ydp acquires your Default Printer
* the spaces surrounding the single-quotes are for clarity. the quotes are
required; the spaces not.
* all on one batchfile line - it may be wrapped as you view it.
* In using ydp in your batch, you would probably have to encase it in
double-quotes "%ydp%"
** Personal opinion - don't establish names with non-alphamerics. Stick to
A..Z,a..z,0..9 and underscore.
opshn
2006-10-23 22:03:54 UTC
Permalink
Thank you very much. That helped out enormously.
Post by billious
Post by opshn
In retrospect this question did belong in the .nt section.
But since I didn't do that I will try to be more specific.
If I run the following from a command prompt -
cscript prnmngr.vbs -g >> %temp%\$net.txt
__________________________________________________________
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
The default printer is hp deskjet 5600
__________________________________________________________
I want to store "hp deskjet 5600" to a variable.
[untested, since .vbs not on my system]
for /f "tokens=4*" %%i in ( ' cscript prnmngr.vbs -g ^|find "default
printer" ' ) do set ydp=%%j
where
* ydp acquires your Default Printer
* the spaces surrounding the single-quotes are for clarity. the quotes are
required; the spaces not.
* all on one batchfile line - it may be wrapped as you view it.
* In using ydp in your batch, you would probably have to encase it in
double-quotes "%ydp%"
** Personal opinion - don't establish names with non-alphamerics. Stick to
A..Z,a..z,0..9 and underscore.
Mike Jones
2006-10-24 07:18:07 UTC
Permalink
Post by billious
Post by opshn
In retrospect this question did belong in the .nt section.
But since I didn't do that I will try to be more specific.
If I run the following from a command prompt -
cscript prnmngr.vbs -g >> %temp%\$net.txt
__________________________________________________________
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
The default printer is hp deskjet 5600
__________________________________________________________
I want to store "hp deskjet 5600" to a variable.
[untested, since .vbs not on my system]
for /f "tokens=4*" %%i in ( ' cscript prnmngr.vbs -g ^|find "default
printer" ' ) do set ydp=%%j
where
* ydp acquires your Default Printer
* the spaces surrounding the single-quotes are for clarity. the quotes are
required; the spaces not.
* all on one batchfile line - it may be wrapped as you view it.
* In using ydp in your batch, you would probably have to encase it in
double-quotes "%ydp%"
** Personal opinion - don't establish names with non-alphamerics. Stick to
A..Z,a..z,0..9 and underscore.
cscript can suppress it's header information by adding the parameter "//B"
Todd Vargo
2006-10-24 22:24:34 UTC
Permalink
Post by opshn
In retrospect this question did belong in the .nt section.
But since I didn't do that I will try to be more specific.
If I run the following from a command prompt -
cscript prnmngr.vbs -g >> %temp%\$net.txt
__________________________________________________________
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
The default printer is hp deskjet 5600
__________________________________________________________
I want to store "hp deskjet 5600" to a variable.
Please post the contents of prnmngr.vbs
--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)
Ted Davis
2006-10-23 20:38:42 UTC
Permalink
Post by opshn
I have many computers here at work that varies from win2k to win XP and
I need to run a net use command every time the computers boot up.
net use lpt1: \\pcname\printername /persistent:yes
The pcname is the name of the pc that the command runs on. So I could
use %computername% for that.
The printername is where I have my first problem. I want this to be
cscript prnmngr.vbs -g
Which I have tried outputting to a file and then trying to get my
printer name out of the file with a FIND command. Unsuccessfully.
The second issue is that each computer will be logged in as a user with
restricted access and I believe that the net use command needs to be
run by the local administrator. Which if I put this batch file in the
startup folder then it would not run as the administrator. I have found
some examples of passing a user name and password but I would rather
not do that in a batch file that any user could read.
1. The default printer name stored to a variable to use on a net use
command.
2. My batch file to run as administrator but without someone being able
to figure out what the password is.
You are barking up the wrong tree: network connections and default
printers are user specific, not machine specific, so you need for the
script to run at login, not on boot, so the mappings are visible to
the current user. Running as Administrator would make the connection
for Administrator, but not for the user - I don't see the point in
that.

Put the script in the All users startup directory.

I don't know how to identify the default printer (which is subject to
change after the script has run anyway).
--
T.E.D. (***@gearbox.maem.umr.edu)
Remove "gearbox.maem." from address - that one is dead
Continue reading on narkive:
Loading...