locked
Simple double if script not working RRS feed

  • Question

  • Hi guys, please someone help with the simple few lines below

    Getting the error shown

    else : The term 'else' is not recognized as the name of a cmdlet, function, script file, or operable program.

    _______________________________________________________

    $Computers = Get-Content C:\scripts\ComputerName.txt
    $Computers | ForEach-Object {
        if (Test-Connection $_ -Count 1 -Quiet)
        {Write-Host "$_ is online"}


        if (Test-WSMan -ComputerName $_.Name -ErrorAction SilentlyContinue) {
            Write-Host "$($_.Name) - WinRM Enabled" }
            else { Write-host "$($_.Name) - WinRM Disabled "}


        else {
            
            Write-Host "$_ is offline"

            }
    }

    Thursday, June 11, 2020 4:37 PM

All replies

  • Sorry, this one here

    $Computers = Get-Content C:\scripts\ComputerName.txt
    $Computers | ForEach-Object {
        if (Test-Connection $_ -Count 1 -Quiet)
        {Write-Host "$_ is online"}


        if (Test-WSMan -ComputerName $_.Name -ErrorAction SilentlyContinue) {
            Write-Host "$_ - WinRM Enabled" }
            else { Write-host "$_ - WinRM Disabled "}


        else {
            
            Write-Host "$_ is offline"

            }
    }

    Thursday, June 11, 2020 4:47 PM
  • If you would format your code correctly you wouldn't have this problem.  It would also help you to see your mistakes esily.

    Get-Content C:\scripts\ComputerName.txt |
        ForEach-Object{
            if (Test-Connection $_ -Count 1 -Quiet){
            
                Write-Host "$_ is online"
                if(Test-WSMan -ComputerName $_.Name -ErrorAction SilentlyContinue){
                    Write-Host "$($_.Name) - WinRM Enabled"
                }else{
                    Write-host "$($_.Name) - WinRM Disabled "
                }
    
            }else{
                Write-Host "$_ is offline"
            }
        }

    Please post code correctly in technical forums.


    \_(ツ)_/


    Thursday, June 11, 2020 6:22 PM
  • Hi,
    Was your issue resolved? 
    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.
    Best Regards,
    Yang Yang

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, July 3, 2020 3:35 AM