none
include instructions in a script RRS feed

  • Question

  • Hello, so I'm trying to write a simple script for one of our users to restart a computer that's running a display in the lobby. The display sometimes freezes up and all we have to do is restart the compute and it loads the display on startup. So I got a simple script here:

    param(
         [Parameter(Mandatory=$True)]
         [ValidateSet("localhost,Computer1,Computer2")]
         [String]
         $ComputerName
         )
    
    Restart-Computer -ComputerName $ComputerName -Force -Verbose

    It works just fine, but it looks like this when it's ran:

    cmdlet Restart Computer.ps1 at command pipeline position 1 Supply values for the following parameters: ComputerName[0]:

    I'd like to get rid of the top two lines and replace it with some instructions like:

    "Type the NETBIOS name, IP address, or fully-qualified domain name of one or more computers in a comma-separated list.”

    I'm still pretty new to PowerShell, so I don't know if this is even possible.

    Any help would be appreciated. Thanks!

    Friday, July 31, 2015 9:24 PM

Answers

  • You can redesign your script to be interactive. Here is an example:


    Write-Host "Type the NETBIOS name, IP address, or fully-qualified domain name of one or more computers in a comma-separated list."
    $inputString = Read-Host "Computers"
    if ( -not $inputString ) {
      break
    }
    $computerList = $inputString -split ","
    foreach ( $computerName in $computerList ) {
      Restart-Computer -ComputerName $computerName -Force -Verbose
    }
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Alex.Franco Friday, July 31, 2015 9:45 PM
    Friday, July 31, 2015 9:35 PM
    Moderator
  • You can include a HelpMessage argument like so:

    param(
         [Parameter(Mandatory=$True,HelpMessage='Type the NETBIOS name, IP address, or fully-qualified domain name of one of more computers in a comma-separated list.')]
         [ValidateSet("localhost,Computer1,Computer2")]
         [String]
         $ComputerName
         )

    Try this out yourself and see if it'll suffice.

    Edit: What this ends up doing is adding a line (see below) that will give the user an option to view a help message by entering !?.

    cmdlet Restart Computer.ps1 at command pipeline position 1
    Supply values for the following parameters:
    (Type !? for Help.)
    ComputerName[0]: !?
    Type the NETBIOS name, IP address, or fully-qualified domain name of one of more computers in a comma-separated list.
    ComputerName[0]: 


    • Edited by tommymaynard Friday, July 31, 2015 9:44 PM
    • Marked as answer by Alex.Franco Friday, July 31, 2015 9:45 PM
    Friday, July 31, 2015 9:39 PM

All replies

  • You can redesign your script to be interactive. Here is an example:


    Write-Host "Type the NETBIOS name, IP address, or fully-qualified domain name of one or more computers in a comma-separated list."
    $inputString = Read-Host "Computers"
    if ( -not $inputString ) {
      break
    }
    $computerList = $inputString -split ","
    foreach ( $computerName in $computerList ) {
      Restart-Computer -ComputerName $computerName -Force -Verbose
    }
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Alex.Franco Friday, July 31, 2015 9:45 PM
    Friday, July 31, 2015 9:35 PM
    Moderator
  • You can include a HelpMessage argument like so:

    param(
         [Parameter(Mandatory=$True,HelpMessage='Type the NETBIOS name, IP address, or fully-qualified domain name of one of more computers in a comma-separated list.')]
         [ValidateSet("localhost,Computer1,Computer2")]
         [String]
         $ComputerName
         )

    Try this out yourself and see if it'll suffice.

    Edit: What this ends up doing is adding a line (see below) that will give the user an option to view a help message by entering !?.

    cmdlet Restart Computer.ps1 at command pipeline position 1
    Supply values for the following parameters:
    (Type !? for Help.)
    ComputerName[0]: !?
    Type the NETBIOS name, IP address, or fully-qualified domain name of one of more computers in a comma-separated list.
    ComputerName[0]: 


    • Edited by tommymaynard Friday, July 31, 2015 9:44 PM
    • Marked as answer by Alex.Franco Friday, July 31, 2015 9:45 PM
    Friday, July 31, 2015 9:39 PM
  • Both of those are the coolest things I've ever seen! They both work perfectly so I'll be using both examples and studying them for future use. 

    Thanks a ton, guys!

    Friday, July 31, 2015 9:45 PM