Some articles are posted as Images, Please use Computers to go through them for best experience. For phone users, switch to Web Version

AD Script - Disable ID's and then Move to different OU

 <#Created By - Abhishek Bansal

Use - Disable Users mentioned in InputUsers.csv and then move them onto a different OU containing all the disabled Users".

Prerquistes - Copy entire code, save it in a file with .ps1 extension. On same location, create another file named "InputUsers.csv". 

This csv will be containing user details in columns like Samaccountname, DistinguishedName.

#>

Start-Transcript -Path ./Transcript.txt -Append

$disabledOU_DN = Read-Host "Provide DN of Disabled OU = "  #***Provide destination OU of disabled Users***#

$userdetails = Import-Csv .\InputUsers.csv

$line = 0 #

$linecount = $userdetails.Count #

$pct = 0 #


foreach($user in $userdetails)

{

$Error.Clear()

$line = $line + 1 #

$pct = $line/$linecount*100 #

Write-Progress -Activity "Checking on $server" -Status "$line out of $linecount" -PercentComplete $pct #


    try{

   Write-Host -ForegroundColor Cyan "`nDisabling User account $($User.SamAccountName)"

   Disable-ADAccount -Identity $user.SamAccountName-PassThru -ErrorAction Stop | Export-Csv ./DisableUsersResult.csv -NoTypeInformation -Append

   Write-Host -ForegroundColor Green "Succesfully disable $($User.SamAccountName) ID."

     Write-Host -ForegroundColor Cyan "`nMoving User account $($User.SamAccountName) to $disabledOU_DN now.."

     Move-ADObject -Identity $user.DistinguishedName -TargetPath $disabledOU_DN -PassThru -ErrorAction Stop | Export-Csv ./MoveUserResult.csv -NoTypeInformation -Append

     Write-Host -ForegroundColor Green "Succesfully moved $($User.SamAccountName) ID."

    }

    catch{

    Write-Host -ForegroundColor Red "Error handling $($User.SamAccountName) ID."

    $user | Select-Object -Property @{n="User ID";e={$user.SamAccountName}},@{n="Error";e={$Error.exception.message}} | Export-Csv ./DisableUsersErrorlogs.csv -NoTypeInformation -Append

    }


}


Stop-Transcript

Windows Script - Test if Path input by User exist or not on Remote Servers

 <#Created By - Abhishek Bansal

Use - Script that runs on Servers mentioned in Input.txt file, checks if path input by User while executing this script exists or not on the server.

Prerquistes - Copy entire code, save it in a file with .ps1 extension. On same location, create another file named "Input.txt" and mention all the servers on which this script will run.

Output - TestaccessPath.csv file containing details. Column name Status shows if Path exist or not.

#>


$servers = Get-Content .\Input.txt

$line = 0 #

$linecount = $servers.Count #

$pct = 0 #

$path = Read-Host "Enter a path = "

$remotepath = $path -replace '^([A-Z]):', '$1$'

foreach($server in $servers)

{

$Error.Clear()

$line = $line + 1 #

$pct = $line/$linecount*100 #

Write-Progress -Activity "Checking on $server" -Status "$line out of $linecount" -PercentComplete $pct #

Write-Host -ForegroundColor Cyan "Traversing to $server."#

$result = Test-Path "\\$server\$remotepath" -ErrorAction Stop

    if($result -eq "True")

    {

    Write-Host -ForegroundColor Green "$($path) exists on $server`n"

    $server | Select-Object -Property @{n="Hostname";e={$server}},@{n="Path";e={$path}},@{n="Status";e={"Yes"}} | Export-Csv ./TestaccessPath.csv -NoTypeInformation -Append

    }

    else

    {

    Write-Host -ForegroundColor Red "$($path) doesn't exist on $server`n"

    $server | Select-Object -Property @{n="Hostname";e={$server}},@{n="Path";e={$path}},@{n="Status";e={"No"}} | Export-Csv ./TestaccessPath.csv -NoTypeInformation -Append

    }

}

Windows Script - Check Unallocated free space right after Recovery Partition

 <#Created By - Abhishek Bansal

Use - Script that runs on Servers mentioned in Input.txt file, fetches details of Servers having Unallocated free space right after Recovery Partition.

Prerquistes - Copy entire code, save it in a file with .ps1 extension. On same location, create another file named "Input.txt" and mention all the servers against which this script will run.

Output - Two Output files, Output.csv containing details and Errorlogs.csv containing errors if any occued during execution of script.

#>

$servers = Get-Content .\Input.txt

$line = 0

$linecount = $servers.Count

$pct = 0

foreach($server in $servers)

{

$line = $line + 1

$pct = $line/$linecount*100

$Error.Clear()

Write-Progress -Activity "Checking $server" -Status "$line out of $linecount" -PercentComplete $pct

Write-Host -ForegroundColor Green "Exporting results for $server."

try{

$OSDisk = Invoke-Command -ComputerName $server -ErrorAction Stop -ScriptBlock{Get-Disk | ?{$_.bootfromdisk -eq $true}}

$partitions = Invoke-Command -ComputerName $server -ScriptBlock{Get-Partition -DiskNumber $using:OSDisk.Number | Sort-Object Offset} -ErrorAction Stop 

$recoverypartition =  $partitions | Where-Object {$_.Type -eq 'Recovery'} | Sort-Object Offset -Descending | Select-Object -First 1

    if ($recoverypartition) {

        $recoveryEnd = $($recoverypartition.Offset + $recoverypartition.Size)

        $lastoffset = $partitions | Select-Object -Last 1

       if($($recoveryEnd /1GB) -lt $($lastoffset.Offset /1GB))

       {

       $server | Select-Object -Property @{n="Hostname";e={$server}},@{n="Status";e={"Recovery Partition is not at the end of volume, hence skipping checks"}},@{n="Size (GB)";e={"NA"}} | Export-Csv ./Output.csv -NoTypeInformation -Append

       }

      else

      {

        if($($OSDisk.Size /1GB) -gt $($recoveryEnd /1GB))

        {

        $server | Select-Object -Property @{n="Hostname";e={$server}},@{n="Status";e={"Yes, Unallocated space exists."}},@{n="Size (GB)";e={$($OSDisk.Size /1GB - $($recoveryEnd /1GB))}}  | Export-Csv ./Output.csv -NoTypeInformation -Append

        }

        else

        {

        $server | Select-Object -Property @{n="Hostname";e={$server}},@{n="Status";e={"No, Unallocated space exists."}},@{n="Size (GB)";e={"NA"}} | Export-Csv ./Output.csv -NoTypeInformation -Append

        }

    }}

    else

    {

    $server | Select-Object -Property @{n="Hostname";e={$server}},@{n="Status";e={"No Recovery Partition exists."}},@{n="Size (GB)";e={"NA"}} | Export-Csv ./Output.csv -NoTypeInformation -Append

       }

}

catch{

$server | Select-Object -Property @{n="Hostname";e={$server}},@{n="Error";e={$Error.exception.Message}} | Export-Csv ./Errorlogs.csv -NoTypeInformation -Append

    }

}