Tuesday, November 25, 2014

#33 : Complete script to download file with Powershell

Powershell can be used to download some files from internet or intranet. We can simply do it with Invoke-WebRequest.

Below is the code which can be used for this purpose :

#--------------------------------------------------------------------------------------------------------------
#-- Script to Download file 
#--------------------------------------------------------------------------------------------------------------

function Download-File ([string]$url , [string]$target)
{
 trap {
  echo "Error occured" 
  continue 
 }
 
 if ( !(Test-path $target ))
 {
  invoke-webrequest $url -OutFile $target
 }
 else 
 {
  echo "Error : Directory does not exit!"
 }

}

#--------------------------------------------------------------------------------------------------------------

Download-File "http://site.foo.com/file.exe" "C:\temp"

#--------------------------------------------------------------------------------------------------------------

Enjoy!

Tuesday, November 11, 2014

#32 : How to check if TCP/IP Port is open?

Without talking much, please find the code below.
#------------------------------------------------------------------------------------------
#     Script : Check_Port.ps1 
#     Author : Som DT.
#    Purpose : Checking if Port is open on target TCP/IP server from current host 
#------------------------------------------------------------------------------------------

function Check-Port([string]$HostName, [string] $PortNumber) 
{
    #--Set the client --# 
    $tcp = New-Object System.Net.Sockets.TcpClient
    
    #--Connecting to the Port --# 
    $tcp.connect($HostName , $PortNumber ) 
    

}

Enjoy!

Monday, November 3, 2014

#31 : Loading file content into Array of Hash Table

While programming, sometimes we need to read some configuration entries from a flat file. There are several approaches to do it, I have followed the below approach to read file-entries into a hash table which is also an array.
I have assumed that there is a flat file which contains multiple lines of information about employee. This can be configuration settings or it can be anything. I have used this simple example for simplicity -



#---------------------------------------------------------------------------------------------#
#--Script to load file into an Array of Hash Table
#---------------------------------------------------------------------------------------------#
#--[1]- Let's create a file with entries seperated by comma --#
$file_name="D:\Check\file.txt"
echo "1,John,5000" > $file_name echo "2,Jack,3000" >> $file_name
echo "3,Jack,3000" >> $file_name
#--[2]- Load the information into an Hash table Array --#
$emp_data = @()
$file_entries = gc $file_name
foreach ( $file_entry in $file_entries )
{
$emp_id=$file_entry.split(",")[0]
$emp_name=$file_entry.split(",")[1]
$emp_salary=$file_entry.split(",")[2]
$emp_data += @{emp_id="${emp_id}"; emp_name="$emp_name"; emp_salary="${emp_salary}"}
}
#--[3]- Display the same --#
for ($x=0 ; $x -lt $emp_data.Count ; $x++)
{
$emp_data[$x].emp_id
$emp_data[$x].emp_name
$emp_data[$x].emp_salary
}




There might be many other approach to do it, I do not advocate it as a best approach, but if you want to have full control and understandability of the code, you may use the above approach.

#112: How to handle xml document in Powershell?

 In PowerShell, you can handle XML data using various cmdlets and methods provided by the .NET Framework. Here's a basic guide on how to...