Add network printer with PowerShell

This is my PowerShwell translation of my colleague’s VBScript solution for mapping network printers with a script.

<#
.SYNOPSIS
Add a Network Printer connection, optionally making it the default printer.

.DESCRIPTION
Uses a COM object to add a Network Printer, and optionally sets that printer
as the default. If an error is encountered, the exception is written to a
file called Add-NetworkPrinter.err in the current $env:temp directory, and then
the script terminates.

This is my PowerShell translation of my colleague's VBScript solution:
http://blog.uvm.edu/jgm/2014/06/11/parting-scripts-add-a-new-network-printer-and-set-it-as-default/

.PARAMETER PrinterShare
The UNC path to the shared printer.
e.g. \\printers1.campus.ad.uvm.edu\ETS-SAA-SamsungML-3560

.PARAMETER Default
Specifies that the printer will also be set as the default printer for the current user.

.EXAMPLE
Add-NetworkPrinter.ps1 -PrinterShare '\\printers1.campus.ad.uvm.edu\ETS-SAA-SamsungML-3560' -Default

.NOTES
    Script Name: Add-NetworkPrinter.ps1
    Author     : Geoff Duke <Geoffrey.Duke@uvm.edu>
#>

[cmdletbinding()]

Param(
    [Parameter(Mandatory=$true,
        HelpMessage="Enter the UNC path to the network printer")]
    [ValidateNotNullOrEmpty()]
    [string] $PrinterShare,

    [parameter(Mandatory=$false)]
    [switch] $Default
)

Set-PSDebug -Strict

$PSDefaultParameterValues = @{"out-file:Encoding"="ASCII"}

$ws_net = New-Object -COM WScript.Network

write-verbose "Adding connection to $PrinterShare"
try {
    $ws_net.AddWindowsPrinterConnection($PrinterShare)
}
catch {
    $error[0].exception | out-file (join-path $env:temp 'Add-NetworkPrinter.err')
    throw $error[0]
}

write-verbose "Setting the printer as the default"
if ( $Default ) {
    try {
        $ws_net.SetDefaultPrinter($PrinterShare)
    }
    catch {
        $error[0].exception | out-file (join-path $env:temp 'Add-NetworkPrinter.err')
        throw $error[0]
    }
}

# the end

For use with Group Policy, it will probably be helpful to create a simple Set-DefaultPrinter.ps1 script. But that’s just the second stanza from the script above.

Quick tip – Human readable folder sizes in Excel

From the school of There’s Probably a Better Way, But…

I was taking the output of SysInternals’ du.exe utility and wanted to email it to a colleague, but the folder sizes were in KB. Since most of them were many Gigabytes in size, I wanted a quick way to convert them to the appropriate size in GB, MB, or KB.

I copied the du.exe size column and pasted it into Excel. Then I created the following formula:

=IF(E6>(2^20),TEXT(E6/(2^20),"0.0") & " GB","smaller")

…where E6 is the cell reference. Also note that du.exe’s output was in KB, so my test is one order of magnitude smaller than it would need to be if I was formatting a size in bytes. (i.e., 2^20 is 1 MB).

I then nested another similar if() function with a final formatting option to get my final formula:

=IF(E6>(2^20),TEXT(E6/(2^20),"0.0") & " GB",IF(E6>(2^10),TEXT(E6/(2^10),"0.0") & " MB",TEXT(E6,"0.0") & " KB"))

Updating firewall rules with netsh

I needed to adjust the scope of a built-in firewall rule in a couple of servers, restricting the remote IPs to a list of UVM subnets in CIDR notation. The netsh documentation describes the syntax for a list as comma-separated values (no spaces). But I kept getting errors with the command:

netsh advfirewall firewall set rule name="Windows Internet Naming Service (WINS) (NB-Name-UDP-In)" remoteip="10.10.0.0/16,10.11.0.0/16,10.12.0.0/16"

Finally, I actually read the error message:

For ‘set’ commands, the ‘new’ keyword must be present and must not be the last argument provided.

And the related part of the usage text:

Values after the new keyword are updated in the rule.  If there are no values, or keyword new is missing, no changes are made.

One little three-letter keyword was all I needed:

netsh advfirewall firewall set rule name="Windows Internet Naming Service (WINS) (NB-Name-UDP-In)" new remoteip="10.100.0.0/16,10.101.0.0/16,10.102.0.0/16"

/sigh

Remote Desktop Gateway

In order to connect to the Remote Desktop service on centrally managed servers from on or off campus, you need to use the Remote Desktop (RD) Gateway feature of your RD client application.

For the traditional Remote Desktop Client on Windows, you can configure this option by going to the Advanced Options tab, and then click the Settings button.

Remote Desktop Gateway settings

On the RD Gateway Server Settings window that open, select the second option, Use these RD Gateway server settings. The server name is rdgateway.uvm.edu, and chose the Ask for password option.

Click OK, and you should be able to connect using the RD Gateway service. For more information about RD Gateway, see What is a Remote Desktop Gateway server?

Outlook 2013 at UVM

UPDATE! All UVM email accounts have been migrated to Microsoft Exchange, and so this guide is no longer necessary. Outlook 2013 and 2016 setup is very easy, and all functionality is available.

These instructions describe a configuration for connecting to our old email system via IMAP. For connecting Outlook to UVM’s current mail service, see http://www.uvm.edu/techteam/outlook-2016-for-exchange/.

Changes in Outlook 2013

The most significant change in the new version of Outlook is the support of touch-based interfaces. In Outlook 2010, using your finger to scroll a message resulted in selecting a swath of text, as though clicking and dragging with a mouse. Outlook 2013 addresses this, and adds specific support for touch interaction with adjusted menu spacing and a thumb-based button bar that works really well on Windows tablets.

Outlook 2013 - tablet mode
Outlook 2013 – special features available on touch devices include touch scrolling, and a button bar for the right thumb.

You’ll also notice a much cleaner design, where the faux 3D buttons and look have been simplified and flattened. I find myself selecting the dark gray color scheme to restore some visual variety and separation, though.

One change that’s a little irritating is that you can no longer specify the folder into which copies of your sent messages are received; they go into a folder called Sent Items. Instead, I’ve configured Pine, Webmail, and Thunderbird to use Sent Items, too.

I continue to find Outlook a user-friendly, feature-rich email client, and I’m glad to share these instructions with you. Let’s get started.Continue reading →

Software updates user experience

As we transition from Windows Automatic Updates to the more capable System Center Configuration Manager (SCCM or ConfigMgr), the timing and the appearance of alerts and update-related content is changing as well. I think it may be confusing to non-technical users, so I thought I’d document the most common elements alerts and windows

This alert pops-up in the upper right corner of the screen on Windows 8 family systems.
This alert pops-up in the upper right corner of the screen on Windows 8 family systems.
Windows 7-style alert
Windows 7-style alert above the system tray in the lower-right corner.

Continue reading →

Who’s logged into Server 2012?

I’m severely disappointed that Microsoft has removed from Server 2012 the tools required for managing and configuring Remote Desktop for Administration. On previous server editions, I’ve used the Remote Desktop Services Manager to troubleshoot colleagues and delegated administrators’ access issues. We’ve also had situations where the SSL certificate used to identify the server and to encrypt traffic has expired and needed some intervention.

Some discussion on Mark Minasi’s support forum indicates that some of this functionality is returning in Server 2012 R2. In the meantime, and until all our servers get upgraded, I’ll be using a combination of the Task Manager (which shows user sessions),

Displaying user logon sessions in Server 2012 Task manager
Displaying user logon sessions in Server 2012 Task manager

and Process Explorer, which can show the username and start time for process.

Mark Minasi’s talk: The Case for Powershell

With Mark’s permission, I’m mirroring the video of his recent web talk “The Case for PowerShell: Why You Should Learn-PowerShell So You Needn’t Leave-Industry.” Read more about it in his recent newletter.

He shared it via Skydrive, but there are some capacity constraints in place that have been blocking access. (Really, Microsoft‽‽ You’ve suspended Mark Minasi’s SkyDrive account‽)

Hopefully, folks can download it here, or watch it online:

The Case for PowerShell

Thanks, Mark, for your evangelism and expertise.