1

I'm brand new to powershell, and I'm finding a very odd behavior when I try and pass a parameter into a function:

Code

Param(
  [Parameter(Mandatory=$true)][string]$vhdSourceDir,
  [Parameter(Mandatory=$true)][string]$vhdDestinationDir,
  [Parameter(Mandatory=$true)][array]$hypervisorList,
  [int]$vhdKeep = 10
)


function getDirectoryBuildNumber ($dir) {

    # Returns a number like 627c6ddeb8776914 from a path like: 
    # c:\BulidAgent\work\627c6ddeb8776914\packer\windows\box\hyperv\win2012r2std-cheflatest-001.box
    return ( Get-ChildItem $dir | Where-Object {$_.Name -match "^[A-Za-z0-9]{16}$" } | Sort-Object LastAccessTime -Descending | Select-Object -First 1 )
}


function findBoxFile ($dir, $build) {

    echo "looking for build $build at $dir "
    echo "the dir is $dir and the build is $build"
    # e.g c:\BulidAgent\work\627c6ddeb8776914\packer\windows\box\hyperv\win2012r2std-cheflatest-001.box
    return ( Get-ChildItem $dir\$build\packer\windows\box\hyperv\ | Where-Object Extension -in '.box' | Sort-Object LastAccessTime -Descending | Select-Object -First 1 )
}

Function Main ()
{
    $THEBUILD=getDirectoryBuildNumber($vhdSourceDir)
    echo "THEBUILD is $THEBUILD"
    findBoxFile($vhdSourceDir,$THEBUILD)
    #echo "BOXFILE is $BOXFILE"

}
main

Problem

Here are the parameters that I call the script with:

.\boxMove.ps1 -vhdSourceDir C:\BuildAgent\work -vhdDestinationDir e:\ -hypervisorList 'foobar'

Here is the output it generates

THEBUILD is 527c6ddeb8776914
looking for build  at C:\BuildAgent\work 527c6ddeb8776914 
the dir is C:\BuildAgent\work 527c6ddeb8776914 and the build is 
Get-ChildItem : Cannot find path 'C:\BuildAgent\work 527c6ddeb8776914\packer\windows\box\hyperv\' because it does not exist.

The parameters show up out of order. For example, the phrase 'looking for build should appear like so"

looking for build 527c6ddeb8776914  at C:\BuildAgent\work

but it shows up as

looking for build  at C:\BuildAgent\work 527c6ddeb8776914

Also the phrase 'the dir is..' should read

the dir is C:\BuildAgent\work and the build is 527c6ddeb8776914 

but it reads as

the dir is C:\BuildAgent\work 527c6ddeb8776914 and the build is

Why is powershell not printing the strings in order?

1
  • 3
    First: Do not use parentheses to call PowerShell functions. Commented Apr 13, 2015 at 20:46

2 Answers 2

2

When you pass parameters to functions do not encapsulate them in parentheses and where multiple parameters are being passed, they should not be comma separated, instead a space should be used to separate them.

For instance:

findBoxFile($vhdSourceDir,$THEBUILD) 

should read:

findBoxFile $vhdSourceDir $THEBUILD

Then you'll find that this removes the issue you were encountering with the incorrectly ordered output.

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

Comments

0

Firstly, clean up your function definitions. Instead of

function (params){code}

you should use

function
{
Param ($Param1,$param2)
code
}

Also, when accepting an array as a param, instead of using [array], specify the array type, (probably string in your case), so that would be (line 4)

[Parameter(Mandatory=$true)][string[]]$hypervisorList,

It's hard do say exactly why the script fails, but I'd start by cleaning up the obvious issues and test again.

Comments

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.