Ezine 168 - Mapping Drives and Printers with PowerShell
Ezine 168 - Mapping Drives and Printers with PowerShell
This week's scripts illustrate the advantages of transferring from PowerShell most
eloquently. VBScripters will recognise the following CreateObject command, and I
hope they can see the similarities in the way that PowerShell maps to
printers.
Set objNetwork = CreateObject("WScript.Network") [VBScript] new-Object
-com WScript.Network [PowerShell]
Topics for Mapping PowerShell's Printers and Network Drives
Back in the year 2,000 when I first published my website I noticed that
the pages on mapping network drives and mapping printers generated the
most hits. From that basic market research I developed a whole
section on VBScript logon scripts.
Over time I have faced a dilemma, on the one hand my readers clamber for
more on mapping drives and printers, while on the other hand I have a
vision of Group Policies providing better methods for giving users access
to printers and shared resources.
This week's mission is split into two sections. Firstly, a 'refresher'
on adding or mapping a printer. If you remember the idea is that
you have a printer attached to server and you want the users to be able to
'map' to this network printer; as a result they can get hard copies of
their documents.
Secondly, to appreciate how just five words of PowerShell build a network
object, which we then can manipulate in all manner of ways. That one-line is: new-Object
-com WScript.Network. [Note the only two spaces are before -com, and
between -com and WScript].
As aside I hope that you can see how easy it is to transfer VBScript
skills to PowerShell.
It was a coin toss whether to start with map network drive, or add
printer. I came down on the side of the printer because it followed on
from last week's script.
Preliminary step: To get this script to work on your computer you must amend 'Server\PrintShare'
to reflect the name of a real server and a real printer on your network.
# PowerShell for Mapping a
Printer # Author: Guy Thomas # Version 1.3 July 2008 tested on PowerShell v 1.0
Note 1: Did you edit the value for $PrinterPath to reflect
a computer on your network?
Note 2: At the heart of this PowerShell script is:
New-Object -com WScript.Network.
Note 3: Troubleshooting There should be no space
before the bracket, I
realized that for once there was no spelling mistake in AddWindowsPrinterConnection , just an unwanted space,
to see what I mean here is the error amplified in the line below:
$net.AddWindowsPrinterConnection ($PrinterPath).
Note 4: Talking of brackets, in PowerShell the
type of bracket is always highly significant, the (parenthesis style) means a compulsory component. PowerShell's
{curly brackets} pronounce:- script block inside. Occasionally
PowerShell employs a third type, [square brackets are optional]. Thus
the type of bracket gives extra information, and all these nuances make
scripts easier to read and easier to troubleshoot.
As usual, preparation and orientation are the keys to a successful script,
in this instance amend the value for the variable $Share. I seriously
doubt you have a server called 'server' and a share called 'share'.
# PowerShell for Map a Network
Drive # Author: Guy Thomas # Version 2.2 July 2008 tested on PowerShell v 1.0
Note 1: How did I know about the .mapnetworkdrive
method? The answer is once I created the com object called
$Net, I tried this command: $Net | get-Method
Note 1a: The above get-Method (gm) technique
reveals more .net methods, for instance, AddWindowsPrinterConnection which
we used in Example 1.
Note 2: As I hinted earlier, I have a whole
section describing the syntax for MapNetworkDrive and the other com objects.
The point of this script is to get you started emulating, then replacing VBScript with
PowerShell.
If you are looking for handy network utilities, try some of the free downloads at
Tools4Ever
Summary of PowerShell Mapping Drives and Printers
I sympathise with those who are reluctant to
abandon their VBScript skills in favour of PowerShell. However, I
justify becoming a dripping tap and preaching, 'Switch to PowerShell',
because I have taken this route myself and I am reaping the benefits.
As one level this article explains how to map a drive or a printer using
PowerShell. At other level it shows how easy it is to create
.net objects and thus assist you make the transition from VBScript to
PowerShell.
Please write in if you see errors of any kind. Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.
Guy
Recommends: Orion's NPM - Network Performance Monitor
Orion's performance monitor is designed for detecting network outages.
A network-centric
view make it easy to see what's working, and what needs your attention.
This utility guides you through troubleshooting by indicating whether the
root cause is faulty equipment or resource overload.