12 December 2014

Replicate Permissioning

Here is a script I have written that will replicate the permissions between two folders including all subfolders and file permissions. Execute the script and you will be prompted for the source and destination folders. It will first parse through and set folder permissions, followed by parsing through file permissions.

You can download the script here.

   Mick Pletcher  
   Copy Permissions  
   This script will replication permissions for both files and folders using  
   robocopy. Change the $SourceDrive and $DestinationDrive variables to match what  
   you need to replicate.  
 $Errors = 0  
 $SourceDrive = Read-Host 'Enter source folder'  
 $DestinationDrive = Read-Host 'Enter destination folder'  
 #Match capitalization with directories  
 $TempDrive = Get-Item -Path $SourceDrive  
 $SourceDrive = $TempDrive.FullName  
 $TempDrive = Get-Item -Path $DestinationDrive  
 $DestinationDrive = $TempDrive.FullName  
 $SourceFolders = Get-ChildItem $SourceDrive -Recurse | ?{ $_.PSIsContainer }  
 $SourceFiles = Get-ChildItem $SourceDrive -Recurse -Force | where { ! $_.PSIsContainer }  
 $DestinationFolders = Get-ChildItem $DestinationDrive -Recurse | ?{ $_.PSIsContainer }  
 $DestinationFiles = Get-ChildItem $DestinationDrive -Recurse -Force | where { ! $_.PSIsContainer }  
 #Copy permissions for folders  
 $Output = robocopy $SourceDrive $DestinationDrive /ZB /E /LEV:0 /COPY:SOU /XF *.* /R:5 /W:5  
 #Verify Folder Permissions Match  
 Write-Host "Folders:"  
 Write-Host "========"  
 For ($Count=0; $Count -le $SourceFolders.Count; $Count++) {  
      If ($SourceFolders[$Count].FullName -ne $null) {  
           $SourceFolder = $SourceFolders[$Count].FullName  
           $DestinationFolder = $DestinationFolders[$Count].FullName  
           Write-Host $SourceFolder"....." -NoNewline  
           $SourceFolderACL = Get-Acl -Path $SourceFolder  
           $DestinationFolderACL = Get-Acl -Path $DestinationFolder  
           For ($Count1=0; $Count1 -le $SourceFolderACL.Access.Count; $Count1++) {  
                If ($SourceFolderACL.Access[$Count1].FileSystemRights -ne $DestinationFolderACL.Access[$Count1].FileSystemRights) {  
           If ($Errors -eq 0) {  
                Write-Host "Success" -ForegroundColor Yellow  
           } else {  
                Write-Host "Failed" -ForegroundColor Red  
      $Errors = 0  
 $Output = robocopy $SourceDrive $DestinationDrive /ZB /E /LEV:0 /COPY:SOU /XD *.* /R:5 /W:5  
 #Copy permissions for files  
 Write-Host "Files:"  
 Write-Host "======"  
 For ($Count=0; $Count -le $SourceFiles.Count; $Count++) {  
      If ($SourceFiles[$Count].FullName -ne $null) {  
           $SourceFile = $SourceFiles[$Count].FullName  
           $DestinationFile = $DestinationFiles[$Count].FullName  
           Write-Host $SourceFile"....." -NoNewline  
           $SourceFileACL = Get-Acl -Path $SourceFile  
           $DestinationFileACL = Get-Acl -Path $DestinationFile  
           For ($Count1=0; $Count1 -le $SourceFileACL.Access.Count; $Count1++) {  
                If ($SourceFileACL.Access[$Count1].FileSystemRights -ne $DestinationFileACL.Access[$Count1].FileSystemRights) {  
           If ($Errors -eq 0) {  
                Write-Host "Success" -ForegroundColor Yellow  
           } else {  
                Write-Host "Failed" -ForegroundColor Red  
      $Errors = 0  

11 December 2014

Uninstall All Printers

Recently, we upgraded our print servers and needed to reinstall all of the printers. This script will uninstall all printers. I deployed this script out and had it run as the user and a GPO reinstalled the printer with the new network location.

You can download the script from here: UninstallPrinters.ps1

   Uninstall Printers  
   This script will uninstall all printers for a user  
 .PARAMETER <paramName>  
   <Description of script parameter>  
   powershell.exe -executionpolicy bypass -file UninstallPrinters.ps1  
 $Printers = Get-WmiObject Win32_Printer  
 $EXE = $env:windir + "\system32\printui.exe"  
 $PrintUI = "/dn /n "  
 Foreach ($Printer in $Printers) {  
      If ($Printer.ShareName -ne $null) {  
           Write-Host "Uninstall"$Printer.ShareName"....." -NoNewline  
           $Parameters = $PrintUI + [char]34+ $Printer.Name + [char]34  
           $ErrCode = (Start-Process -FilePath $EXE -ArgumentList $Parameters -Wait -Passthru).ExitCode  
           If ($ErrCode -eq 0) {  
                Write-Host "Success" -ForegroundColor Yellow  
           } else {  
                Write-Host "Failed" -ForegroundColor Red  

08 December 2014

Deployment Module

This module is designed to make automating the installation of software a breeze. It also provides logging that makes it easy to check and see if there were errors during an installation. The logging has been designed so that there is an installation log file that records all steps in the installation, an application log file that the installer creates, and finally a build.log file that records if the application was successfully installed. The build.log file provides a goto location for checking to see if all applications are installed while generating a golden image. It sequentially numbers easy application that makes it a snap to go and check if all apps are there.

The application log will give a step-by-step logging of the installation as shown below:

In order to properly install the module, it is suggested that you create the following folder: %programfiles%\windowspowershell\modules\Deployment. Next, copy the .PSD1 and PSM1 files to that folder. That is all that is needed to install the module.

The next step to using the module is the use the template I created called install.ps1. The global variables are in an array called $GlobalVariables. The function InitializeVariables is where you go in and make the appropriate modifications to the $Global:LogFile, $Global:Phase, $Global:Sequence, and $Global:Title. The Sequence is populated only if this is an installation that occurs during an image processs. If it is an image process, change Phase to Software Deployment.

Once the InitializationVariables is populated, you will insert the appropriate functions in the field that reads #<Insert Functions to install/uninstall applications>. That is all that is to this. I have been testing this out for a few months and it has made my life as an SCCM administrator much easier. I hope it does the same for you.

The module includes the following functions:

  • Copy-Files
  • Disable-WindowsFeature
  • Enable-WindowsFeature
  • Exit-PowerShell
  • Get-Architecture
  • Get-OSVersion
  • Import-RegistryFile
  • Install-EXE
  • Install-Fonts
  • Install-MSI
  • Install-MSP
  • Install-MSU
  • New-Directory
  • New-FileShortcut
  • New-LogFile
  • New-StartMenuShortcut
  • New-TaskbarShortcut
  • New-URLShortcut
  • Remove-Directory
  • Remove-DirectoryFromUserProfiles
  • Remove-File
  • Remove-FileFromUserProfiles
  • Remove-HKUKey
  • Remove-RegistryKey
  • Remove-StartMenuShortcut
  • Remove-TaskbarShortcut
  • Remove-Variables
  • Set-ConsoleTitle
  • Set-FolderPermissions
  • Set-Variables
  • Start-Task
  • Stop-Task
  • Uninstall-EXE
  • Uninstall-MSI
  • Uninstall-MSIByGUID
  • Uninstall-MSIByName
  • Wait-ProcessEnd
  • Write-LogFile

You can download the module, installer template, and manifest here:
Here is a Youtube tutorial video on how to implement the module: