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

 Server inventory


Change the file and software name given in Bold according to your need.

$com = Get-Content C:\FileNAme.txt
foreach($computers in $com){
$cpu = Get-WmiObject -Class Win32_Processor -Computername $computers
$mb =  Get-WmiObject -Class Win32_BaseBoard -Computername $computers
$sn =  Get-WmiObject -Class win32_bios -Computername $computers
$cn =  Get-WmiObject -Class Win32_ComputerSystem -Computername $computers
$os =  Get-WmiObject -Class Win32_OperatingSystem -Computername $computers
$Office = Get-WmiObject -Class Win32_Product -Computername $computers | where {$_.Name -eq "Microsoft Office Standard 2007"}

$props = @{           
    Computername  = $cn.__Server
    SerialNo      = $sn.Serialnumber
    Model         = $cn.Model
    TotalPhysicalMemory = $cn.TotalPhysicalMemory / 1024 / 1024
    Os = $os.Caption
    IE = $IE.Version
    Office = $office.Name
   
    
    }

New-Object PSObject -Property $props | Ft -Autosize}

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 Script - Model, Name, Serial Number

One who is starting to learn powershell this ones for you fella. This is a very basic script to fetch Computer Model, its name and manufacturer serial number which is unique and helps to identify the machine from a lot.This one works from sitting on your own machine and is able to pull the information from multiple computers one by one. What we you have to do is to add every computer name in a text file then it will connect to all those machine one by one and will keep fetching the details for you in an well defined manner.

Change the filename given in bold letters and put computernames in it. Run the below script from powershell only.


$com = Get-Content C:\FileNAme.txt
foreach($computers in $com){
$sn =  Get-WmiObject -Class win32_bios -Computername $computers -EA silentlyContinue
$cn =  Get-WmiObject -Class Win32_ComputerSystem -Computername $computers -EA silentlyContinue
Write-Host $sn.SerialNumber, $cn.Name, $cn.Model

}

Regards, IIS

PowerShell Scripts - File exist or not

In an organisation where software inventory application does not exist Scripts plays a good role. I met one situation where we had to analyse that in how many computers of our organisation has Winzip installed so that licenses can be procured before they raid us and charge the fine upon.Thus inspired me to create a script which would explore the files remotely exist or not.

I have again used the powershell to make this happen. Script would go through the text file which has computer names in it and will execute the same command one by one on all of them.


$colComputers = Get-Content C:\FileName.txt
$ErrorActionPreference = "SilentlyContinue"
foreach ($StrComputer in $colComputers)
{
$GenItems1 = gwmi Win32_ComputerSystem -Comp $StrComputer
$x86Path = 'C:\Program Files (x86)\WinZip\WZCAB.DLL'
if(!$GenItems1){
Write-Host “$StrComputer not found”
}
else
{
Write-Host $StrComputer, (Get-Item $x86Path | Select -ExpandProperty VersionInfo).fileVersion
}
}



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

Install Multiple Software with a Click