receive an input and then run a command on it.

Good Afternoon 

Im trying to write a script that will receive an input that i give it and then run a command using the input that was given 

i have the below but it is currently not working 

$pcname=Read-Host -Prompt 'What is the name of the machine that is having issues with bbgrouter' (this will ask for the name of the PC that is having the issue)

ForEach ($i in $pcname) (i thought the Foreach command will use the input i gave it and then run the commands in the bracket)
{
Enter-PSSession $i  (i want to be able to remote into the machine so that it can run the below commands)
Stop-Process -Name BBGRouter,excel.exe -f (will stop the process on the remote machine)
Start-Sleep -s 3 
Start-Service -Name bbgrouter (then it will start only one of the services)
}

Any help will be greatly appreciated Thank you 

  • Aliases are good, carpal tunnel reducing of course , but there use is potentially a bad idea for those who'd follow you. It's more prudent to ensure you avoid using these in production code, to avoid inflicting pain on those who have maintain your stuff later and who potentially never made the effort to learn or care about short names. It's all about being kind to your current and future team mates.

    You also don't need to do this explicit remoting action of 'Enter-PSSession'. That's overkill for what you are after. With PSRemoting enabled, you can just use the Implicit Remoting 'Invoke-Command'

    Remember PowerShell is not about trying to cram all things in one-liners, that's very limiting. So, plan this a  full function with help documentation to all it to be treated and function just like a cmdlet.


    For your query - Here is a quick and dirty abbreviated option ---

    $pcnames = Read-Host -Prompt 'What is the name of the machine that is having issues with bbgrouter. Use a comma when entering more computer names.'
    $pcnames -split ',' | % { icm $_ -ScriptBlock { gps bbgrouter,Excel | Kill -F } };Start bbgrouter


    Thus, keep in mind when to use each remoting option

    Enter-PSSession --- A static interactive session you type what you want at the console and
    immediately see the results in the console, DOS prompt-like stuff.
    If it's multiple servers then you can use enter-pssession but it is always going
    to be serial, you do something session, exit and move on one server then you move
    onto another.

    As per the help file...
       The Enter-PSSession cmdlet starts an interactive session with a single
       remote computer. During the session, the commands that you type run on the
       remote computer, just as if you were typing directly on the remote computer.
       You can have only one interactive session at a time.

    New-PSSession --- creates a persistent connection to a remote server and is generally used when
    you have a series of commands to run on multiple servers at various stages of
    a larger script or workflow.

    As per the help file...
       Use a PSSession to run multiple commands that share data, such as a
       function or the value of a variable. To run commands in a PSSession , use
       the Invoke-Command cmdlet.

    Invoke-Command ---if you just want to stop\start a couple of services\process then you can do this
    without opening an explicit interactive or persistent connection

    As per the help file...
       The Invoke-Command cmdlet runs commands on a local or remote computer and
       returns all output from the commands, including errors. By using a single
       Invoke-Command command, you can run commands on multiple computers

  • In reply to postanote:

    THank you very much for the explanation i really appreciate it. I can better understand it now.
  • Good Afternoon
    Thank you for all the help but IF you have time can you please help me clean up the below script, as of right now i have the script doing the below
    1) force kill bbcomm and bbgrouter
    2) start bbgrouter
    3) telnet port 4321

    but as you can see all im doing for the telnet is just pasting the command 6 times in a row, Is there a way that powershell can run the command again if the telnet connection is not up and have it retry 6 times.

    Thank you


    $pcnames = Read-Host -Prompt 'What is the name of the machine that is having issues with bbgrouter. Use a comma when entering more computer names.'
    $pcnames -split ',' | % { Invoke-Command $_ -ScriptBlock { Get-Process bbgrouter,bbcomm | Kill -F }} ;
    Get-Service -Name bbgrouter -ComputerName $pcnames | Set-Service -Status Running

    Start-Sleep -s 3

    $servers = $pcnames
    $portToCheck = '4321'

    foreach ($server in $servers) {

    If ( Test-Connection $server -Count 1 -Quiet) {

    try {
    $null = New-Object System.Net.Sockets.TCPClient -ArgumentList $server,$portToCheck
    $props = @{
    Server = $server
    PortOpen = 'Yes'
    }
    }

    catch {
    $props = @{
    Server = $server
    PortOpen = 'No'
    }
    }
    }

    Else {

    $props = @{
    Server = $server
    PortOpen = 'Server did not respond to ping'
    }
    }

    New-Object PsObject -Property $props

    }

    Start-Sleep -s 3

    $servers = $pcnames
    $portToCheck = '4321'

    foreach ($server in $servers) {

    If ( Test-Connection $server -Count 1 -Quiet) {

    try {
    $null = New-Object System.Net.Sockets.TCPClient -ArgumentList $server,$portToCheck
    $props = @{
    Server = $server
    PortOpen = 'Yes'
    }
    }

    catch {
    $props = @{
    Server = $server
    PortOpen = 'No'
    }
    }
    }

    Else {

    $props = @{
    Server = $server
    PortOpen = 'Server did not respond to ping'
    }
    }

    New-Object PsObject -Property $props

    }

    Start-Sleep -s 3

    $servers = $pcnames
    $portToCheck = '4321'

    foreach ($server in $servers) {

    If ( Test-Connection $server -Count 1 -Quiet) {

    try {
    $null = New-Object System.Net.Sockets.TCPClient -ArgumentList $server,$portToCheck
    $props = @{
    Server = $server
    PortOpen = 'Yes'
    }
    }

    catch {
    $props = @{
    Server = $server
    PortOpen = 'No'
    }
    }
    }

    Else {

    $props = @{
    Server = $server
    PortOpen = 'Server did not respond to ping'
    }
    }

    New-Object PsObject -Property $props

    }

    Start-Sleep -s 3

    $servers = $pcnames
    $portToCheck = '4321'

    foreach ($server in $servers) {

    If ( Test-Connection $server -Count 1 -Quiet) {

    try {
    $null = New-Object System.Net.Sockets.TCPClient -ArgumentList $server,$portToCheck
    $props = @{
    Server = $server
    PortOpen = 'Yes'
    }
    }

    catch {
    $props = @{
    Server = $server
    PortOpen = 'No'
    }
    }
    }

    Else {

    $props = @{
    Server = $server
    PortOpen = 'Server did not respond to ping'
    }
    }

    New-Object PsObject -Property $props

    }

    Start-Sleep -s 3

    $servers = $pcnames
    $portToCheck = '4321'

    foreach ($server in $servers) {

    If ( Test-Connection $server -Count 1 -Quiet) {

    try {
    $null = New-Object System.Net.Sockets.TCPClient -ArgumentList $server,$portToCheck
    $props = @{
    Server = $server
    PortOpen = 'Yes'
    }
    }

    catch {
    $props = @{
    Server = $server
    PortOpen = 'No'
    }
    }
    }

    Else {

    $props = @{
    Server = $server
    PortOpen = 'Server did not respond to ping'
    }
    }

    New-Object PsObject -Property $props

    }

    Start-Sleep -s 3

    $servers = $pcnames
    $portToCheck = '4321'

    foreach ($server in $servers) {

    If ( Test-Connection $server -Count 1 -Quiet) {

    try {
    $null = New-Object System.Net.Sockets.TCPClient -ArgumentList $server,$portToCheck
    $props = @{
    Server = $server
    PortOpen = 'Yes'
    }
    }

    catch {
    $props = @{
    Server = $server
    PortOpen = 'No'
    }
    }
    }

    Else {

    $props = @{
    Server = $server
    PortOpen = 'Server did not respond to ping'
    }
    }

    New-Object PsObject -Property $props

    }

    Read-Host -Prompt "Press Enter to exit"
  • In reply to tonydacto1:

    I see that you have posted this twice, I assume to hit a wider audience.
    I have responded to both of your posts similarly.

    Remember there are always more elegant ways to achieve given use case, the below is just an idea for you to try.

    That is a lot of code, for your use case.
    Test defined port 
    On fail to connect, max retry 6 times

    $servers = @("$env:COMPUTERNAME",'L201T')
    $RetryCounter = 6


    foreach ($server in $servers)
    {
        If (( Test-NetConnection -ComputerName $server -Port '3389' ).PingSucceeded)
        {
            "$server is avaialble and responding on the defined port"
        }
        Else
        {
            While ($RetryCounter -ne 0)
            {
                $RetryCounter -= 1
                Write-Warning -Message "$server is not available or not responding on the defined port
                    Retrying the connection attempt. Retrying attempt countdown: $RetryCounter"

                If (( Test-CustomNetConnection -ComputerName $server -Port '3389'))
                {
                    "$server is available and responding on the defined port"
                }
                Else
                {
                    Write-Warning -Message "$server is still not available or not responding on the defined port"
                }
            }
        }
    }



    But, since you are on a legacy OS and PowerShell version, you'll not have the
    necessary modules for the Test-NetConnection cmdlet, Hence the reason for your
    entry...

    New-Object System.Net.Sockets.TCPClient -ArgumentList $server,$portToCheck

    But nothing stops you from fixing that for Win7 PowerShell usability.

    You don't have to install modules to use them.

    You can do this by using Implicit PowerShell remoting to proxy cmdlets from a
    higher OS version and hopefully by extension, higher PowerShell version, then
    executing in using the proxied set of cmdlets, via you downlevel client....
    Or take an approach like this...

    Now, note this is quick and dirty, but shoudl give you the direction you
    require to satisfy your use case.

    So, digest and modify as fits your needs.

     

    Function Test-CustomNetConnection($ComputerName, $Port)
    {
        $ErrorActionPreference = 'SilentlyContinue'
        New-Object System.Net.Sockets.TCPClient -ArgumentList $ComputerName,$Port
        $ErrorActionPreference = 'Continue'
    }

    $servers = @("$env:COMPUTERNAME",'L201T')
    $RetryCounter = 6


    foreach ($server in $servers)
    {
        If (( Test-CustomNetConnection -ComputerName $server -Port '3389'))
        {
            Write-Host "$server is available and responding on the defined port" -ForegroundColor Green
        }
        Else
        {
            While ($RetryCounter -ne 0)
            {
                $RetryCounter -= 1
                Write-Warning -Message "$server is not available or not responding on the defined port
                    Retrying the connection attempt. Retrying attempt countdown: $RetryCounter"

                If (( Test-CustomNetConnection -ComputerName $server -Port '3389'))
                {
                    "$server is avaialble and responding on the defined port"
                    Write-Host "$server is now available and responding on the defined port" -ForegroundColor Green
                }
                Else
                {
                    Write-Host "`t`t`t`tStill no response from server or defined port." -ForegroundColor Gray
                }
            }
        }
    }

     

    # results

    LP70 is available and responding on the defined port
    WARNING: L201T is not available or not responding on the defined port
                    Retrying the connection attempt. Retrying attempt countdown: 5
        Still no response from server or defined port.
    WARNING: L201T is not available or not responding on the defined port
                    Retrying the connection attempt. Retrying attempt countdown: 4
        Still no response from server or defined port.
    WARNING: L201T is not available or not responding on the defined port
                    Retrying the connection attempt. Retrying attempt countdown: 3
        Still no response from server or defined port.
    WARNING: L201T is not available or not responding on the defined port
                    Retrying the connection attempt. Retrying attempt countdown: 2
        Still no response from server or defined port.
    WARNING: L201T is not available or not responding on the defined port
                    Retrying the connection attempt. Retrying attempt countdown: 1
        Still no response from server or defined port.
    WARNING: L201T is not available or not responding on the defined port
                    Retrying the connection attempt. Retrying attempt countdown: 0
        Still no response from server or defined port.

  • In reply to postanote:

    Great thank you very much i really appreciate the help