Monday, February 17, 2020

Test Script

##############################################################################





###################################################################################





 

 

$asciiNum=97..122

$letters=$asciiNum|ForEach-Object { [char]$_ }

$date = (Get-Date).AddDays(-15) #Last 15 Day

foreach($letter1 in $letters)



{

$filter=$letter1+"*"

$gname="TS.SAP_HKG_"+$letter1

$users= Get-ADUser -Filter {whenCreated -ge $date -and name -like $letter} -server hkg -Properties * | select name

If ($users -eq $null)



{

Write-Host -ForegroundColor Red "No user Exist in Last 15 days in Starting with $letter"



}

Else



{

foreach ($user1 in $users.name)



{

Add-ADGroupMember -Identity $gname -Members $user1

Write-Host -ForegroundColor Cyan "$user1 added to group $gname"



}

}

}


Sunday, February 16, 2020

sample script for Access

<# This tool has been desgined by Swagat Rout
For More pls Contact: Swagatkumar.rout@outlook.com
.NAME
Untitled
#>

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Delegation = New-Object system.Windows.Forms.Form
$Delegation.ClientSize = '523,348'
$Delegation.text = "ACCESS Management"
$Delegation.BackColor = "#f8d41c"
$Delegation.TopMost = $false

$URL = New-Object system.Windows.Forms.Label
$URL.text = "URL"
$URL.AutoSize = $true
$URL.width = 25
$URL.height = 10
$URL.location = New-Object System.Drawing.Point(27,21)
$URL.Font = 'Microsoft Sans Serif,14,style=Bold'

$NetworkPath = New-Object system.Windows.Forms.TextBox
$NetworkPath.multiline = $false
$NetworkPath.width = 329
$NetworkPath.height = 20
$NetworkPath.location = New-Object System.Drawing.Point(108,15)
$NetworkPath.Font = 'Microsoft Sans Serif,10'

$ACL = New-Object system.Windows.Forms.Button
$ACL.BackColor = "#7ed321"
$ACL.text = "ACL"
$ACL.width = 60
$ACL.height = 30
$ACL.Anchor = 'top,bottom,left'
$ACL.location = New-Object System.Drawing.Point(25,69)
$ACL.Font = 'Microsoft Sans Serif,11,style=Bold'

$result = New-Object system.Windows.Forms.TextBox
$result.multiline = $true
$result.width = 294
$result.height = 159
$result.location = New-Object System.Drawing.Point(136,70)
$result.Font = 'Microsoft Sans Serif,10'

#$result = New-Object system.Windows.Forms.ListBox
#$result.text = "listbox"
#$result.width = 294
#$result.height = 159
#$result.location = New-Object System.Drawing.Point(136,70)

$CloseButton = New-Object system.Windows.Forms.Button
$CloseButton.BackColor = "#21d371"
$CloseButton.text = "close"
$CloseButton.width = 56
$CloseButton.height = 21.20001220703125
$CloseButton.Anchor = 'bottom,left'
$CloseButton.location = New-Object System.Drawing.Point(356,270)
$CloseButton.Font = 'Microsoft Sans Serif,10,style=Bold'
$CloseButton.ForeColor = "#000000"

$Delegation.controls.AddRange(@($URL,$NetworkPath,$ACL,$result,$CloseButton))

$ACL.Add_Click({ Acl })
$closeButton.Add_Click({ closeForm })

Function Acl()
{
$result.Text += "`r`n" + "fetching information for" +$NetworkPath.Text
$val=Get-Acl $NetworkPath.Text #| select Access -ExpandProperty Access | select IdentityReference,FileSystemRights |ogv
#Validation and result part
if($val -ne $null)
{
Get-Acl $NetworkPath.Text | select Access -ExpandProperty Access | select IdentityReference,FileSystemRights |ogv
}
else
{
$result.Text += "`r`n" + "path does not exist, Try again with correct path"
}
# $result.Text = $rights.filesystemrights,$rights.identityreference
}


function closeForm(){$Delegation.close()}

#Write your logic code here

[void]$Delegation.ShowDialog()

Friday, March 15, 2019

PowerShell Script to fetch drive information for C: drive in multiple servers:


Below script will provide you the information of Disk space utilization consists of free percentage, full bock size info etc.


############################################################################################################
#                                                                                                          #
#      Author :- Swagat kumar Rout                                                                         #
# for any issues pls contact swagatkumar.rout@outlook.com                                                  #
############################################################################################################
 
 
 
$svn= cat "c:/temp/complist.txt"
$i=0;
foreach ($svn1 in $svn)
{
    $i=$i+1;
    Write-Host $i;
    Write-Host -ForegroundColor cyan "fetching information for $svn1"
    Get-WmiObject win32_Logicaldisk -ComputerName $svn1 -Filter "DeviceID='C:'" | select Name, FileSystem,FreeSpace,BlockSize,Size | % {$_.BlockSize=(($_.FreeSpace)/($_.Size))*100;$_.FreeSpace=($_.FreeSpace/1GB);$_.Size=($_.Size/1GB);$_}| Select-Object @{n="Computername";e={$svn1}},@{n="Drivename";e="name"}, @{n='FS';e={$_.FileSystem}},@{n='Free, Gb';e={'{0:N2}'-f $_.FreeSpace}}, @{n='Free,%';e={'{0:N2}'-f $_.BlockSize}},@{n='Capacity ,Gb';e={'{0:N3}' -f $_.Size}} |Export-Csv C:\temp\DisKSPACELIST.CSV -Append -Encoding UTF8 -ErrorAction SilentlyContinue
  }
 
  Write-Host -ForegroundColor Yellow "PLEASE CHECK THE FILE AT 'C:\temp\DisKSPACELIST.CSV'"

Tuesday, June 20, 2017

Powershell Script to remove Active sessions from server

If you want to remove all the active session on a remote computer/server, Please follow the below script . In going forward we can modify the script for number of computers/servers , for that we need to run another for each loop.
#################################################################################
Write-Host -ForegroundColor Yellow "This is tool has made to remove Session from server, For any issues please contact, Swagat Rout"

$server= read-host "Please enter the server name"

quser /server:$server #| Select USERNAME,STATE,LOGON,TIME

$condition= Read-Host "Do you want to remove a session of user,for yes type [y] & No Type [N]"

if ($condition -eq "y")



{
 
$user= Read-Host "Do you want to remove all the session or some specific users, For all type [A]& Specific userType [S]"

if($user -eq "A")



{
 
$sessions = qwinsta /server $server| ?{ $_ -notmatch '^ SESSIONNAME' } | %{

$item = "" | Select "Active", "SessionName", "Username", "Id", "State", "Type", "Device"

$item.Active = $_.Substring(0,1) -match '>'

$item.SessionName = $_.Substring(1,18).Trim()

$item.Username = $_.Substring(19,20).Trim()

$item.Id = $_.Substring(39,9).Trim()

$item.State = $_.Substring(48,8).Trim()

$item.Type = $_.Substring(56,12).Trim()

$item.Device = $_.Substring(68).Trim()

$item}




 
foreach ($session in $sessions)



{
 
if ($session.Username -ne "" -or $session.Username.Length -gt 1)



{
 
$nam= $session.username

#logoff $session.Id /server $server

Write-Host -ForegroundColor Cyan "Sessions has been removed from $server for $nam "



}

}
 
Write-Host -ForegroundColor Cyan "All the sessions has been removed"

Read-Host "Please enter any key to exit"



}
 
elseif($user -eq "S")



{
 
$specificuser = Read-Host "Please enter the UID, Please ensure that user should have active logon session in the list"

#$sesionID=((quser /server:$server | Where-Object { $_ -match "$specificuser" }) -split ' +')[2]

#logoff $sesionID /server:$server

Write-Host -ForegroundColor Cyan "Sessions has been removed from $server for $specificuser"

Read-Host "Please enter any key to exit"



}

}
 
Else
 
 
{
 
Read-Host "Please enter any key to exit"



}

###################################################################################################
 

Test Script

############################################################################## #####################################################...