Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 8583

Help Make Script better

$
0
0

Hello Experts,

I have written a script to stop anti-virus service on few servers.  Everything's working as expected.  I wanted to get your guy's input on how I can make this script better or if there are some obvious mistake that I am doing in the script or things can be done in a better way..

<#
-----------------------------------------------------------
    AP   
    Script to Stop the Service on the Remote Computer
    v1.0
-----------------------------------------------------------       

@Description:

This script is created to stop the service remotely on the servers provided in the list.  Note: At this time, I've included it to stop one service at a time. 

What to expect in v1.1

- This script will be modified to have an array of services to stop. In other words, we can stop multiple services using this script on multiple servers.
- Additional Try - Catch Block will be added to see if the service name provided is valid or not. If not a seprate log file will be created to catch the wrong name and error.

------------------------------------------------------------
@Server List:
Please create a CSV file with the names of the servers.  Top column should contain "Server"

------------------------------------------------------------
@Log File's:

Q. Where to check the Log Files?
A. The Log Files will be located under C:\Temp\SvcRequest

Q. How can I check if services are stopped on the intended server:
A. Browse to the location; C:\Temp\SvcRequest and open the file "Stopped_BY_Script.log". Check the name of the server intended and the status of the service.

------------------------------------------------------------

@How do I run this script:

Step1: Go to Start -> Search PowerShell -> Shift+Right Click -> Run as a different User.  Use you admin.account to open powershell.

Step2: Browse to the directory, where script is located. Type in .\StopService.ps1 and hit enter

Step3: It will ask for the service to stop

Step4: As an additional pre-caution; if the service matches; it will ask for confirmation. Please enter Y if you want to stop the intended service

#>

# Input file for Server
$InputFile = "C:\Scripts\Serverlist.csv"

# Log file; if service is Running
$LogMe_stopped = "C:\Temp\SvcRequest\Already_Stopped.log"

# Log file; if service is stopped
$LogMe_started = "C:\Temp\SvcRequest\Stopped_BY_Script.log"
#
# Function to check the services and stop
Function Stop-Services
{
 
  [CmdletBinding()]
Param(
  [Parameter(Mandatory=$True,Position=1)]
   [string]$svcName
   
 )
 
 Import-Csv $InputFile | ForEach-Object {
 
            $checkStatus = Get-Service -DisplayName $svcName -ComputerName $_.Server
   
            # Write-host $checkStatus   
               
 IF ($checkStatus.status -eq "Stopped"){

                # Write-host "This is a if loop"  
                #$checkStatus | Out-File C:\Scripts\Already_Stopped.log -Width 120
                #$checkStatus | Out-File $LogMe_stopped -Width 120
               
                $Errorobj = New-Object -TypeName PSObject

                $Errorobj | Add-Member -MemberType NoteProperty `
                -Name ComputerName -Value $_.Server
               
                $Errorobj | Add-Member -MemberType NoteProperty `
                -Name Service -Value ($CheckStatus.Name)

                $Errorobj | Add-Member -MemberType NoteProperty `
                -Name Status -Value ($CheckStatus.Status)


                Write-Output $Errorobj

                $Errorobj | Out-File  $LogMe_stopped -Append -NoClobber
               

                #Write-Host "Service is stopped, cannot stop it again!!!"

                                           }

   Else

        {

            #Write-Host "This is a else loop"  
            $svcstop = Get-Service -DisplayName $svcName -ComputerName $_.Server | Stop-Service -Confirm
                      
            $Stopobj = New-Object -TypeName PSObject

                $Stopobj | Add-Member -MemberType NoteProperty `
                -Name ComputerName -Value $_.Server
               
                $Stopobj | Add-Member -MemberType NoteProperty `
                -Name Service -Value ($CheckStatus.Name)

                $Stopobj | Add-Member -MemberType NoteProperty `
                -Name svcStatusBefore -Value ($CheckStatus.Status)

                $NewStatus = Get-Service -DisplayName $svcName -ComputerName $_.Server

                $StopObj | Add-Member -MemberType NoteProperty `
                -Name svcStatusAfter -Value ($NewStatus.Status)

                $DateTime = Get-Date

                $StopObj | Add-Member -MemberType NoteProperty `
                -Name OperationPerformedon -Value ($DateTime)

                $Stopobj | Out-File $LogMe_started -Append -NoClobber
                #Write-Output $obj

           
          <# #$svcstop | Select Name,Status
             #Write-host "$svcstop"
             #$svcstop | Out-File C:\Scripts\Stopped_BY_Script.log -Width 120
             #$svcstop | Out-File $LogMe_started
            
           #>
                       
            Write-Host "Services Stopped Successfully"
          
        }      

                        }
}


Stop-Services

__________________________________________________________________________

Appreciate your time in advance.

Thanks,

AP


Viewing all articles
Browse latest Browse all 8583

Latest Images

Trending Articles



Latest Images