Logon Script

Windows PowerShell Logon Scripts

PowerShell Script Install-Printers.ps1

#***********************************************************************
# "Install-Printers.ps1"
#
# Written by Aaron Wurthmann (aaron <AT> wurthmann <DOT> com)#

# If you edit please keep my name as an original author and
# keep me apprised of the changes, see email address above.
# This code may not be used for commercial purposes.
# You the executor,runner,user accept all liability.
# This code comes with ABSOLUTELY NO WARRANTY.
# You may redistribute copies of the code under the terms of the GPL v2.
# -----------------------------------------------------------------------
# 2010.03.01 ver 1.2
#
# Summary:
# Enumerates Print shares on Print Server and installs them.
#
# Known Limitations:
# In its present form the script will work only with a single site and
# a single print server. You'll have to edit it to do more if need be.
#************************************************************************

#Function Section - Dont Touch, unless you Know what you are doing.
function Test-Port{
    Param([string]$srv="localhost",$port=135,$timeout=300)
    $ErrorActionPreference = "SilentlyContinue"
    $tcpclient = new-Object system.Net.Sockets.TcpClient
    $iar = $tcpclient.BeginConnect($srv,$port,$null,$null)
    $wait = $iar.AsyncWaitHandle.WaitOne($timeout,$false)
    if(!$wait)
    {
        $tcpclient.Close()
        Return $false
    }
    else
    {
        $error.Clear()
        $tcpclient.EndConnect($iar) | out-Null
        Return $true
        $tcpclient.Close()
    }
}

function Add-Printer{
    Param([string]$server="",$printer="")
    if (!$server -or !$printer){
        exit
    }
    if (!$net){
        $net = new-Object -com WScript.Network
    }
    $PrinterPath = "\" + $server + "" + $printer
    write-host "Attempting to Silent Install:" $PrinterPath
    $net.AddWindowsPrinterConnection($PrinterPath)
}
# End Function Section

# Print Server
[string]$strPrintserver = "PRINTSERVER"

# Exempt Sites/DCs - Add DCs here for sites you want to skip
$DCs=@(
    '\DC101',
    '\DC02';
)

[string]$strLogonserver = ($env:logonserver)
[string]$strComputername = ("\" + $env:computername)
$net = new-Object -com WScript.Network

if ($DCs -contain $strLogonserver) {
    exit
}

if ($strLogonserver -eq $strComputername -or $strComputername -eq "\" + $strPrintserver){
    exit
}

[string]$portResult=Test-Port $strPrintserver 135 1
if($portResult -eq $false){
    exit
}

$netviewResult=@(net view \$strPrintserver | Select-String Print)
$printerShares = $netviewResult |foreach-object { $_.toString().Split('  +')[0] }

$errorActionPreference="SilentlyContinue"

$printerShares | ForEach-Object {
    Add-Printer $strPrintserver $_
}

PowerShell Script Install-WindowsUpdates.ps1

#***********************************************************************
# "Install-WindowsUpdates.ps1"
#
# Re-posted by Aaron Wurthmann (aaron <AT> wurthmann <DOT> com)
#
# If you edit please keep my name as an author and
# keep me apprised of the changes, see email address above.
# This code may not be used for commercial purposes.
# You the executor,runner,user accept all liability.
# This code comes with ABSOLUTELY NO WARRANTY.
# You may redistribute copies of the code under the terms of the GPL v2.
# -----------------------------------------------------------------------
# 2010.03.01 ver 1.1
#
# Summary:
# Gets and installed Windows Updates
#
# Background:
# A friend of mine posted this code into a chat Window one day to aid me
# with a project I was working on. Converting my batch scripts to PowerShell
# He couldn't remember where he got the code from so as a result I can't give
# the original author the credit they deserve. I made some very minor edits
# in order to change the code/script to what I use it for. Mainly a module
# to my Windows Logon Script.
#************************************************************************

clear-host
Write-host "Starting Update Process..." -foregroundcolor blue
Write-host ""
$UpdateSession = New-Object -com Microsoft.Update.Session
$UpdateSearcher = $UpdateSession.CreateupdateSearcher()
$SearchResult =  $UpdateSearcher.Search("IsAssigned=1 and IsHidden=0 and IsInstalled=0")
$UpdateLowNumber = 0
$UpdateHighNumber = 1
$NumberofUpdates = $searchResult.Updates.Count
while ($UpdateHighNumber -le $NumberofUpdates) {
$UpdatesToDownload = New-Object -com Microsoft.Update.UpdateColl
$Update = $searchResult.Updates.Item($UpdateLowNumber)
if ($Update.EulaAccepted -eq 0) {$Update.AcceptEula()}
[void]$UpdatesToDownload.Add($Update)
$Downloader = $UpdateSession.CreateUpdateDownloader()
$Downloader.Updates = $UpdatesToDownload
[void]$Downloader.Download()
$UpdatesToInstall = New-Object -com Microsoft.Update.UpdateColl
[void]$UpdatesToInstall.Add($Update)
$Title = $update.Title
$KBArticleIDs = $update.KBArticleIDs
$SecurityBulletinIDs = $update.SecurityBulletinIDs
$MsrcSeverity = $update.MsrcSeverity
$LastDeploymentChangeTime = $update.LastDeploymentChangeTime
$MoreInfoUrls = $update.MoreInfoUrls
Write-host "Installing Update $UpdateHighNumber of $NumberofUpdates"
Write-host "Title: $Title"
if ($KBArticleIDs -ne "") {Write-host "KBID: $KBArticleIDs"}
if ($SecurityBulletinIDs -ne "") {write-host "Security Bulletin: $SecurityBulletinIDs"}
if ($MsrcSeverity -eq "Critical") {Write-host "Rating: $MsrcSeverity" -foregroundcolor red} else {Write-host "Rating: $MsrcSeverity"}
if ($LastDeploymentChangeTime -ne "") {Write-host "Dated: $LastDeploymentChangeTime"}
if ($MoreInfoUrls -ne "") {Write-host "$MoreInfoUrls"}
$Installer = $UpdateSession.CreateUpdateInstaller()
$Installer.Updates = $UpdatesToInstall
$InstallationResult = $Installer.Install()
Write-host "--------------------------------------------"
if ($InstallationResult.ResultCode -eq "2") {Write-host "  Installation Succeeded" -foregroundcolor green}  else {Write-host "  INSTALLATION FAILED, check event log for details" -foregroundcolor red}
if ($InstallationResult.RebootRequired -eq "False") {Write-host "  Reboot not required" -foregroundcolor green} else {Write-host "  REBOOT REQUIRED" -foregroundcolor red}
Write-host "--------------------------------------------"
Write-host ""
Write-host ""
$Title = ""
$KBArticleIDs =  ""
$SecurityBulletinIDs =  ""
$MsrcSeverity =  ""
$LastDeploymentChangeTime =  ""
$MoreInfoUrls =  ""
$UpdateLowNumber = $UpdateLowNumber + 1
$UpdateHighNumber = $UpdateHighNumber + 1
if ($ProgressValue -lt $NumberofUpdates) {$ProgressValue = $ProgressValue + 1}
}
$ComputerStatus = New-Object -com Microsoft.Update.SystemInfo
 if ($ComputerStatus.RebootRequired -eq 1) {Write-host "A Reboot is Required"}

PowerShell Script Register-OutlookProfile.ps1

Project Objective:
Convert existing Batch logon script, Configure-Outlook.bat, to PowerShell, while doing so improve upon the proper Outlook version enumeration with a registry check.

Problem to be solved:
Current script has been written in Batch. Getting Batch logon scripts to run the same way they ran in Windows XP, on Windows Vista and Windows 7 has become a game of hit and miss. This script, in its Batch form, runs just fine. So re-writing it in PowerShell is merely for academic and consistency purposes.

Suggestions:
None.. Just re-write it and learn.

Future Versions:
The current version is not passing the variables to the environment that is launched at the end of the script. I am sure this is an error on my part. I just need to sit down and figure out what I am doing wrong.

# WARNING WORK IN PROGRESS
# This script is currently not working as it should.
# I am looking into why and debugging. It is based on a
# working batch version, its just a matter of getting it
# to work in PowerShell
# – Aaron Wurthmann

function Test-ServerOS {
    if ($OS.Caption -like "*Server*"){
        exit
    }
}

function Test-Profile {
    if ($OS.Version -gt 6.0){
        $strProfileDir="$env:USERPROFILEAppDataRoamingMicrosoftOutlook"
    }
    Else {
        $strProfileDir="$env:USERPROFILELocal SettingsApplication DataMicrosoftOutlook"
    }
    $ProfileResult=test-path $strProfileDir
    if ($ProfileResult=$true){
        exit
    }
}

function Get-Outlook {
    $regOutlook=Get-Itemproperty -path "HKLM:SoftwareMicrosoftWindowsCurrentversionApp Pathsoutlook.exe"
    $getOutlook=$regOutlook.path +'outlook.exe'
    $outlook=Get-Item $getOutlook
}

function Test-Config {
    $ConfigResult=test-path $config
    if ($ConfigResult=$false){
        exit
    }
}

$env:ExchServer="EXCHANGE.DOMAIN.EXT"
$env:OWAServer="OWA.DOMAIN.EXT"
$env:MailProfile="Outlook"
$env:ExchProfile="EXCHANGE-PROFILE.PRF"
$config="$env:logonservernetlogonmodulesexchange-profiles$env:ExchProfile"

$OS=Get-WmiObject -Class win32_OperatingSystem -namespace "rootCIMV2"
Test-ServerOS
Test-Profile
Get-Outlook
Test-Config
& $outlook /importprf $config