Combining functions to pass variables between the two when the first is initalized

  • I have the two script below, the first responses to a windows file creation event on a specified folder, and then writes data to a log file. The second, does a hash on a file and writes to the console. I have tried many different ways to get the windows file creation script to call the hash script but not having any luck. Been trying to pass the $path and $name variables from the first to the second. Ultimately I want to have the file creation data being combined with the hash data (for the specified file that was created) and have that all write inline to the log file. Hoping someone can see the tree through the forest where as I can't? Thanks

    # Below is the function for created file events on folder

    function get-newfiles{

    $folder = 'C:\Scripts'

    $filter = '*.*' # <-- set this according to your requirements

    $destination = '<full path to the destination folder>'

    $fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{

    IncludeSubdirectories = $true # <-- set this according to your requirements

    NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'

    }

    $onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {

    $path = $Event.SourceEventArgs.FullPath

    $name = $Event.SourceEventArgs.Name

    $changeType = $Event.SourceEventArgs.ChangeType

    $timeStamp = $Event.TimeGenerated

    $outfile = "$name, $changeType, $timeStamp"

    $outfile | out-file c:\scripts\outcreatefile.txt -append

    }

    }

    --------------------------------------------------------------

    # Below is the hash table script-function

    function Get-MD5([System.IO.FileInfo] $file = $(throw 'Usage: Get-MD5 [System.IO.FileInfo]'))

    {

    $stream = $null;

    $cryptoServiceProvider = [System.Security.Cryptography.MD5CryptoServiceProvider];

    $hashAlgorithm = new-object $cryptoServiceProvider

    $stream = $file.OpenRead();

    $hashByteArray = $hashAlgorithm.ComputeHash($stream);

    $stream.Close();

    ## We have to be sure that we close the file stream if any exceptions are thrown.

    trap

    {

    if ($stream -ne $null)

    {

    $stream.Close();

    }

    break;

    }

    return [string]$hashByteArray;

    }

    A clever person solves a problem. A wise person avoids it. ~ Einstein
    select cast (0x5365616E204465596F756E67 as varchar(128))

  • Without getting into the guts of your script, you can pass variables to other functions by setting the scope.

    This doesn't work:

    function AssignVar

    { $Var1 = "abc" }

    function DisplayVar

    { Write-Host $Var1 }

    AssignVar

    DisplayVar

    But this does:

    function AssignVar

    { $Script:Var1 = "abc" }

    function DisplayVar

    { Write-Host $Var1 }

    AssignVar

    DisplayVar

    Hope this helps.

    - Jeff

  • Thanks very much. Rather then continuing to go in circles I turned to C# to get this to work. However, now that I have something to chew on I will see if I can get it to work in PowerShell.

    jjtetzlo (3/22/2012)


    Without getting into the guts of your script, you can pass variables to other functions by setting the scope.

    This doesn't work:

    function AssignVar

    { $Var1 = "abc" }

    function DisplayVar

    { Write-Host $Var1 }

    AssignVar

    DisplayVar

    But this does:

    function AssignVar

    { $Script:Var1 = "abc" }

    function DisplayVar

    { Write-Host $Var1 }

    AssignVar

    DisplayVar

    Hope this helps.

    A clever person solves a problem. A wise person avoids it. ~ Einstein
    select cast (0x5365616E204465596F756E67 as varchar(128))

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply