Poor Man’s Personal PowerShell Logon Script

Those of you familiar with Group Policy Preferences will understand that using vbscript for logon scripts is nearly a thing of the past. While GP Preference doesn’t necessarily replace scripting, it can be said that it’s no longer the “only option” for user preferences.

In IT, sometimes your users need to be a bit more flexible. Here’s a simple approach for mapping customized drive mappings above and beyond what a vbscript logon script or GP preferences provide.

First, I’ve set up several message box functions in a function library I load up in my profile.ps1. One of these is a YesNoCancel message function.

function Show-MsgBoxYNC ($title, $msg) {
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
[Windows.Forms.MessageBox]::Show($msg, $title, [Windows.Forms.MessageBoxButtons]::YesNoCancel, [System.Windows.Forms.MessageBoxIcon]::Question, [System.Windows.Forms.MessageBoxDefaultButton]::Button1, [System.Windows.Forms.MessageBoxOptions]::DefaultDesktopOnly)
}

Next, I have a “logon” folder at the root of my scripts directory with my start-LogonScript.ps1 file. I call the script from within the startup folder in the following manner:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -WindowStyle hidden C:\scripts\POSH\Logon\start-LogonScript.ps1

Here’s the start-LogonScript.ps1 code:

$answer = Show-MsgBoxYNC "Logon Script" "Do you want to run the personal logon script?"
$net = New-Object -ComObject WScript.Network
if ($answer -eq "Yes"){
$net.MapNetworkDrive("G:","\\Server\ShareName","False") | Out-Null
}

A couple of things here. First, this isn’t “fool proof”. I don’t trap any errors here to catch either map drive conflicts, etc. This is just a simple example.

So, why didn’t I use New-PSDrive? When you map a drive from within PowerShell, as soon as you exit the shell, the drive mapping goes away. I want to map the drive so it remains visible within windows.

Since I’ve already had an existing message box function, it’s quite flexible and I could be more creative with error handling.

Advertisements