0

Function.psm1

function split-release {
Param
(
  [string]$Release
)

# Regex to match semantic versioning
if($release -notmatch '\d+[.]\d+[.]\d+')
{
    Write-Error "Invalid Release Number"
}

# Split the version string into an array
$RelVersion=$release.split(".")

@{"Major"=$RelVersion[0];"Minor"=$RelVersion[1];"Patch"=$RelVersion[2]}
}

split.psm1

Import-Module .\Function.psm1
split-release

I call the function as

 PS c:\ > .\split.psm1 1.2.3

It doesn't print any output or errors out.

2
  • You don't call a powershell function with parentheses. You call it like this funcname "arg1" $arg2. Commented May 28, 2015 at 20:01
  • Tried but not good split-release "$release" returns nothing Commented May 28, 2015 at 20:24

2 Answers 2

2

Seems to print to the console when I test importing just that function in a psm1 file, and in a separate file import the module and then pass in "0.0.0" to split-release.

The .\ syntax indicates that the desired file is in the same directory as the caller. Is that the case with your files? Is there any additional code that may be obscuring output?

Other minor points:

  • Write-Host will not write to your output stream. In PS the alias to echo is Write-Output.
  • You can use a hash table to return these as a single object with properties.

Modified function:

function split-release {
    Param
    (
      [string]$Release
    )

    # Regex to match semantic versioning
    if($release -notmatch '\d+[.]\d+[.]\d+')
    {
        Write-Error "Invalid Release Number"
    }

    # Split the version string into an array
    $RelVersion=$release.split(".")

    @{"Major"=$RelVersion[0];"Minor"=$RelVersion[1];"Patch"=$RelVersion[2]}
}

Output:

PS C:/ > $release = "1.2.3"
PS C:/ > $result = split-release -Release $release

PS C:/ > $result.Major
1
PS C:/ > $result.Minor
2
PS C:/ > $result.Patch
3

More Info:

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

7 Comments

Thank you. But I cannot hard core any values, since this will be used for automation later. I want to have 2 files. file1 should have the actual function and file2 should call it. I should be able to pass the release value through file2 only. I tried splitting your code but that doesn't work. Sorry
Nothing needs to be hardcoded, the "0.0.0" is a default value in the split method that a $null wouldn't reach in your env since you throw an exception on no input in your calling code. It is there for demo purposes. I understand the problem you're trying to solve. I don't understand the failures you're experiencing. For example: " I tried splitting your code but that doesn't work." This is a pretty simple string split. What specifically is not working in your environment? I can't reproduce the failures you're describing, and I can't help you troubleshoot without details.
sorry, ill explain it better. my file1 and file2 are in same directory. I used your modified function portion in file1.psm1, In file2, I imported it as Import-Module .\file1.psm1 and called the function. Finally I ran file2.psm1 as "PS c:/ > file2.psm1 1.2.3"
Weird. I'm doing pretty much that exactly except with different file names and it works fine. So for you it just isn't printing anything? No errors or blank lines printing? And what version of powershell are you using? (just use $PSVersionTable in your console to find that.)
version 3.0 no it doesn't print any empty line or erring out. which line of yours is exactly printing those outputs? should i add the write-output to the function?
|
0

I tried this finally and it appears to work.

File1.psm1
function split-release ($release) {

$RelVersion=$release.split(".")
$Relmajor=$RelVersion[0]
$Relminor=$RelVersion[1]
$Relpatch=$RelVersion[2]
write-host $Relmajor $Relminor $Relpatch
}

File2.ps1
param(
 [string]$release = $(throw "Release number required as script parameter")
)
Import-Module ./File1.psm1
split-release "$release"

Finally run it as PS C:\ > ./file2.ps1

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.