Remove All Permission for a Folder

function Remove-All-Permissions {
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]$Path
)
$acl = Get-Acl $Path
$isProtected = $true
$preserveInheritance = $false
$acl.SetAccessRuleProtection($isProtected, $preserveInheritance)
Set-Acl -Path $Path -AclObject $acl
$allusers = (Get-Acl $Path).Access
foreach($val in $allusers)
{
Write-Host $val.IdentityReference.Value ” ” $val.FileSystemRights
Set-TargetResource -Path $Path -Account $val.IdentityReference.Value -Rights $val.FileSystemRights -Ensure “Absent”
}
}
function Set-TargetResource {
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]$Path,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]$Account,
[Parameter()]
[ValidateNotNullOrEmpty()]
[String]$Rights,
[Parameter()]
[ValidateSet(“Present”, “Absent”)]
[String]$Ensure = “Present”,
[Parameter()]
[ValidateSet(“Allow”, “Deny”)]
[String]$Access = “Allow”,
[Parameter()]
[Bool]$NoInherit = $false
)
$InheritFlag = if($NoInherit){ “None” }else{ “ContainerInherit, ObjectInherit” }
$DesiredRule = New-Object System.Security.AccessControl.FileSystemAccessRule($Account, $Rights, $InheritFlag, “None”, $Access)
$CurrentACL = (Get-Item $Path).GetAccessControl(“Access”)
if($Ensure -eq “Present”)
{
$CurrentACL.AddAccessRule($DesiredRule)
Set-Acl $Path $CurrentACL
}
else
{
$CurrentRules = $CurrentACL.GetAccessRules($true, $false, [System.Security.Principal.NTAccount])
$Match = $CurrentRules |?{ ($DesiredRule.IdentityReference -eq $_.IdentityReference) -and
( $DesiredRule.FileSystemRights -eq $_.FileSystemRights) -and
( $DesiredRule.AccessControlType -eq $_.AccessControlType) -and
( $DesiredRule.InheritanceFlags -eq $_.InheritanceFlags )}
$Match | % {[void]$CurrentACL.RemoveAccessRule($_)}
Set-Acl $Path $CurrentACL
}
}
Remove-All-Permissions “C:\mytestfolder”

Quick Firewall Port Status

Quick way to check the ports open in a windows machine…netstat -a

Powershell script to check list of ports open on windows machine

<#
.NOTES
===========================================================================
Created on: 12/03/2015 11:00 AM
Created by: Pramod S Kumar 
Filename: GetFirewallStatus.ps1
===========================================================================
.DESCRIPTION
Checks firewall ports based on environment
.Usage: .\GetFirewallStatus.ps1 -CSVFile “ListofPorts.csv”
#>
param (
[string]$CSVFile = $null
)
function Check-FirewallState
{
[CmdletBinding()]
param
(
[Parameter(Position = 1, Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$CsvFileName
)
$CsvObject = Import-Csv $CsvFileName
$OutArray = @()
foreach($line in $CsvObject)
{
$RemoteServer = $($line.’IP Address’)
$Port = $($line.’Port Number’)
if (![string]::IsNullOrEmpty($RemoteServer) -and ![string]::IsNullOrEmpty($Port))
{
$myobj = “” | Select “Hostname”,”Port”,”Status”
$myobj.Hostname = $RemoteServer
$myobj.Port = $Port
Try
{
$tcp = New-Object System.Net.Sockets.TcpClient
Write-Host “Connecting to “$RemoteServer”:”$Port” (TCP)..”;
$myobj.Status = “Open”
$tcp.Connect($RemoteServer, $Port);
Write-Host “Connection successful” -ForegroundColor Green;
}
Catch
{
Write-Host “Connection failed” -ForegroundColor Red;
$myobj.Status = “Closed”
}
Finally
{
$tcp.Dispose();
}
$OutArray += $myobj
$myobj = $null
}
}
$OutArray | export-csv “PortStatus.csv”
}
Check-FirewallState $CSVFile

Above script takes input from csv file(listofports.csv) in below format and creates output file portstatus.csv. Script can be downloaded here.

Excelsheet
Connection