Powershell cmdlet’s from OpenAPI

I am building PS Cmdlet’s from an OpenAPI specifications. Here are pre-reqs.

apt update
wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb
dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb
curl -sL https://deb.nodesource.com/setup_10.x | sudo bash
apt -y install curl dirmngr apt-transport-https lsb-release ca-certificates gcc g++ make nodejs powershell dotnet-sdk-3.1
npm install -g autorest@beta

Exchange/Office 365 add domains from EML files to SPAM block list

The script reads EML files from a directory and parses the header.from and adds the domain to the SPAM block list.

 <#
Stephen

Spam Filter
#>

Connect-ExchangeOnlineShell
$SpamFolder = "C:\Users\Administrator\Desktop\spam"

$DomainsNeverBlock = @('gmail.com','outlook.com','aol.com','yahoo.com')

$DefaultPolicy = Get-HostedContentFilterPolicy -Identity "Default"

$regex = [regex]"header\.from=(.*);"

Get-ChildItem -Path $SpamFolder -File | % {
    #Get-Content $_.FullName
    $from = (Get-Content $_.FullName | Select-String 'header.from')
    if($from -match $regex) {
        $domain = $Matches[1]
        if(-not $DomainsNeverBlock.Contains($domain)) {
            Write-Warning "Blocking Domain $domain"
           $DefaultPolicy | Set-HostedContentFilterPolicy -BlockedSenderDomains @{Add=$domain} -Confirm
        }
    }
}

#Sync Spam Policies
$OnPremPolicy = Get-HostedContentFilterPolicy -Identity "Cloud quarantine for on prem users"
$DefaultPolicy = Get-HostedContentFilterPolicy -Identity "Default"

$OnPremPolicy | Set-HostedContentFilterPolicy -AllowedSenderDomains $DefaultPolicy.AllowedSenderDomains -AllowedSenders $DefaultPolicy.AllowedSenders -BlockedSenders $DefaultPolicy.BlockedSenders -BlockedSenderDomains $DefaultPolicy.BlockedSenderDomains
 

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"
}

Create exchange accounts from Perl

I have not been able to create MS Exchange 2007 accounts from perl, the only method i have found that works is to call the PowerShell command to create the account. Below is an example.

system qq[PowerShell.exe -PSConsoleFile "C:\\Program Files\\Microsoft\\Exchange Server\\Bin\\ExShell.psc1" -Command ". {Enable-Mailbox -Identity fitsuny\\] . $adUser->samaccountname . qq[ -Alias ] . $adUser->samaccountname . qq[ -Database $database] . $value. qq[}"];