PowerShell Ezine, Logon Scripts

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

This Week's Secret

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

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.

Preliminary Instructions to Run PowerShell code:PowerShell copy and paste com objects

If you have not used PowerShell before, here are step-by-step instructions to execute commands.

Method 1 (Quick)

  • Launch PowerShell
  • Copy the code into memory
    (For instance, from Example 1 below)
  • Right-click on the PowerShell symbolPowerShell Scripts
  • Edit --> Paste
  • Press enter to execute the code
  • See screenshot to the right

 

Method 2 (Best)

  • Prepare to run cmdlets with this PowerShell command:
    set-ExecutionPolicy RemoteSigned
  • Copy the code below into a text file.
  • Save the file with a .ps1 extension, for example: printer.ps1
  • In PowerShell, navigate to where you saved printer.ps1
  • Issue this command:
    .\network 
    (dot backslash filename)

 

Example 1: To Add or 'Map' a Network Printer

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

$PrinterPath = "\\Server\PrintShare"
$net = new-Object -com WScript.Network
$net.AddWindowsPrinterConnection($PrinterPath)

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.

ˆ

Example 2:  'Map' a Network Drive

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

$Drive = "Y:"
$Share ="\\server\share"
$Net = New-Object -com WScript.Network
$Net.mapnetworkdrive($Drive,$Share)

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.

See more Microsoft PowerShell tutorials:

PowerShell Home  • Com  • Shell Application  • Active Directory  • QAD Snap-in  • Get-Member

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.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Google

Web  This website

Review of Orion NPMGuy 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.

Download a free trial of the Network Performance Monitor

 

Home Copyright © 1999-2010 Computer Performance LTD All rights reserved

Please report a broken link, or an error.