I was given another task today to filter out some users in a certain fashion. A coworker asked me to get a few properties and one of the OUs certain users were residing in. Our structure is as follows OU1,OU2,OU3,user accounts. I was asked with the distinguished name to get the OU2 name and if the user object wasn’t residing in the same OU structure as provided, then to filter it out. I came up with the following code to query the results.

Get-ADUser -Filter * -SearchBase "OU=<OU1>,OU=<OU2>,OU=<OU3>,DC=<DC1>,DC=<DC2>,DC=<DC3>,DC=<DC4>" -Properties Name, personalTitle, telephoneNumber, MobilePhone, EmailAddress |
 Select-Object -property Name, personalTitle, telephoneNumber, MobilePhone, EmailAddress, @{label='DistinguishedName';expression={($_.DistinguishedName -replace'(.*.-?OU=Users,OU=)|(,OU.*)') }} |
 Where-Object {($_.DistinguishedName -notlike '*CN=*')}

The first part grabbed the users I wanted where the second line did the initial filter to retrieve just the second OU name with the regex portion stripping away most of the junk I didn’t need. The third line then removed any objects not following the same OU structure.

Work needed me to assist on creating a script for an SCCM package that would help automatically deploy MSIs. While this isn’t a ‘one script does all’ solution, I was able to provide them with a basic template to work with that they could tweak as needed for each package. This will provide the option to install/uninstall silently along with verbose install/uninstall.

Running this guy is pretty simple of course by passing the first argument with the type of installation, and the second with the MSI name.

Param([Parameter(Mandatory=$true,Position=1)]
        [ValidateSet('silentInstall','silentUninstall','verboseInstall', 'verboseUninstall')]
        [string]$InstallScriptParameters,
        [Parameter(Mandatory=$True,Position=2)]
        [string]$MSIFileName)
        

$OSArchitecture = (Get-WmiObject Win32_OperatingSystem).OSArchitecture
$localDir = [System.Environment]::CurrentDirectory.ToString()
$MSIFilePath = "$localDir\SourceFiles\$MSIFileName.msi"


If ($OSArchitecture -eq '64-bit')
    {
    $MSIExec = "$env:windir\SysWOW64\msiexec.exe"
    }
Else
    {
    $MSIExec = "$env:windir\System32\msiexec.exe"
    }



If ($InstallScriptParameters -like "silentInstall")
    {
        $params = @{
            "FilePath" = "$Env:SystemRoot\system32\msiexec.exe"
            "ArgumentList" = @(
                "/package"
                $MSIFilePath
                "/quiet"
            )
            "PassThru" = $true
        }
    }
ElseIf ($InstallScriptParameters -like "silentUninstall")
    {
        $params = @{
            "FilePath" = "$Env:SystemRoot\system32\msiexec.exe"
            "ArgumentList" = @(
                "/uninstall"
                $MSIFilePath
                "/quiet"
            )
            "PassThru" = $true
        }
    }
ElseIf($InstallScriptParameters -like "verboseInstall")
    {
        $params = @{
            "FilePath" = "$Env:SystemRoot\system32\msiexec.exe"
            "ArgumentList" = @(
                "/package"
                $MSIFilePath
            )
            "PassThru" = $true
        }
    }
ElseIf ($InstallScriptParameters -like "verboseUninstall")
    {
        $params = @{
            "FilePath" = "$Env:SystemRoot\system32\msiexec.exe"
            "ArgumentList" = @(
                "/uninstall"
                $MSIFilePath
            )
            "PassThru" = $true
        }
    }
Else 
    {
        Write-Host "No installation parameter was properly assigned."
    }       

Start-Process @params