help writing a script

This topic contains 5 replies, has 2 voices, and was last updated by  Lidor 3 months, 1 week ago.

  • Author
    Posts
  • #98653

    Lidor
    Participant

    I need a script that will ask a user to input any service name and the script will check if the script is available (doesn't matter running or stopped as long as its available in windows)

    if it is available it will write "The service exists!" in green color

    If it doesn't exist
    it will write "the service does not exist!" in red letters

    Please help me Im trying for hours with no success!

  • #98656

    Lidor
    Participant

    correction:

    I need a script that will ask a user to input any service name and the script will check if the service is available (doesn't matter running or stopped as long as its available in windows)

    if it is available it will write "The service exists!" in green color

    If it doesn't exist
    it will write "the service does not exist!" in red letters

  • #98658

    Simon B
    Participant

    What code do you have already ? Also a quick google and I came up with this that might point you in the correct direction
    https://social.technet.microsoft.com/Forums/azure/en-US/8c2c8111-8995-4ff6-a25c-e0f48136e14f/determine-if-service-exists?forum=winserverpowershell

  • #98664

    Lidor
    Participant

    I found this link aswell but it doesn't help me since this script searchs for 1 service "alg"
    I want the script to check any service the user will input

    I don't know how to fix this script in order to do it.. if u could do it for me it will be great.

  • #98667

    Simon B
    Participant

    Try something like this :-

    $serviceName = Read-Host "Please enter service name"
    
    If (Get-Service $serviceName -ErrorAction SilentlyContinue) {
    
        If ((Get-Service $serviceName).Status -eq 'Running') {
    
            Write-Host "$serviceName found, and is running" -ForegroundColor Green
    
        } Else {
    
            Write-Host "$serviceName found, but it is not running." -ForegroundColor Yellow
    
        }
    
    } Else {
    
        Write-Host "$serviceName not found" -ForegroundColor Red
    
    }
  • #98685

    Lidor
    Participant

    thanks simon it worked well.

    I managed to write it in a different way

    $s=Get-Service
    $name= read-Host "enter a service name"
    foreach($s2 in $s.name)
    {
    if($s2 -like $name)
    {
    Write-Host "service exist" -ForegroundColor green
    exit
    }
    }

    Write-Host "service no exist" -ForegroundColor red

You must be logged in to reply to this topic.