While loop cuases argumental errors with cmdlets Powershell

Kyoto Tasori 21 Reputation points
2022-05-27T14:52:40.493+00:00

I was attempting to make a while loop for a input box and have it process it through another file though I keep getting a error that on another line it thinks its a positional argument

while ($true) {
$shell = Read-Host -Prompt "<"
Invoke-Command -ScriptBlock ([scriptblock]::Create((Get-Content ".\rescources\entry\Basehandler.ps1"))) -ArgumentList '$shell'
}

Error:

Read-Host : A positional parameter cannot be found that accepts argument 'Invoke-Command'.
At line:1 char:30

  • ... $shell = Read-Host -Prompt "<" Invoke-Command -ScriptBlock ([s ...
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidArgument: (:) [Read-Host], ParameterBindingException
  • FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.ReadHostCommand
Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,211 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
8,311 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,390 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 32,081 Reputation points
    2022-05-27T17:43:04.897+00:00

    At line:1 char:30

    You don't have any new line characters to separate the lines. Everything is on one line, so you need to separate cmdlets with a semicolon.

    while ($true) {$shell = Read-Host -Prompt "<";  Write-Host "You entered $shell"} 
    

    Personally, I like to put commands on separate lines to make it easier to read.

    while ($true) {
        $shell = Read-Host -Prompt "<"
        Write-Host "You entered $shell"
    }
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful