Function from imported script doesn't execute

Viewed 47

i write a script with a function.

here is the script with the function:

function GenerateHashesForProjects(){
    [array]$result=@()
    Write-Output "Genrate Hash Values"
    $dependencyFolder = Join-Path -Path $PSScriptRoot -ChildPath "..\..\Sources\_Dependencies"
    #get all folder in a list below the dependency folder expect the "Modules" folder
    $dependencyContent = Get-ChildItem -Path $dependencyFolder | where {$_.PSIsContainer -and ($_.Name -notlike  "*Modules*")}

    #Fill the result array with the project file name and the depending hash value of this file
    foreach ($item in $dependencyContent) {
        $denpencyProjects = Get-ChildItem -Path $item.Fullname | where { ($_ -like "*.csproj") }
        $hashValue = (Get-FileHash $denpencyProjects.FullName -Algorithm MD5).Hash
        $name = $denpencyProjects.Name
        Write-Output "name: $name `nvalue: $hashValue"
        $result += @($denpencyProjects.Name, $hashValue)
    }
    return $result
}

That script works fine.

Now i want to use this function also in another script. So i import the script and define a variable with that function. Here is the issue if a call the function without the variable it works fine but with the variable definition not, why?

Here is the second script with the import:

. Join-Path -Path $PSScriptroot -ChildPath "..\..\Build\Tools\GenerateHashesForProjects.ps1"
[array]$dependencyFileValues = GenerateHashesForProjects

This test works fine:

. Join-Path -Path $PSScriptroot -ChildPath "..\..\Build\Tools\GenerateHashesForProjects.ps1"
GenerateHashesForProjects
1 Answers

since you didn't post any responses to questions [grin], here is one way to rewrite your code.

what it does ...

  • creates an advanced function
  • uses the recommended name format for such
  • does not supply the "otta be there" Comment Based Help [grin]
  • defines the parameters
    only the $Path is required.
  • defines but does not use a begin {} block
  • defines a process {} block
  • grabs a list of the dirs that branch from the source path
  • filters out the dirs that are in the $ExcludeDirList
  • gets the files in those dirs that match the $FileFilter
  • iterates thru that list
  • builds a [PSCustomObject] for each file with the desired details
    you can add or remove them as needed.
  • sends that PSCO out to the calling code

the line that calls the function stores the entire set of results into the $Result variable and then shows that on screen.

a few notes ...

  • i had to change a lot of your details since i have no csproj files
  • there are no "what is happening" lines
    if you need that, you can easily add such. i would NOT use Write-Output, tho, since that will pollute your output data.
  • there is no error detection OR error handling

here's the code ...

function Get-ProjectFileHash
    {
    <#
    CommentBasedHelp goes here
    #>
    [CmdletBinding ()]
    Param 
                                                                                                                                        (
    [Parameter (
        Mandatory,
        Position = 0
        )]
        [string]
        $Path,

    [Parameter (
        Position = 1
        )]
        [ValidateSet (
            'MD5',
            'MACTripleDES',
            'RIPEMD160',
            'SHA1',
            'SHA256',
            'SHA384',
            'SHA512'
            )]
        [string]
        $Algorithm = 'MD5',
        
    [Parameter (
        Position = 2
        )]
        [string[]]
        $ExcludeDirList,

    [Parameter (
        Position = 3
        )]
        [string]
        $FileFilter
    )

    begin {}

    process
        {
        $ProjDirList = Get-ChildItem -LiteralPath $Path -Directory |
            Where-Object {
                # the "-Exclude" parameter of G-CI is wildly unreliable
                #    this avoids that problem [*grin*] 
                # build a regex OR listing to exclude
                $_.Name -notmatch ($ExcludeDirList -join '|')
                }

        $FileList = Get-ChildItem -LiteralPath $ProjDirList.FullName -File -Filter $FileFilter

        foreach ($FL_Item in $FileList)
            {
            [PSCustomObject]@{
                FileName = $FL_Item.Name
                DirName = $FL_Item.Directory
                Algorithm = $Algorithm
                Hash = (Get-FileHash -LiteralPath $FL_Item.FullName -Algorithm $Algorithm).Hash
                }
            }

        }

    end {}

    } # end >>> function Get-ProjectFileHash

$Source = 'C:\ProgramData\chocolatey\lib'
$NotWanted = 'choco', '7zip', 'kb', 'bad', 'bkp'
$Filter = '*.nupkg'

$Result = Get-ProjectFileHash -Path $Source -Algorithm MD5 -ExcludeDirList $NotWanted -FileFilter $Wanted

$Result

truncated output ...

FileName                  DirName                                           Algorithm Hash                            
--------                  -------                                           --------- ----                            
autohotkey.nupkg          C:\ProgramData\chocolatey\lib\autohotkey          MD5       35A1B894AEA7D3473F3BBCBF5788D2D6
autohotkey.install.nupkg  C:\ProgramData\chocolatey\lib\autohotkey.install  MD5       EFE8AD812CBF647CFA116513AAD4CC15
autohotkey.portable.nupkg C:\ProgramData\chocolatey\lib\autohotkey.portable MD5       D31FA1B5496AAE266E4B0545835E9B19

[*...snip...*] 

vcredist2015.nupkg        C:\ProgramData\chocolatey\lib\vcredist2015        MD5       56321731BC0AEFCA3EE5E547A7A25D5E
vlc.nupkg                 C:\ProgramData\chocolatey\lib\vlc                 MD5       8177E24675461BDFF33639BF1D89784B
wiztree.nupkg       
Related