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

Thursday, 6 March 2014

PowerShell Script - Stop similar services

This script can be very important when you want to schedule some of the similar service to stop at certain time for the maintenance purposes. I had one incident in which Altiris asset explorer used to malfunction after a while and solution to the issue is stop its service and restart thus I created the script to get rid of the situation. Here below is the example which I used to stop all SQL services at once.

Change the Bold Part as per your need.

$services = Get-Service
Foreach ($service in $services) {
If($service.name –like ‘SQL*’) {
$service.stop()
}
}

Regards,
Baij

PowerShell Scripts - Running on remote host

If you have any powershell script which is working on your local system and the same want to run over remote hosts and get details. Use below as a solution to your request. The very first line determines how variable for getting hostname is determined. I have created one for getting Operating system details.



$computername = (Read-host 'Enter a computer name')
)

get-wmiobject -class win32_operatingsystem -computer $computername | select-object BuildNumber, Caption, ServicePackMajorVersion

Regards, Baij

PowerShell Scripts - Functions Definition

Have Created one function which will fetch Operating Information for multiple computers.

Change the text file name accordingly.

Function Get-Osinfo {
BEGIN{}
Process{
                $computername = $_
                Get-wmiobject –class win32_operatingsystem –computer $computername | select-object BuildNumber, Caption, ServicePackMajorVersion
}
END{}
}

Get-content C:\computers.txt | get-osinfo

Regards
Baij

PowerShell Scripts - Ping a Series of IP Address

There are many application over the internet which can ping multiple machines and provide you the status however no software is trustworthy unless you know its coding thus should not be used as far as server or important machines are concerned thus created one of which coding is visible and is very easy to implement.

We need to simply change IP address series 10.10.10.$ to what is our requirement and it is just ready for your environment. Track your important servers connectivity using this simple script.

Start

$ping = New-Object System.Net.NetworkInformation.Ping
$i = 0
1..255 | foreach { $ip = “10.10.10.$_”
$Res = $ping.send($ip)
if ($Res.Status -eq “Success”)
{
$result = $ip + ” = Success”
Write-Host $result
$i++
}}

$Hosts = [string]$i + ” Hosts is pingable”


Write-Host $Hosts

Finish

Regards, IIS

PowerShell Scripts - Machine details from AD

It is also possible to fetch the Operating system and Machine name from Active Directory for which simple one line is enough. This script can fetch more in less time as it simply has to connect to active directory database. Other ways to pull the same information is to run a script remotely to each and every computer one by one remotely and then have data. More information can also be added like version, fsdn by just extending the script.

Below script will give away the output in a well defined table and can be used to create dashboard.

Get-ADComputer -Filter * -Property * | Format-Table Name,OperatingSystem -Wrap –Auto 


Same Information can be achieved by using Power shell also.


DSQUERY Computer "OU=OUNAME,DC=india,DC=DOMAINNAME,DC=local" -name COMPName* -scope subtree -limit 0 > C:\ComputerOU.csv


Regards, IIS

Install Multiple Software with a Click