Skip to content

Instantly share code, notes, and snippets.

@tabs-not-spaces
Created January 29, 2019 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tabs-not-spaces/9f72041cbb1b929b98ceca381c5de4e8 to your computer and use it in GitHub Desktop.
Save tabs-not-spaces/9f72041cbb1b929b98ceca381c5de4e8 to your computer and use it in GitHub Desktop.
logon script
<#
.SYNOPSIS
Logon script for the EUC Environment.
.DESCRIPTION
Configures the following things:
- Map Network Drives if the UNC path is connectable (must be onPrem)
.NOTES
Author: Ben Reader
Company:
CreationDate: 22/08/2018
#>
#region Config
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$client = "companyName"
$logPath = "$ENV:ProgramData\$client\Logs"
$logFile = "$logPath\LogonScript.log"
#$startScriptPath = "$ENV:ProgramData\$client\Scripts\StartMenu\startmenu.xml"
$fileServer = "filserver.company.local"
$uName = $ENV:UserName
#endregion
#region Default Network Drives
<# $baseDrives = @(
[PSCustomObject]@{
driveLetter = "N"
uncPath = "\\filserver.company.local\NAIF.$"
},
[PSCustomObject]@{
driveLetter = "O"
uncPath = "\\filserver.company.local\Scandoc"
},
[PSCustomObject]@{
driveLetter = "T"
uncPath = "\\filserver.company.local\NAIF_Travel.$"
}
) #>
#endregion
#region Security Based Drives
#endregion
#region Ad-Hoc Drives
<# $adHocDrives = @(
[PSCustomObject]@{
usrName = "UserName"
drives = @(
[PSCustomObject]@{
driveLetter = "X"
uncPath = '\\SERVER\PATH'
}
)
}
) #>
#endregion
#region Printer Config
$printers = @(
"FollowMe - Colour",
"FollowMe - BW"
)
#endregion
#region functions
Function Set-DriveMapping {
<#
.SYNOPSIS
Map a network drive. Also checks for network connectivity before attempting the mapping.
.PARAMETER driveLetter
The drive letter to be assigned
.PARAMETER uncPath
Full FQDN path of the network share
.EXAMPLE
Set-DriveMapping -driveLetter X -uncPath "\\server1.contoso.com\NetworkShareX"
#>
[cmdletbinding(SupportsShouldProcess = $True)]
param (
[string]$driveLetter,
[string]$uncPath
)
$server = ($uncPath -split "\\")[2]
$netConn = Test-NetConnection -ComputerName $server | select-object PingSucceeded, NameResolutionSucceeded
if (($netConn.PingSucceeded) -and ($netConn.NameResolutionSucceeded)) {
if (Test-Path -Path $uncPath) {
if (Get-PSDrive | where-object {$_.Name -eq $driveLetter}) {
if (Get-PSDrive | where-object {($_.Name -eq $driveLetter) -and ($_.DisplayRoot -eq $uncPath)}) {
Write-Host "$($driveLetter): Currently Mapped - will remap now.." -ForegroundColor Green
}
else {
Write-Host "$($driveLetter): Mapped incorrectly. Removing and remapping.." -ForegroundColor Yellow
}
if ($WhatIfPreference) {
Write-Host "would remove drive: $driveLetter"
Write-Host "would map drive $driveLetter with uncPath $uncPath"
}
else {
#Remove-PSDrive -Name $driveLetter -PSProvider FileSystem -Scope Global -Force -Verbose
#New-PSDrive -Name $driveLetter -PSProvider FileSystem -Root $uncPath -Scope Global -Persist -Verbose
}
}
else {
if ($WhatIfPreference) {
Write-Host "would map drive $driveLetter with uncPath $uncPath"
}
else {
Write-Host "$($driveLetter): not found - mapping to $uncPath" -ForegroundColor Green
New-PSDrive -Name $driveLetter -PSProvider FileSystem -Root $uncPath -Scope Global -Persist -Verbose
}
}
}
else {
Write-Host "Getting a response from $($server) `nCan't access UNC Path: $($uncPath)." -ForegroundColor Red
if (Get-PSDrive | Where-Object {($_.Name -eq $driveLetter) -and ($_.DisplayRoot -eq $uncPath)}) {
if ($WhatIfPreference) {
Write-Host "would remove drive: $driveLetter"
}
else {
#Remove-PSDrive -Name $driveLetter -PSProvider FileSystem -Scope Global -Force -Verbose
}
}
}
}
else {
Write-Host "Could not reach $($uncPath): Going to check if $($driveLetter) is already mapped and remove it" -ForegroundColor Red
if (Get-PSDrive | Where-Object {($_.Name -eq $driveLetter) -and ($_.DisplayRoot -eq $uncPath)}) {
if ($WhatIfPreference) {
Write-Host "would remove drive: $driveLetter"
}
else {
#Remove-PSDrive -Name $driveLetter -PSProvider FileSystem -Scope Global -Force -Verbose
}
}
}
}
Function Set-LocalPrinters {
<#
.SYNOPSIS
Installs network printer to local machine.
.PARAMETER Server
FQDN or IP Address of print server
.PARAMETER printerName
Name of printer to be installed
#>
param (
[string]$server,
$printerName
)
$netConn = Test-NetConnection -ComputerName $Server | select-object PingSucceeded, NameResolutionSucceeded
if (($netconn.PingSucceeded) -and ($netConn.NameResolutionSucceeded)) {
if ($printerName.count -gt 1) {
write-host "Multiple Printers to install.." -ForegroundColor Green
$printerName | ForEach-Object {
$printerPath = $null
$PrinterPath = "\\$($server)\$($_)"
if (Get-Printer -Name "$printerPath" -ErrorAction SilentlyContinue) {
Write-Host "Printer $printerPath already installed" -ForegroundColor Green
}
else {
Write-Host "Installing $printerPath" -ForegroundColor Green
Invoke-Expression -Command "C:\Windows\System32\RUNDLL32.EXE printui.dll,PrintUIEntry /in /n'$($printerPath)' /q"
return $true
}
}
}
else {
write-host "Single Printer to install.." -ForegroundColor Green
$printerPath = $null
$PrinterPath = "\\$($server)\$($printerName)"
if (Get-Printer -Name "$printerPath" -ErrorAction SilentlyContinue) {
Write-Host "Printer $printerPath already installed" -ForegroundColor Green
}
else {
Write-Host "Installing $printerPath" -ForegroundColor Green
Invoke-Expression -Command "C:\Windows\System32\RUNDLL32.EXE printui.dll,PrintUIEntry /in /n'$($printerPath)' /q"
return $true
}
}
}
else {
#offline. cant add printers
Write-Host "Print server not pingable. Printers will not be installed" -ForegroundColor Red
}
}
#endregion
#region logging
if (!(Test-Path -Path $logPath)) {
New-Item -Path $logPath -ItemType Directory -Force
}
Start-Transcript -Path $logFile -Force
#endregion
#region Required Modules
if (!(Get-Module -Name CredentialManager -ListAvailable)) {
Install-Module -Name CredentialManager -Scope CurrentUser -Force -Verbose
Import-Module -Name CredentialManager
}
#endregion
#region logon script
try {
#region Initialization
$sw = New-Object System.Diagnostics.Stopwatch
$sw.Start()
$timeSpan = New-TimeSpan -Minutes 2
Write-Host "Hello $uName.." -ForegroundColor Green
Write-Host "Just going to make sure you have access to $client resources before we begin.." -ForegroundColor Green
Write-Host "Testing connectivity to $fileServer..`ngoing to try for the next few minutes, hold tight.." -ForegroundColor Yellow
while ((!(Test-Connection -ComputerName $fileServer -Count 2 -Quiet)) -and ($sw.ElapsedMilliseconds -lt $timeSpan.TotalMilliseconds)) {
start-sleep -Seconds 2
}
#endregion
#region credentials
if (!(Get-StoredCredential -Target $printServer -Type DomainPassword -AsCredentialObject -ErrorAction SilentlyContinue)) {
Write-Host "Credentials not found - let's add the defaults"
New-StoredCredential -UserName "$client\$env:USERNAME" -Password "P@ssw0rd" -Target $printServer -Type DomainPassword -Persist Enterprise
}
if (!(Test-Path -Path "$printServer\C`$")) {
Write-Host "Credential found locally, but cannot reach print server with details.."
$cred = Get-Credential -Message "Please enter $client account password to add printers to $env:COMPUTERNAME.." -UserName "$client\$env:USERNAME"
New-StoredCredential -UserName $cred.UserName -SecurePassword $cred.Password -Target $printServer -Type DomainPassword -Persist Enterprise
}
#endregion
#region Printers
try {
Write-Host "Installing Printers.." -ForegroundColor Green
$printers = Set-LocalPrinters -server "printsvr.company.local" -printerName $printers
}
catch {
}
#endregion
#region StartMenu Polish
if (Test-Path $startScriptPath) {
Write-Host "Start menu config found.. Going to update modification date to reinforce start menu config.." -ForegroundColor Yellow
Get-ChildItem $startScriptPath | ForEach-Object {
$_.LastWriteTime = (Get-Date)
}
}
#endregion
}
catch {
$errorOccurred = $_.Exception.Message
}
finally {
if ($errorOccurred) {
Write-Warning "Logon Script completed with errors."
Stop-Transcript
Throw $errorOccurred
}
else {
Write-Warning "Logon Script completed successfully."
Stop-Transcript
}
}
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment