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

Welcome Forums General PowerShell Q&A PS Script works in ISE not in Console. v3.0

This topic contains 2 replies, has 2 voices, and was last updated by

4 years, 5 months ago.

  • Author
  • #20247

    Points: 18
    Rank: Member


    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.

    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 )

    Script :

    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)
            Write-Output "Service failed : $fail : Attempting to restart!!"
            Start-Service -Name $fail -Verbose
            sleep 2
            Get-Service -Name $fail
        Write-Output "Services are running as designed"
    sleep 1

    Console Output

    " .\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)


    Any help would be higly appreciated.


  • #20248

    Points: 0
    Rank: Member

    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

      Points: 18
      Rank: Member

      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..

The topic ‘PS Script works in ISE not in Console. v3.0’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort