PS Script works in ISE not in Console. v3.0

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Venugopal Venugopal 2 years, 1 month ago.

  • Author
    Posts
  • #20247
    Profile photo of Venugopal
    Venugopal
    Participant

    Hello,

    I am new to PS,but have been trying to learn for sometime now.

    I've made s script that would
    1.On a new build server, take the important services and export them to c:\Services.txt.
    2.Then keep this task that would run on the server (PS script in scheduled task) to check and verify and possibly try to restart it if found not running.

    Problem.
    1.The script executes perfectly in ISE.( after re-opening multiple times, just to clear any cached vars )

    2. Script fails badly in Console. Tried STA mode also. ( after re-opening multiple times, just to clear any cached vars )

    [b]Script :[/b]

    
    While($true)
    {
    
    Write-Output "Checking Services .........."
    Sleep 2
    $Running =""
    $Running = Get-Service |Where { $_.Status -eq "Running"} | Select-Object Name
    Write-Output $Running | Out-File C:\Users\xxxxx\Scripts\services1.txt
    $failed =""
    $failed = Compare-Object (Get-Content  C:\Users\xxxxx\Scripts\services.txt) -DifferenceObject (Get-Content C:\Users\xxxxxx\Scripts\services1.txt) | where {$_.SideIndicator -eq "< ="}| Select-Object InputObject -ExpandProperty InputObject
    
    If($failed.length -ne "0")
    {
        foreach($fail in $failed)
        {
     
            $fail=$fail.trim()
            Write-Output "Service failed : $fail : Attempting to restart!!"
            Start-Service -Name $fail -Verbose
            sleep 2
            Get-Service -Name $fail
             
        }
    }
    else
    {
    
        Write-Output "Services are running as designed"
    
    }
    sleep 1
    }

    [b]Console Output[/b]

    " .\AutoServiceBot.ps1
    Checking Services ..........
    Service failed : Name : Attempting to restart!!
    Start-Service : Cannot find any service with service name "Name".
    At C:\Users\xxxxx\Scripts\AutoServiceBot.ps1:26 char:9
    + Start-Service -Name $fail -Verbose
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (Name:String) [Start-Service], ServiceCommandException
    + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.StartServiceCommand

    ==========================================================================================================================

    ExpandProperty has to be removing the below from output so that it could be compared.(please let me know if i were to be wrong anywhere)

    "
    Name
    ———
    "

    Any help would be higly appreciated.

    Regards.

  • #20248
    Profile photo of Simon Wåhlin
    Simon Wåhlin
    Participant

    You are absolutely correct.

    I assume that the file services.txt which is your list of servers that should be running does not contain either "Name" nor "—-"

    Change the row:
    $Running = Get-Service |Where { $_.Status -eq "Running"} | Select-Object Name

    to this:
    $Running = Get-Service |Where { $_.Status -eq "Running"} | Select-Object -ExpandProperty Name

    And your services1.txt will only contains servernames.

    • #20296
      Profile photo of Venugopal
      Venugopal
      Participant

      Thank you Simon, that really helped, and one more issue that i noticed was,

      1.The initial services.txt had space after the service names, which returned incorrect output to $failed when compared.

      My whole idea was to automate some part of maintenance works for service alerts.
      Of course I have to modify the script in many more ways....

      ......... any ideas/guidance in real time auto-'fix'-mation 🙂 from your end? 🙂

      Thank you once again..

You must be logged in to reply to this topic.