Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling custom functions from inside Invoke-Parallel #27

Open
bab5470 opened this issue Aug 17, 2015 · 4 comments
Open

Calling custom functions from inside Invoke-Parallel #27

bab5470 opened this issue Aug 17, 2015 · 4 comments

Comments

@bab5470
Copy link

bab5470 commented Aug 17, 2015

I'm trying to call some custom functions inside of Invoke-Parrallel as such:

if ($xmlfile.configuration.DBAndFilesBackup.item("MSSQL").haschildnodes) {
    foreach ($backup in $xmlfile.configuration.DBAndFilesBackup.MSSQL.backup) {     

        Invoke-Parallel -ImportVariables -Throttle 2 {

            # Create a unique log file for this particular backup job
            $log="$ScriptDir\logs\corebackup\$ScriptName $($backup.Name) $DateStamp.log"

            # Begin the Backup
            BeginBackup $backup.Name $log

            # Backup the mssql database
            MSSQLBackup $backup.Name $backup.DBPath 

            # Backup the files
            FilesBackup $backup.Name $backup.FolderName $backup.FilesPath 

            # End the Backup
            EndBackup $backup.FolderName $backup.Name $log $emailTo

                }
    }
}

However when I do this I get the following error:

Get-RunspaceData : The term 'BeginBackup' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\scripts\includes\invoke-parallel.ps1:591 char:13
+             Get-RunspaceData -wait
+             ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (BeginBackup:String) [Write-Error], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException,Get-RunspaceData

Get-RunspaceData : The term 'MSSQLBackup' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\scripts\includes\invoke-parallel.ps1:591 char:13
+             Get-RunspaceData -wait
+             ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (MSSQLBackup:String) [Write-Error], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException,Get-RunspaceData

Get-RunspaceData : The term 'FilesBackup' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\scripts\includes\invoke-parallel.ps1:591 char:13
+             Get-RunspaceData -wait
+             ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (FilesBackup:String) [Write-Error], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException,Get-RunspaceData

Get-RunspaceData : The term 'EndBackup' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\scripts\includes\invoke-parallel.ps1:591 char:13
+             Get-RunspaceData -wait
+             ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (EndBackup:String) [Write-Error], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException,Get-RunspaceData

What am I doing wrong and how can I avoid this error?

As a side note - how to i avoid entering an input variable? I don't think I necessarily have anything to input if that makes sense?

Thanks
Brad

@RamblingCookieMonster
Copy link
Owner

Hi Brad!

Invoke-Parallel won't pull in function definitions - as a workaround, if you import the module or load up the functions inside the Invoke-Parallel scriptblock, it should work.

I'll look at adding a parameter to allow including functions from the current PowerShell session, but might take a bit of time.

I still use Invoke-Parallel for a number of ad-hoc tasks and scripts, but do check out @proxb's PoshRSJob; it includes this functionality already, if I recall correctly.

Thanks!

Warren

@bab5470
Copy link
Author

bab5470 commented Aug 17, 2015

I'll look at adding a parameter to allow including functions from the current PowerShell session, but might take a bit of time.

That would be great - thanks.

My reservation with using a PS module is portability of the script. I'd like to keep the code all self contained in one folder so that migrating it to a new server is as simple as copying the script folder.

Having the extra step of having to install a custom module isn't the end of the world but also not ideal either - particularly if a future maintainer isn't familiar with the script.

The reason for trying to use Invoke-Parallel is that I need to kick off a number of backjob jobs (as you may have guessed from my function names) but I also need transcription (powershell logging).

I was attempting to use the powershell start-job native function but I guess that doesn't support transcription when running jobs. :(

Would invoke-parallel support powershell transcripts if i created a POSH module with my custom functions? Any idea if PoshRSJob does either?

Thanks,
Brad

@RamblingCookieMonster
Copy link
Owner

Alrighty! This was added in #46. You can now use -ImportFunctions

Now that I think about it... there might be some oddities for functions defined in modules - will have to revisit that.

Cheers!

@pathaknikhil87
Copy link

Hi, I am trying to use this function to call my custom function in parallel, but it fails..
1: I am not able to pass multiple input parameters to my custom function
2. Custom Function doesnt respond correctly when invoke using this function

Invoke-Parallel -ScriptBlock { Get-vspherevmname -vmname $_.NAME -org "12345" -vapp "12345"} -InputObject $vm -ImportFunctions

Below is how i call the function individually and using invoke-parallel

PS C:\Users\e554> Get-vspherevmname -vmname $vmv[0].Name -org $org.Name -vapp $Vappp.Name
output: R511-SRVA-WyU3

PS C:\Users\e554> $bab = Invoke-Parallel -ScriptBlock { Get-vspherevmname -vmname $_.NAME -org "ID14434-1-1231231" -vapp "12345"} -InputObject $vmv -ImportFunctions
Output:Catch Block

Can you please help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants