• PowerShell script to change default prf imported when Outlook starts up for the first time

    In this previous post, I talk about how we use Office Customization Tool (OCT) and “.prf” files to deploy Office 2010. Continuing with the idea that  I want to know if a person is visiting from another office, I want to be able to switch from our default of “cached mode” to “online mode” for that visitor. I wrote this script with the logic of: IF {visiting from other office}, THEN {JBMURPHY-Install-ChangeDefaultOutlookPRF -CachedMode $false}.

    This script would change where the ImportPRF registry entry points (in this example to a “.prf” file with cached mode disabled)

    function JBMURPHY-Install-ChangeDefaultOutlookPRF {
     PARAM($CachedMode=$TRUE)
    foreach ($PATH in (gci "HKLM:\SOFTWARE\Microsoft\Office\14.0\User Settings\*{*")){
     $ImportPRFRegPATH=$PATH.Name.Replace("HKEY_LOCAL_MACHINE","HKLM:")+"\Create\Software\Microsoft\Office\14.0\Outlook\Setup"
     If (Test-Path $ImportPRFRegPATH){
      $ImportPRFPath=$(get-itemproperty($ImportPRFRegPATH)).ImportPRF
      write-host -NoNewline "`nIportPRF=$ImportPRFPath - "
      if ($CachedMode) {
    	if ($ImportPRFPath -eq "C:\PROGRA~1\MICROS~1\WITHCA~1.PRF") { write-host "Already in CachedMode"}
    	else {write-host "Enabling CachedMode"
    	Set-ItemProperty $ImportPRFRegPATH -Name ImportPRF -Value "C:\PROGRA~1\MICROS~1\WITHCA~1.PRF"
    	write-host "Now IportPRF=$($(get-itemproperty($ImportPRFRegPATH)).ImportPRF)"
    	}
      }
      else {
    	if ($ImportPRFPath -eq "C:\PROGRA~1\MICROS~1\WITHOU~1.PRF") { write-host "CachedMode already turned off"}
    	else {write-host "Turning Off CachedMode"
    	Set-ItemProperty $ImportPRFRegPATH -Name ImportPRF -Value "C:\PROGRA~1\MICROS~1\WITHOU~1.PRF"
    	write-host "Now IportPRF=$($(get-itemproperty($ImportPRFRegPATH)).ImportPRF)"
    	}
      }
     }
    }
    

  • Command to enable windows backup feature on Server 2008 R2

    One liner:

    DISM /online /Enable-Feature /FeatureName:WindowsServerBackup /Quiet /NoRestart


  • A script to loop through a WordPress database and search for text

    We maintain both a WordPress development environment and a production environment. I have written a script that copies a WordPress site to the local machine. In that script I change the GUID (even thought this page says you shouldn’t), siteurl, and home. I change the GUID, because the site has not been publicly accessible, so I believe the warning on the documentation page does not apply.

    One thing that I always wanted to do is make sure that the old (dev) site url is not elsewhere in the database. So I figured out this script below that loops thorough all the tables, and all the columns in the database searching for text, in this case the old site url.

    DATABASENAME="mywpdb"
    SEARCHTEXT="devURL"
    
    for TABLE in $(mysql --batch --skip-column-names -e "use $DATABASENAME;show tables"); do
    for COLUMN in $(mysql --batch --skip-column-names -e "use $DATABASENAME;SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '"$TABLE"' AND TABLE_SCHEMA = '"$DATABASENAME"';"); do
    query="use $DATABASENAME;select substring($COLUMN,1,100) as '"$TABLE":"$COLUMN"' from $TABLE WHERE $COLUMN like '%"$SEARCHTEXT"%';"
    mysql -e "$query"
    done
    done
    

  • Xcopy vs Robocopy

    This is news to me: “Xcopy fails with insufficient memory when the path plus filename is longer than 254 characters”

    Robocopy it is from now on!


  • PowerShell: Two functions to determine if a user is visiting the office

    If you have users in distribution groups associated with each site in your organization, it should be easy to tell if a user is visiting from another office (more on why I want to do this later). First function returns the current site that the user is logging into:

    Function JBMURPHY-AD-GetCurrentSite {
    return [System.DirectoryServices.ActiveDirectory.ActiveDirectorySite]::GetComputerSite().name
    }
    

    The second function loops through all “office groups” and if a user is in that office, it returns the group name

    
    Function JBMURPHY-AD-GetHomeSite {
    Param($username=$(cat env:username))
    foreach ($group in "OfficeGroup1","OfficeGroup2","OfficeGroup3") {
    	foreach ($user in $(Get-ADGroupMember $group)){
    	if ($user.SamAccountName -eq $username) {return $group}
    	}
    }
    }
    
    

    Simple I know, but I want to put these together to do some custom startup activity (maybe via “Active Startup”)


  • PowerShell to verify ACLs (permissions) on a folder

    In my previous post, I showed how to create a new ACL and apply it to a folder. Why apply it to the folder if the folder is already set correctly? I wrote the following function to compare the ACLs of a folder to a desired set of ACLs (either created by hand (lines 3-12) or copied from an existing folder (lines 12-15).

    function JBMURPHY-PERMS-ArePermsCorrect {
            Param([parameter(Mandatory = $true)]$Path,
                  [parameter(Mandatory = $true)]$CorrectACL,
                  [switch]$ShowCorrect)
    
        $folderACLs=get-acl(get-item $Path)
        if ((compare-object $($folderACLs.access) $($CorrectACL.access) -property FileSystemRights,IdentityReference,InheritanceFlags,PropagationFlags).count -gt 0) {
        Write-host "$PATH is INCORRECT"
        return $false
        }
        else {
        if ($ShowCorrect.IsPresent){write-host "$PATH is correct"}
        return $true
        }
    }
    

    If the compare-object command returns nothing, then they are the same, if they are not the same then the items returned will be greater than 0, and the first part of the conditional will be used.


  • PowerShell to assign permission to a folder (not copy inherited permissions)

    In my previous post, I used PowerShell to change the permissions of a top level folder. In that script, I took the folder in question and copied the inherited permissions to it, and then I tinkered it to be what I wanted. I wanted to do something similar, but I wanted a set of permission that differed from the parent. Basically I wanted the folder to have unique permissions. Below is the function to do that:

    function JBMURPHY-PERMS-ClientsFolderReBase {
        Param([parameter(Mandatory = $true)]$Path)
        $correctACLs = New-Object System.Security.AccessControl.DirectorySecurity
        $correctACLs.SetAccessRuleProtection($true,$true)
        $Rule_Admin = New-Object Security.AccessControl.FileSystemAccessRule("BUILTIN\Administrators",@("FullControl"),"ContainerInherit, ObjectInherit","None","Allow")
        $Rule_System = New-Object Security.AccessControl.FileSystemAccessRule("NT AUTHORITY\SYSTEM",@("FullControl"),"ContainerInherit, ObjectInherit","None","Allow")
        $Rule_Users1 = New-Object Security.AccessControl.FileSystemAccessRule("BUILTIN\Users",@("ReadAndExecute", "Synchronize"),"None","None","Allow")
        $Rule_Users2 = New-Object Security.AccessControl.FileSystemAccessRule("BUILTIN\Users",@("Modify, Synchronize"),"ContainerInherit, ObjectInherit","InheritOnly","Allow")
        $correctACLs.AddAccessRule($Rule_Admin)
        $correctACLs.AddAccessRule($Rule_System)
        $correctACLs.AddAccessRule($Rule_Users1)
        $correctACLs.AddAccessRule($Rule_Users2)
        write-host "Changing $Path"
        set-acl $path $correctACLs
    }
    

    In line 3 I create a new ACl, and in line 4, I set the cal to not inherit parent permissions.

    Lines 4-8 are the specific permissions I want to apply (they are addressing the same issue I described here)

    Lines 9-12 add the new perms to the new ACL, and line 14 set the ACL of the folder to the new ACL.

    A little different want o go about this, as I created an ACL from the start.


  • CentOS 6 in a VM – Console resolution

    I can NEVER remember this, and every time I re-install CentOS in a VM I have to go searching.

    If you want the console size to be larger in a vm add vga=791 (for 1024×768) to the end of the kernel line in /etc/grub.conf.

    The VESA values (for linux) are here