PageViews Last Month

Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

Wednesday, 13 January 2016

Basic Windows Administration through Powershell

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

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...

Monday, 11 January 2016

Getting Started with PowerShell

Windows PowerShell is all about exploring .NET framework capability through the command line.Announced in 2006 it’s an admins paradise with the number of tasks that can be automated through the command line. The below table gives the release dates for various version of PowerShell.

PowerShell Version
Release Date
Default Windows Versions
Available Windows Versions
PowerShell 1.0
November 2006
Windows Server 2008 (*)
PowerShell 2.0
October 2009
Windows 7
Windows Server 2008 R2 (**)
PowerShell 3.0
September 2012
Windows 8
Windows Server 2012
PowerShell 4.0
October 2013
Windows 8.1
Windows Server 2012 R2
PowerShell 5.0
April 2014 
Windows 10

1. Why & what is Powershell.



Prior to Powershell VBscripts/unix scripts or DOS commands were encoded in batch files to be executed for performing repetitive task.Should there be a need to change the pattern of design the entire code would have to be re-written.This involved additional work, time and testing. Instead there was a need of a functionality wherein which you could write you own scripts give it a command name similar to that like DIR or MKDIR and have different parameter sets modifiable as per requirements.


PowerShell was invented with the idea of automating Microsoft GUI commands through a command-line interface.If you have any of the above windows version installed you will find the PowerShell under All programs -> Accessories -> Windows Powershell.  
PowerShell is both a command-line shell and scripting language. 








You may choose the ISE environment (x86 if your are running a 32-bit system) once you are familiar with the commands and wish to start scripting programs in PowerShell. PowerShell has its internal variables as well just like any other shell.  


Get-Variable would return the list.



All the commands written for PowerShell are called CMDLETS and they have the common structure of Verb-Noun. The native Windows-DOS and Unix commands work with PowerShell. For ef - dir,cls,ps,etc. Below are a few basic commands


1.      Get- help
2.      Get-command
3.      Get-verb
4.      Get-member
5.      Get-alias

Anything and everything can be learnt I believe with proper use of the above CmdLets .Get-Help  has a similar functionality as man command in Unix. Powershell works with all the wild card characters similar to Unix or DOS …Suppose you need to know the commands associated with process.I would type something like 

Get-Command *Process

and it would return the following output depending on the version.

To know the syntax and semantics of the usage of the command one could use.

Get-help Start-process 

and it would return the following output depending on the version.


As I stated earlier powershell returns object so if one wants to know what kind of object is returned and how to work further with the object  you could try

Get-Process | Get-Member 

and it would return the following output  depending on the version.

Wondering how the native commands works simple.Type the command

Get-Alias 

and you would get the below output depending on the version.

Get-verb 

lists the existing verb supported by PowerShell.

I'd suggest if you really want to learn first run the command Get-Help Get-Help and browse your way through the options. If you’re a keen learner this blog will just help you take your first step to the arena of automation through PowerShell.

Microsoft Virtual Academy provides and 6 hours intensive course by expert Jeffrey Snover, the inventor of PowerShell, and Jason Helmick, Senior Technologist at Concentrated Technolog to get started with powershell. 

My personal favorite are books by Don Jones. "Learn Powershell in a month of lunches" It helped me get started. I also recommend to go through the below link which provides a summary of PowerShell basics.
https://technet.microsoft.com/en-us/magazine/hh551144.aspx

Powershell has the ability to work with different modules like SQL server, Active Directory, Windows Administration, IIS. Advantages and usage of Powershell are limitless for you to explore.