Categories
Uncategorized

Screen Saver per AD department

Requirement to set different screen saver for customer depending on AD department.
Create Central accessible folder locations, example netlogon folder.
Create Powershell script to determine department from ADSI for logged in user.
Create User GPO to copy script to folder on Machine and set a scheduled task to run the script at any user logon (Delayed if required).
Then Set Security Group filtering and link GPO to users OU and enable GPO
Then just update the central department folders with new images when required
 <# 
 .SYNOPSIS 
   Automate Set Screen Saver as Department
  
 .DESCRIPTION 
   Gets logged on user with CIMInstance
   Gets SID for user to locate reg Hive and sets the ScreenShow speed
   Checks AD for Logged in User using ADSI and gets department name
   Each Department has folder in central location which contains screen saver images
   If there is a new update to folder then apply
   If department changes then apply new department screen savers
  
 .PARAMETER  
     None
  
 .INPUTS 
   None
  
 .OUTPUTS 
   None
  
 .NOTES 
   Version:        1.0 
   Author:         Peter Charleston
   Creation Date:  20/12/2019
   Purpose/Change: Task to automate dynamic screen saver per AD department 
    
 .EXAMPLE 
   None
 #> 

################ Edit Added on 14/01/2020 to test update process ####################

$CurrentImageFolderAll = "C:\Admin\Screen\*"
$CurrentImageFolder = "C:\Admin\Screen"
$NetworkSaverLocation = "\\Domain\NETLOGON\ScreenSavers"
$date = (Get-Date).ToString("yyyy-MM-dd_HHmmss")
$userExplore = Get-CimInstance Win32_Process -Filter "Name='explorer.exe'" | Invoke-CimMethod -MethodName GetOwner
$user = $userExplore[0]
$fltr = "Name='{0}' AND Domain='{1}'" -f $user.User, $user.Domain
$sid = (Get-CimInstance Win32_UserAccount -Filter $fltr).SID
$SlideShowSpeed = "Registry::HKEY_USERS\$sid\Software\Microsoft\Windows Photo Viewer\Slideshow\Screensaver"
#Test to see if Reg folder exists and if not create
if(Test-Path -Path $SlideShowSpeed){
Set-ItemProperty -Path $SlideShowSpeed -Name Speed -Value 0


}#If Folder already exists then create new key and value
else{
New-Item -Path "Registry::HKEY_USERS\$sid\Software\Microsoft" -name "Windows Photo Viewer"
New-Item -Path "Registry::HKEY_USERS\$sid\Software\Microsoft" -name "Windows Photo Viewer\Slideshow"
New-Item -Path "Registry::HKEY_USERS\$sid\Software\Microsoft" -name "Windows Photo Viewer\Slideshow\Screensaver"
New-ItemProperty -Path "Registry::HKEY_USERS\$sid\Software\Microsoft\Windows Photo Viewer\Slideshow\Screensaver" -Name "Speed" -Value 0


}#Else create new Reg items

$UserAccount = $User.User
#$UserAccount = (Get-CimInstance win32_LoggedOnUser).Antecedent.Name
$Department = $null
$DepartmentPathName = $null
#Get SID Poweshell 7 
#([System.Security.Principal.NTAccount]('{0}\{1}' -f $user.Domain, $user.User)).Translate([System.Security.Principal.SecurityIdentifier]) 

##########Function##########
function ScreenFileCheck {
    param(
    #[string]$DepartmentName,
    [string]$DepartmentPath

    )

 
    Remove-Item -Path $CurrentImageFolderAll -Force -ErrorAction SilentlyContinue

    Copy-Item -Path $DepartmentPath  -Destination $CurrentImageFolder
}#ScreenFileCheck Function



############Get Department######################
$Department = ([ADSISearcher]"(sAMAccountName=$UserAccount)").FindOne().Properties['department']
#$Department = "People Department"




function UpdateScreenSaver {
param (

    [String]$Department

)
    $location = $Department
    $DepartmentPathName = "$NetworkSaverLocation\$Location\*"
    $DepartmentPathNameGetItems = "$NetworkSaverLocation\$Location"
    #Add Condition to determine if there are new files loaded
    $GetNetworkFolderlastWrite = Get-Item $DepartmentPathNameGetItems | select LastWriteTime
    $GetLocalFolderLastWrite = Get-Item $CurrentImageFolder | select LastWriteTime
    if ($GetNetworkFolderlastWrite.LastWriteTime -gt $GetLocalFolderLastWrite.LastWriteTime) {
        Write-Output "New Files found @ $DepartmentPathName. Copying over new files"
        ScreenFileCheck -DepartmentName $Department -DepartmentPath $DepartmentPathName
    }#If There are new files on Network location then move to local
    else {

        write-host "There are no new files on $DepartmentPathName" -ForegroundColor Red
        write-host "Checking to see if files in $CurrentImageFolder contain the $Location Savers" -ForegroundColor Yellow
        
        #Get count of number of files @ network location and write-host
        #$NetworkfileCount = (Get-item $DepartmentPathName).count
        #write-host "Current number of files at $DepartmentPathNameGetItems is $NetworkfileCount"
        $ListOfFileSavers = (Get-ChildItem -Path $CurrentImageFolder).Name
        #Get count of number of files @ local locatoin and write-host
        #$LocalFileCount = (Get-Item $CurrentImageFolderAll).count
        #write-host "Current number of files at $CurrentImageFolder is $LocalFileCount"

        #Compare folders for file names
        try{
        $FileNameCheck = Compare-Object -ReferenceObject ((Get-Childitem $CurrentImageFolderAll).Name) -DifferenceObject ((Get-Childitem $DepartmentPathName).Name) -ErrorAction Stop

            If($FileNameCheck){
        $DifferrenceInFile = $True

        }#If there is a differrence in FileNames check
        else{
         $DifferrenceInFile = $False

        }#If thre is no differrence in FileNames check

        }#Try to compare objects
        #catch [System.Management.Automation.ParameterBindingValidationException] {
       catch{
       if(($PSItem.Exception -like '*ReferenceObject*') -or ($PSItem.Exception -like '*DifferenceObject*')){
       If($PSItem.Exception -like '*ReferenceObject*'){

        write-host "$CurrentImageFolder is empty" -ForegroundColor Red
        }#if Ref Object
        If($PSItem.Exception -like '*DifferenceObject*'){

        write-host "$DepartmentPathName is empty" -ForegroundColor Red
        }#if Ref Object


        $DifferrenceInFile = $True

        }#If 
        }#catch exception

    

        #if (($ListOfFileSavers -like "$Department*") -and ($ListOfFileSavers.Count -eq $NetworkFileCount))  {


        ############Add just check if DifferrenceInFile######## this will tell us if there are any differrences in FileNames and determine if files are correct - Dont need to check fileName -like
      #   if (($ListOfFileSavers -like "$Department*") -and !($DifferrenceInFile))  {
      if(!($DifferrenceInFile)){

            Write-host "Correct files in $currentImageFolder" -ForegroundColor Green
        }#If There is no differrence in Files

        #}#If List of files contatin Place
        else {

            write-host "Incorrect files located in $CurrentImageFolder... Moving files from $DepartmentPathName to $CurrentImageFolder" -ForegroundColor Green
            ScreenFileCheck -DepartmentPath $DepartmentPathName

        }#Else incorrect files 
        
    }#Else there are no new files on Network folder

    }#Function UpdateScreenSaver

UpdateScreenSaver -Department $Department





By Peter Charleston

Powershell, automation enthusiast.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s