I am trying to pass local function to remote code but not getting any success. Below is my code.
#variables defined over here
...
function comparehash ($source, $destination)
{
$sourcefiles = @{}
...
}
$securePassword = ConvertTo-SecureString -AsPlainText -Force $Password
$cred = New-Object System.Management.Automation.PSCredential $Username, $securePassword
$session = New-PSSession -ComputerName $srv -port 22 -Credential $cred –Authentication CredSSP
Invoke-Command -Session $session -ScriptBlock {
param( $source, $destination, $application)
#Write-Host "This is" $source
# Take backup of the site first
Copy-Item $source\$application $destination -Recurse -force
${function:comparehash}
} -ArgumentList $site_path_local, $backup_path, $app
Remove-PSSession -Session $session
This code is copying source files to destination. Function has been created to validate md5 sum of the copied files. When I run the script script runs fine but it doesn't call the function code. Is there anything additional need to be done to call function?
Update
Invoke-Command -Session $session -ScriptBlock {
param( $source, $destination, $application, $fundef, $comparefunc )
Write-Host "This is" $source, $destination
# Take backup of the site first
#Copy-Item $source\$application $destination -Recurse -force
[ScriptBlock]::($comparefunc).Invoke($source,$destination)
#comparehash $site_path_local $backup_path
} -ArgumentList $site_path_local, $backup_path, $app, ${function:comparehash}
Remove-PSSession -Session $session
Above code is throwing following error:
PS C:\Windows\system32> C:\Users\vijay.patel\Documents\myscripts\Env_Refresh.ps1
This is F:\inetpub F:\Env_Backup\Refresh_Backup\
You cannot call a method on a null-valued expression.
+ CategoryInfo : InvalidOperation: (Invoke:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
+ PSComputerName : 172.16.82.124