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 -







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