Why does PowerShell code behave different when called from a function?

Casey Chester

Experienced C# developer learning PowerShell here so I am sure I am just missing something silly. What I am trying to do is write a function that simply writes it's input to a temporary file in JSON format. I have code that works fine if I run it 'inline', but that same code writes an empty file when called in a function.

Here is the code:

function Dump-File {
    param (
        [Parameter(Mandatory=$true)]
        $Input
    )
    $tmp = New-TemporaryFile
    $Input | ConvertTo-Json | Out-File $tmp.FullName 
    Write-Output "Dump file written: $($tmp.FullName)"
}

$args = @{}
$args.Add('a', 1)
$args.Add('b', 2)
$args.Add('c', 3)
$args.Add('d', 4)

# results in json written to temp file
$tmp = New-TemporaryFile
$args | ConvertTo-Json | Out-File $tmp.FullName
Write-Output "args dumped: $($tmp.FullName)"

# results in empty temp file
Dump-File $args

Can anyone help me understand why the code called inline works but the same code does not work when I wrap it up as a function?

Sage Pourpre

$Input is an automatic variable.

Changing the name of your Dump-File parameter to $somethingelse will resolve your problem. Never use $input as a parameter or variable name.

Automatic variables are to be considered read-only.

About Automatic Variables

SHORT DESCRIPTION

Describes variables that store state information for PowerShell. These variables are created and maintained by PowerShell.

LONG DESCRIPTION

Conceptually, these variables are considered to be read-only. Even though they can be written to, for backward compatibility they should not be written to.

Here is a list of the automatic variables in PowerShell:

...

$INPUT

Contains an enumerator that enumerates all input that is passed to a function. The $input variable is available only to functions and script blocks (which are unnamed functions). In the Process block of a function, the $input variable enumerates the object that is currently in the pipeline. When the Process block completes, there are no objects left in the pipeline, so the $input variable enumerates an empty collection. If the function does not have a Process block, then in the End block, the $input variable enumerates the collection of all input to the function.

Source: About_Automatic_Variables

This information is also avaible through Get-help command

Get-Help about_Automatic_Variables

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

In ReactJS, why does `setState` behave differently when called synchronously?

Why does strlen cause SIGSEGV when called from JNI code (64bit)?

Why does this TensorFlow code behave differently when inside a test case?

Why does an async consumer called in __init__ in a Tornado RequestHandler behave differently from statically called?

Why does Rcpp function segfault only when called from R package but not when sourced directly via sourceCpp?

Why does python's `datetime.strptime` function not behave the same way when called using `functools.partial`?

Why does python evaluate the arguments of a function when defined and not when called?

Why does my powershell function fail to be called from C#?

Why does my function in a shared library which uses OpenMP hang when called from a subprocess via swig?

Swift generic constraint on method does not behave as expected when called from instance

Why does ForEach-Object behave differently when called as foreach?

Why does itertools.imap behave differently from map when None is passed as the mapping function?

Why does this FFMPEG command when called from Powershell generate white noise?

Why does Bash's "source" command behave differently when called from a function?

Why does the buffer overflow in this code behave different from what I expect?

Why does this code behave inconsistently?

Why view::make doesn't work when called from a different function in controller?

Why does bash -c "ulimit -Sn" have different output when called from a JVM?

tensorflow - Why an array is different when called in metrics function?

Why does the function behave like this?

Partialised glue function does not work when called in different environment

Why does printf behave differently when called from a Makefile?

Why does a line of code that is outside a function (when the function is called in another file) prints out?

Why does std::begin behave differently when called in a nested function

Why does 'await' break from the local function when called from main()?

Why does px and em behave different in this case?

Dynamically created Select Options behave differently when called from a button vs directly in function Materialize CSS

Why do my powershell code behaviors differently when called from another file as a module?

Why does Bash behave differently when compiled from source code?

TOP Ranking

HotTag

Archive