Wednesday 7 October 2015

Killing networking Commands with Command Prompt

                   These commands which will be very useful if your machine is connected over a network where hundreds or thousands of machine connected together.

ipconfig 

            This command will get your current ip address configuration with IPv4 address, default gateway and Subnet mask. If you want to get some more advanced information about your network , then go for the same command with /all after command with white space.



                      This will list a bunch of informartion about entire network information like IPv6 Address , DNS configuration , Network Adaptor Details , MAC address etc.,.



                     If you have enabled DHCP in your machine to renew and to get the new IP address type the command followed by ipconfig as ipconfig /renew

hostname

This command will prints the host name of our machine.


getmac

This will print the MAC address of you r machine will be single value if single interface card is present.


ping

If you want to check whether you  are connected to your network use ping command with neighbor host or other machine connected with your network.

                          You can also check with your internet connectivity with command prompt itself by typing any url followed by the ping command. Eg : ping www.google.com



telnet 

This command can be used for getting remote machine information with its server and client. For that we have to enable telnet client or server in our windows machine.

If you want to check any remote machine within your network use the following command
telnet <IPAddress> <Port>

How to enable telnet in Windows 8

      Before going to give details about how to enable telnet client in Windows 8 , better will learn something about telnet and it's uses.

                 Telnet which is available for both Windows and Linux environment . Telnet is basically one of the networking protocol with helps to communicate with a remote machine.

         In windows there are two things as part available such as telnet server and telnet client . Telnet Server enables you to manage a remote machine . In order to display the local server settings with telnet type the command tlntadmn.

Let's get on to the thing of how enable telnet server\client in Windows 8 machine in a simple way.

Go to the search tab my moving pointer to the right corner of your windows machine.

            Type turn windows and this will result option of Turn Windows feature On/Off.
Click on that will show the window like this.





In that you can see both Telnet Client and Server which we can enable and once done click OK . It will take few minutes to and it will be enabled automatically once we finished with the configuration.



To execute telnet commands open command prompt and type with the required telnet command. For example if you want to check a remote machine TCP port is open or already used by some other application . Type the command :

telnet 192.16.0.121 80

In this command telnet command followed by the ip address of the remote machine and the port need to check.


If the specified port is used by another application it will shows with message.

Friday 2 October 2015

Simple Powershell Scripting


               Powershell always rocking with its possibilities. As it mark's it one of the advantage is scripting over powershell .Powershell scripting is nothing like simply batch scripting but with bit advanced and powerful.

               Powershell scripts are nothing but simple text files with .ps1 extension. You can create powershell script in any if things like textpad , notepad or notepad++ etc.,. simply by typing whatever content we need and save it with .ps1 extension.

             Let's start with the simple sample script which is giving addition of two numbers.
The main thing in powershell is declaration and usage of variables. But it's pretty much simple than you thought because there is no data types or limitation of range here.We can store anything in a variable with powershell scripting.

Variable can be defined by using dollar($) symbol before your user defined variable variable name.

Eg:$a = 5 Here $a is the variable which holds the value 5. As I told earlier we can store any type of data in powershell variable . If you want to store your credential in a single variable that's also possible with powershell script.The following is the simple script which will give the addition of 2 numbers.

$a = 10
$b = 34
$c = $a + $b
Write-host "Result is :: "  + $c



Here you can se some command called Write-Host which is nothing but print statement in any of the language like c or c++ or Java

Here this is simple static program. What to do if you want to decide the value at run time ?
Simply it's possible by getting input from the user.

This can be achieved by means of 2 ways either passing as arguments or getting input from user and show it with resultant value.
At first I will explain how to make a script to accept arguments. The script will be

$a = $args[0]
$b = $args[1]
$c = $a + $b
Write-host "Result is :: " $c

pause






Here pause is optional which will be useful when run it using Run command.

Now scripts are ready. Then how to execute the script ?
It's simple with powershell. You can simply drag and drop the script in powershell window or navigate to path where script got located ant type the script name followed by .\ and click Enter to get the result.

If you want to run script without opening console type Powershell.exe and then followed by file location in Windows Run command.

Is it done ? defnintely not. If you execute script in your powershell for the first time it will show error like execution of script is disabled in this system.



Because by default script execution is in restricted state in Windows powershell in any of the system.
In order to enable this in you machine you can use any one of the following execution mode.

You have yo use the command Set-ExecutionPolicy -ExecutionPolicy followed by any one of the following execution mode.

 Unrestricted - No requirements script can be simply executed .
 RemoteSigned - Execute all local script and remote but remote script should be of signed one.
 Allsigned    - Both local and remote script need to be signed.

 For my betterment i will make it as Unrestricteed in my machine by using the command
Set-ExecutionPolicy -ExecutionPolicy Unrestricted



If you execute this command it will whether to allow it or not type 'Y' and fire enter.
If I am enabling in my system fir the first time i got some error likr this. So to solve that again launch powershell as administrator.





That's it with the basic scripting behind powershell since it is such a simple initiative to your advanced scripting.


Thursday 10 September 2015

How to start all windows service at the same time ?

The efficiency and power of powershell is always greater than you thought about it. Here I wish to make it things easier that we used to found very hard at times. In windows lot of services will be there each of them are not in running state .

Some of them are running , some of them are stopped and some of them may got disabled.

What you have to do if you want to start all the services at the same time?

Here is the place where powershell can help you.It is simpe single command to start all of the Windows services.

Get-Service is the command in powershell which will list out all the windows service running in your machine which will get name, state whether it is running or not and display name for each services.



You may want to filter out services which may have several restrictions. For this we can go with Where property or Where-Object cmdlet.

Suppose you want to want to start services which ever is in stopped state then here may use Where-Object cmdlet.

This command will start all the Service which has the Display name like "Windows" .

Get-Service | Where-Object{$_.DisplayName -like 'Windows*'}

The output will be like this,



If you want to start all the service whichever is in automatic state then go to powershell and simply enter the command Get-Service | Start-Service

That's it this command will start all the service. Is it done ? Definitely no.

This command will never start all the services available. It will start the services which we have enable automatic service start . Apart from  these services other services will be there which can be set type as Manual or may be disabled by the user


Wednesday 9 September 2015

How to connect Powershell remotely and execute command

The thing is it's very simple to connect powershell to remote computer if the machine is available with your network. Since we all know that Powershell is a powerful tool it allows you to execute commands and scripts remotely. Here the things which explains about the remote connectivity.

First you need to enable few things in your remote machine which you want to connect it .

It's always better to launch powershell to launch with Administrator privilege to make configurations.




Execute the Command,   Enable-PSRemoting -Force which makes the remote machine to connect for remote powershell. Here -Force is the property of this command which avoids prompting fro remote connection every time.



Next step is to add which are all the machines you want to access powershell with the remote machine . This option is simple that we can add limited machines or may can have all the machines within the network .

For this execute the command , Set-Item wsman:\localhost\client\trustedhosts * . IF you ant to restrict the machines then you can give the machine IP's or Host name as a comma separated.


Once dine with these things you have to restart the WinRM service. WinRM servce is noting but windows Remote management Service which is required for all remoting activities. In order to restart the service execute the command , Restart-service WinRM -Force


OK What is the next step . Say it's Done. How shall I check whether the configuration is working fine or not . For that there is a command to check whether the remote connection to the specific system is fine. Get to your machine once done with the remote machine.

Execute the command Test-WSMan Host/IPAddress . If is configuration is fine then it will show information about the schema and version or in other case if it fails to connect then it will show some error message with red.




These are all common things for remote powershell connectivity but few more things are there which will make your head to rotate. That is what it may starts with port problem.Because we will always connect remotely using port that may not visible usually.

 But if it is configured with different port or not the default port then there will be a problem . So we are not done with the configuration. For that what we need to do is , we hve to get the port which is listening over the remote machine . To gt the listening port execute the following command in remote machine , winrm e winrm/config/listener


Now you got the port, Test it using the command Test-WSMan Host/IPAddress -Port <Port-Listening st Remote Machine>

Note: By default the WinRM listening port will be 5895 for http

If you done fine with all the above configuration then there will be no failing case at all.

Simple command to get list of process running on remote machine:

Testing is fine.Connectivity is fine. Then , we can start to do things we need from remote machine. For that execute the commands.

To login wit the remote machine you should have the credentials . In powershell the beauty is we can save the Credential within a single variable.

$Credential = Get-Credential




the above command will prompt for user name and password . Give the exact user name with domain name with slash before use name if it is there . Click OK to save it. Now the $Credential variable have the credential information. Then proceed for the command which will give you the data of remote machine.

Invoke-Command -Computer COMPUTERNAME -Port <Listening Port> -Credential $Credential -ScriptBlock{Get-Process} 

This will list processes running in the emote machine.What is the command given above is exactly doing. Lest's make you clear with that. In that command Invoke-Command used to invoke remote command or script. The man thing is the -ScriptBlock and the content which we are giving within the curly braces. We can also give the exact  script path within the curly braces if you want to execute a script rather than running as a command in the remote powershell.