Hi Guys,
In the previous blog I took you through PowerShell basics We know that PowerShell to works with different modules like SQL server, Active Directory, Windows Administration, IIS, etc. In this blog I shall be updating a few scripts I've used. You may modify the same as per your environment.You will be able to find many such scripts online and under http://powershell.com
1.Deleting Files older than Certain Date#Folder from which files are to be deleted
$filepath= "D:\"#Change the path based on your file location
#Accepting the number of days prior to which user wants to delete the files
$no_of_days = read-host "Enter the number of days(integer) prior to which backups should be delete "
$a=get-date
$a= $a.adddays(-$no_of_days)
#Confirming the date with the user
write-host "Do you want to delete files older than"$a" Y/N"
$response=read-host
If ($response -eq "Y"-or $response-eq "y")
{
write-host " Deleting files from $filepath older than " $a
#Deleting files only
get-childitem $path -recurse | where-object {$_.mode -notmatch"d"} |where-object {$_.lastwritetime.date -lt $a} | ForEach-Object{ Write-Host "Deleting "$_.FullName; Remove-Item $_.FullName;}
}
#Confirming shell closure
else
{
write-host "Execution cancelled to execute successfully pass the right parameters & hit Y/y. Any other input will be considered Invalid"
start-sleep -s 7}
2. Pinging Multiple Servers
#Servername Text File
$filepath = "C:\Test.txt" #Change the path based on your file location
$Sname = Get-Content $filepath
#Running Loop for each servername in text file
foreach ($S in $Sname)
{
#Checks if the server is pingable
$StatusFlag =test-Connection -ComputerName$S -Count 1 -Quiet
if ($StatusFlag) #If server is pingable dispaly source,destination & response time
{
#Sends just one packet to the server and provides the response time
Test-Connection $S-count 1|select-object -Property @{Name="Source"; Expression = {$_.pscomputername}},@{Name="Destination"; Expression = {$_.address}},responsetime
}
else
{
write-host "$S is inaccessible from $env:computername" -ForegroundColorCyan
}
}
3.Multiple Server Entire Directory Structure and File Copy
# Directory from where the files and folder structure needs to be copied
$directorypath = 'C:\directory\*.*' #Change the path based on your file location
$servercount = read-host "Enter total number of servers for filecopy"
# Accepting servernames and inserting them in an Array
if ($servercount-gt 0)
{
#write-host "Enter the servernames"
$servers = @()
$i=0
do
{
$servers += read-host "Enter Server $($i+1)"
$i++
} while($i -lt $servercount)
#Once servernames are accepted copy the entire direcorypath structure with the files to destination
Foreach ($s in $servers){
#write-host "destination is " $s
Copy-Item $directorypath-Destination "\\$s\C$\directory"-Recurse
#Change the Destination as per your requirement
}
}
else
{
write-host "Total number of servers is 0 script exccution closed"
}
I'll update this with more scripts as and when tested and used. Until then keep learning and exploring...