0

I have the following code down below that will take an array and write out the correlating pixel colors to the powershell window. The array itself is to form an image. At this point I just have the one image as an example but intend to add more arrays to generate more images. When that time comes I want to be able to pass that array into the function to draw it out. As I have my code now I am getting an error saying

"Cannot index into a null array"

How do I properly pass an Array object into a function?

$Colors = @{
    1         =   'White'               
    2         =   'Black'         
    3         =   'DarkBlue'    
    4         =   'DarkGreen'     
    5         =   'DarkCyan'      
    6         =   'DarkRed'       
    7         =   'DarkMagenta'   
    8         =   'DarkYellow'    
    9         =   'Gray'          
    10        =   'DarkGray'      
    11        =   'Blue'          
    12        =   'Green'         
    13        =   'Cyan'          
    14        =   'Red'           
    15        =   'Magenta'       
    16        =   'Yellow'         
}

$omg  =   @(2,2,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1), 
          @(2,2,2,1,1,1,1,2,2,2,2,2,2,2,2,2,2,1,1,1,1,2),
          @(2,2,2,2,2,1,1,1,2,2,2,2,2,2,2,2,1,1,1,2,2,2),
          @(2,2,2,2,2,1,1,1,2,2,2,2,2,2,2,2,1,1,1,2,2,2),
          @(2,2,2,2,2,1,1,1,2,2,2,2,2,2,2,2,1,1,1,2,2,2),
          @(2,2,2,2,1,1,1,1,2,2,2,2,2,2,2,2,1,1,1,1,2,2),
          @(2,2,2,1,1,1,1,2,2,2,2,2,2,2,2,2,2,1,1,1,1,2),
          @(2,2,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1),
          @(2,2,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1),
          @(2,2,1,1,1,1,2,2,2,1,1,1,1,1,1,2,2,2,1,1,1,1),
          @(2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1),
          @(2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1),
          @(2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2),
          @(2,2,2,2,1,1,1,1,1,1,2,2,2,2,1,1,1,1,1,1,2,2),
          @(2,2,2,2,1,1,1,1,1,2,2,2,2,2,2,1,1,1,1,1,2,2),
          @(2,2,2,2,1,1,1,1,2,2,2,2,2,2,2,2,1,1,1,1,2,2),
          @(2,2,2,2,1,1,1,1,2,2,2,2,2,2,2,2,1,1,1,1,2,2),
          @(2,2,2,2,1,1,1,1,2,2,2,2,2,2,2,2,1,1,1,1,2,2),
          @(2,2,2,2,1,1,1,1,2,2,2,2,2,2,2,2,1,1,1,1,2,2),
          @(2,2,2,2,1,1,1,1,1,2,2,2,2,2,2,1,1,1,1,1,2,2),
          @(2,2,2,2,1,1,1,1,1,1,2,2,2,2,1,1,1,1,1,1,2,2),
          @(2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2),
          @(2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2),
          @(2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2),
          @(2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,2,2,2,2,2,2,2)


Function PS-Draw { 
[CmdletBinding()]

param ( 
[Parameter (Mandatory = $True, ValueFromPipeline = $True)]
[Alias("I")]
$Image

)

cls
       
foreach ($row in $Image)
{
  foreach ($position in $row) {
    Write-Host "  " -NoNewline -BackgroundColor $Colors[$position]; Start-Sleep -m 10
  }
  Write-Host ""
}
}

PS-Draw -Image $omg

1 Answer 1

1

Just cast your Image parameter to [array] or [object[]] and remove ValueFromPipeline = $True:

function PS-Draw { 
    [CmdletBinding()]
    param ( 
        [Parameter (Mandatory = $True)]
        [Alias("I")]
        [array]$Image
    )

    cls
   
    foreach ($row in $Image) {
      foreach ($position in $row) {
        Write-Host "  " -NoNewline -BackgroundColor $Colors[$position]
        Start-Sleep -m 10
      }
      Write-Host ""
    }
}

Since the function no longer takes input via the pipeline, you need to call it using

PS-Draw -Image $omg

OR, if you do want to be able to send the array by piping it to the function, do:

function PS-Draw { 
    [CmdletBinding()]
    param ( 
        [Parameter (Mandatory = $True, ValueFromPipeline = $True)]
        [Alias("I")]
        [object[]]$Image
    )

    # if the data is sent through the pipeline, use $input to collect is as array
    if ($PSCmdlet.MyInvocation.ExpectingInput) { $Image = @($input) }

    cls
   
    foreach ($row in $Image) {
      foreach ($position in $row) {
        Write-Host "  " -NoNewline -BackgroundColor $Colors[$position]
        Start-Sleep -m 10
      }
      Write-Host ""
    }
}

Now you can also call the function using

$omg | PS-Draw

Result:

enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

ive tried that this is the error I get Cannot index into a null array. At line:14 char:5 + Write-Host " " -NoNewline -BackgroundColor $Colors[$position]; S ... + + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : NullArray
@JakobyScream I'm using your example and both methods work just fine..
link im really confused then, I copy and pasted your code and still get the same error. You've actually answered other questions I've had before though, you are appreciated
I stand corrected, I had a minor error, your method works just fine. You are wonderful and again appreciated
@zett42 AFAIK, you could leave out the line if ($PSCmdlet.MyInvocation.ExpectingInput) { $Image = @($input) }, but then you need to prefix the array you're sending with a unary array operator (aka the comma) and call the function with ,$omg | PS-Draw. In fact, what you are sending is a single-item array, which gets unravelled by PowerShell leaving the function with the original array.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.