locked
Powershell basics RRS feed

  • Question

  • I am new to this, but love it!

    My goal is to Test-Connection to 1 device. I the connection succeeded continue, If not ask for the device again.

    Here is what I have:

    $GET_DEVICE_NAME = Read-Host "Please enter the device you are backing up"

    $testConnection = Test-Connection $GET_DEVICE_NAME -Quiet

    IF ($testConnection -eq "True")

    {Write-Host "$GET_DEVICE_NAME Has been found and will be renamed" -ForegroundColor Green -BackgroundColor Black       }
    ElSE
    {Write-Host "Unable to ping $GET_DEVICE_NAME. Please verify name and connectivity" -ForegroundColor Red -BackgroundColor Yellow  }

    After the Else I need it to prompt for the device name again...

    I know it will probably be a simple solution...  Thanks in advance!

    Tuesday, November 13, 2018 8:06 PM

Answers

  • Good. Now that you have guessed at a couple of things and seem to be lost I will give you a link to a tutorial that will answer all of your first day questions:

    1. Microsoft Virtual Academy - Getting Started with Microsoft PowerShell
    2. PowerShell Documentation
    3. PowerShell Style Guidelines

    Please edit your post and post the code correctly with the code posting tool provided.  Fist read the style guide to learn how to correctly format code o that it is readable and consistent.


    \_(ツ)_/

    • Marked as answer by LutherY Wednesday, November 14, 2018 5:57 PM
    Tuesday, November 13, 2018 8:22 PM
  • If you really want all of the colorful messages then this is how to structure this:

    while(1){
        $devicename = Read-Host 'Please enter the device you are backing up'
        if(Test-Connection $devicename -Quiet){
            Write-Host "$devicename Has been found and will be renamed" -Fore Green -Back Black    
        }else{
            Write-Host "Unable to ping $devicename. Please verify name and connectivity" -Fore Red -Back Yellow
        }
    }
    "Test" CmdLets return a Boolean when asked.  They are designed to be directly tested so no need for a variable.


    \_(ツ)_/

    Wednesday, November 14, 2018 8:15 AM
  • Hi,

    Thanks for your question.

    About your question, I think you need to add a loop statement.

    $GET_DEVICE_NAME = Read-Host "Please enter the device you are backing up"
    $testConnection = Test-Connection $GET_DEVICE_NAME -Quiet
    while($testConnection -eq $false)
    { Write-Host "Unable to ping $GET_DEVICE_NAME. Please verify name and connectivity" -ForegroundColor Red -BackgroundColor Yellow
      $GET_DEVICE_NAME = Read-Host "Please enter the device you are backing up"
      $testConnection = Test-Connection $GET_DEVICE_NAME -Quiet
      if($testConnection -eq $true)
      {
      Write-Host "$GET_DEVICE_NAME Has been found and will be renamed" -ForegroundColor Green -BackgroundColor Black    
      break;}
    }

    Please read the link below to learn the loop statement of powershell.

    https://ss64.com/ps/while.html

    https://ss64.com/ps/break.html

    Best Regards,

    Lee


    Just do it.

    • Marked as answer by LutherY Wednesday, November 14, 2018 5:57 PM
    Wednesday, November 14, 2018 7:49 AM
  • Wrong loop.

    do{
        $GET_DEVICE_NAME = Read-Host 'Please enter the device you are backing up'
    }
    until(Test-Connection $GET_DEVICE_NAME -Quiet)


    \_(ツ)_/

    • Marked as answer by LutherY Wednesday, November 14, 2018 5:57 PM
    Wednesday, November 14, 2018 8:06 AM

All replies

  • Good. Now that you have guessed at a couple of things and seem to be lost I will give you a link to a tutorial that will answer all of your first day questions:

    1. Microsoft Virtual Academy - Getting Started with Microsoft PowerShell
    2. PowerShell Documentation
    3. PowerShell Style Guidelines

    Please edit your post and post the code correctly with the code posting tool provided.  Fist read the style guide to learn how to correctly format code o that it is readable and consistent.


    \_(ツ)_/

    • Marked as answer by LutherY Wednesday, November 14, 2018 5:57 PM
    Tuesday, November 13, 2018 8:22 PM
  • Hi,

    Thanks for your question.

    About your question, I think you need to add a loop statement.

    $GET_DEVICE_NAME = Read-Host "Please enter the device you are backing up"
    $testConnection = Test-Connection $GET_DEVICE_NAME -Quiet
    while($testConnection -eq $false)
    { Write-Host "Unable to ping $GET_DEVICE_NAME. Please verify name and connectivity" -ForegroundColor Red -BackgroundColor Yellow
      $GET_DEVICE_NAME = Read-Host "Please enter the device you are backing up"
      $testConnection = Test-Connection $GET_DEVICE_NAME -Quiet
      if($testConnection -eq $true)
      {
      Write-Host "$GET_DEVICE_NAME Has been found and will be renamed" -ForegroundColor Green -BackgroundColor Black    
      break;}
    }

    Please read the link below to learn the loop statement of powershell.

    https://ss64.com/ps/while.html

    https://ss64.com/ps/break.html

    Best Regards,

    Lee


    Just do it.

    • Marked as answer by LutherY Wednesday, November 14, 2018 5:57 PM
    Wednesday, November 14, 2018 7:49 AM
  • Wrong loop.

    do{
        $GET_DEVICE_NAME = Read-Host 'Please enter the device you are backing up'
    }
    until(Test-Connection $GET_DEVICE_NAME -Quiet)


    \_(ツ)_/

    • Marked as answer by LutherY Wednesday, November 14, 2018 5:57 PM
    Wednesday, November 14, 2018 8:06 AM
  • If you really want all of the colorful messages then this is how to structure this:

    while(1){
        $devicename = Read-Host 'Please enter the device you are backing up'
        if(Test-Connection $devicename -Quiet){
            Write-Host "$devicename Has been found and will be renamed" -Fore Green -Back Black    
        }else{
            Write-Host "Unable to ping $devicename. Please verify name and connectivity" -Fore Red -Back Yellow
        }
    }
    "Test" CmdLets return a Boolean when asked.  They are designed to be directly tested so no need for a variable.


    \_(ツ)_/

    Wednesday, November 14, 2018 8:15 AM
  • Hi,

    Thanks for your reply. 

    I have tested my loop, it works fine. Please point out where I am wrong. Your loop statement is really good.

    Thank you again.

    Best Regards,

    Lee


    Just do it.

    Wednesday, November 14, 2018 8:32 AM
  • Hi,

    Thanks for your reply. 

    I have tested my loop, it works fine. Please point out where I am wrong. Your loop statement is really good.

    Thank you again.

    Best Regards,

    Lee


    Just do it.

    Half of your code is unnecessary.

    Coding is not about writing as many lines of code as you can until the problem is solved.  It is about understanding the problem and designing a solution that matches the use and logic of the problem. Programming is a mathematical exercise.  It is about writing a formula that solves a problem.

    See: https://en.wikipedia.org/wiki/Software_design_pattern

    Learning to think in patterns and learning to recognize patterns is the foundation of all technology.  You can also think of it as a game to find the most direct and simple solution to a puzzle.


    \_(ツ)_/

    Wednesday, November 14, 2018 8:38 AM
  • Hi Jrv,

    You are right. Thank you.

    Best Regards,

    Lee


    Just do it.

    Wednesday, November 14, 2018 8:43 AM
  • Just trying to promote good skills and continued learning.  Any of us who give advise need to know th fundamentals correctly and need to advise using known methods and try not to fall into just copying what we see others doing.  The key to technical expertise is a deep understanding of the technology.

    A fun way to learn and understand PowerShell deeply is to read the "PowerShell Team Blog" in reverse -  read from the oldest post to the newest.  It is an excellent history and demonstration of most features of PowerShell with excellent discussions of why the components of PS have been designed and why.

    Oldest page here: https://blogs.msdn.microsoft.com/powershell/page/115/

    Read the page in descending order … 115 114 113 112 etc,


    \_(ツ)_/

    Wednesday, November 14, 2018 9:01 AM
  • Hi Jrv,

    Thank you. I will follow your fun way to learn Powershell deeply.

    Could you give me your email? 

    Best Regards,

    Lee


    Just do it.

    Wednesday, November 14, 2018 9:28 AM
  • There is no safe way to post email addresses.


    \_(ツ)_/

    Wednesday, November 14, 2018 9:31 AM
  • Thanks for the links. Sorry for the bad code. I was under the gun to get something working.
    Wednesday, November 14, 2018 2:44 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.

    Best Regards,
    Lee


    Just do it.

    Wednesday, November 14, 2018 3:30 PM
  • Hi LutherY,

    Great that you are new to PowerShell and you love it!

    If you would like to learn PowerShell I really recommed the follow book:

    Powershell step by step third edition - Ed wilson
    PS third edition - Ed Wilson


    Getting Started with PowerShell - Microsoft Virtual Academy
    P.S - You also could look at PowerShell.org and ask your questions their.

    Powershell is crossplatform also works on Linux / MacOS and other operating systems. It is not a full version of Powershell its currently running version 6.0.xx named PowerShell Core.

    Kind Regards
    Marinus Liefers
    System Administrator


    Wednesday, November 14, 2018 3:37 PM