Dell Quick Connect Account Maintenance

Here is a group of Powershell scripts I wrote for account maintenance utilizing Dell Quick Connect

List accounts older than 2 years to remove HomeDirectory

#two years
$DaysInactive = 730
$lastLogonTimestamp= $srcObj["lastLogonTimestamp"]
$pwdLastSet = $srcObj["pwdLastSet"]

$response = $FALSE
$LastLoginResponse = $FALSE
$pwdLastSetResponse = $FALSE

if($lastLogonTimestamp){
	$LastLogonConverted = [datetime]::FromFileTime([int64]::Parse($lastLogonTimestamp))
 
	if( ((get-date) - $LastLogonConverted ).days  -ge $DaysInactive ){
		$LastLoginResponse = $TRUE
	}
}

if($pwdLastSet){
	$pwdLastSetConverted = [datetime]::FromFileTime([int64]::Parse($pwdLastSet))
	if( ((get-date) - $pwdLastSetConverted ).days  -ge $DaysInactive ){
		$pwdLastSetResponse = $TRUE
   }
}

if($LastLoginResponse -or $pwLastSetResponse){
	$response = $TRUE
}

$response

Based on the OU determine HomeDirectory Location

$ParentPath = $dstObj["distinguishedName"]
$Path = ""

if($ParentPath){
    if($ParentPath.Contains("Admins") -or $ParentPath.Contains("Staff") ){
        $Path = "\\fs-c108-01\staff_home$\"
    }elseif($ParentPath.Contains("Faculty")){
        $Path = "\\fs-c108-04\faculty_home$\"
    }elseif($ParentPath.Contains("Students") -or $ParentPath.Contains("Seminar")){
        $Path = "\\fs-c108-03\student_home$\"
    }else{
        Write-Error "Cannot Find Where to Put Home Directory"
    }

    $Path +=  $dstObj["sAMAccountName"]
}else{
    Write-Error "ParentPath Null"
}

$Path

Create HomeDirectory and Assign Permissions

$ParentPath = $srcObj["distinguishedName"]
$User = $srcObj["sAMAccountName"] 
$DomainUser = "fitsuny\"

$DomainUser += $User

$Path = ""

if($ParentPath){
	if($ParentPath.Contains("Admins") -or $ParentPath.Contains("Staff") ){
		$Path = "\\fs-c108-01\staff_home$\"
	}elseif($ParentPath.Contains("Faculty")){
 		$Path = "\\fs-c108-04\faculty_home$\"
	}elseif($ParentPath.Contains("Students") -or $ParentPath.Contains("Seminar")){
		$Path = "\\fs-c108-03\student_home$\"
	}else{
 		Write-Error "Cannot Find Where to Put Home Directory"
}

$Path +=  $srcObj["sAMAccountName"]
}else{
 Write-Error "ParentPath Null"
}

$HasDir  = Test-Path $Path

if($HasDir){
 Write-Error "Directory Exists"
}else{
 New-Item $Path -type directory
    $acl = Get-Acl $Path
 	$rule = New-Object System.Security.AccessControl.FileSystemAccessRule($DomainUser,"FullControl", "ContainerInherit, ObjectInherit", "None", "Allow")
    $acl.AddAccessRule($rule)

    Set-Acl $Path $acl
}

Remove HomeDirectory

$Path = $dstObj["Path"]
$Folder = $dstObj["Folder"]

$FullPath = $Path
$FullPath += $Folder

#Not Stable
#Remove-Item -Recurse -Force $FullPath

cmd /c rd /s /q $FullPath

$HasDir  = Test-Path $FullPath
if($HasDir){
 Throw "Unable to delete home directory"
}

Deploying Java

We had a hard time getting a clean silent java deploy. There is a bug where the settings file has to exist for a silent install. This is the script i ended up with.

@echo off
rem We have to kill the browsers for a clean uninstall and install
taskkill /f /im iexplore*
taskkill /f /im firefox*
taskkill /f /im chrome*
taskkill /f /im safari*
taskkill /f /im msiexec.exe

rem we need a config file in order for java.
mkdir C:\ProgramData\Oracle\Java
copy /y java.settings.cfg C:\ProgramData\Oracle\Java\

start /wait msiexec /i jre1.8.0_31.msi /qn INSTALL_SILENT=Enable AUTO_UPDATE=Disable WEB_JAVA=Enable EULA=0 /L*V "install.log"
AUTO_UPDATE=Disable
EULA=0
INSTALL_SILENT=Enable

SCCM Container Query

I was asked to help write a script to query all machines that do not have the SCCM agent and produce their email address in a CSV file.

Import-Module 'D:\Program Files\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1' -Verbose

set-location "SITEID:"

$Members = Get-CMDevice -CollectionName "Inactive AD computers with SCCM 2012 Client"

 

 

$Members | foreach-object {

  $Username=$_.UserName

  $Computername=$_.Name

 

  If($Username -ne 'Administrator'){

    If($Username){

        Try{

            $email = (Get-ADUser $Username -properties mail).mail

            New-Object -TypeName PSCustomObject -Property @{   

                Username = $Username   

                Computername = $Computername    

                Email = $email} | Export-Csv -Path "Machines.csv" -NoTypeInformation -Append

        }Catch{

            #who cares

        }

        #write-host $name

     }

   }#end if

}#end for each

Hyper-V VM Trunk

I am running Sophos-UTM in Hyper-V and i needed a TRUNK port to the virtual machine so i can do vlan tagging. Here is the powershell code to implement it.

Example with Place Holders

get-vmnetworkadapter -vmname NAME_OF_VM | where-object -property MacAddress -eq "MAC_ADDR_OF_NIC" | set-vmnetworkadaptervlan -Trunk -AllowedVlanIdList Start-End NativeVlanID DEFAULT_VLAN

Example with Real Data

get-vmnetworkadapter -vmname Sophos-UTM | where-object -property MacAddress -eq "1234567890AB" | set-vmnetworkadaptervlan -Trunk -AllowedVlanIdList 10-20 NativeVlanID 10