繁体   English   中英

Powershell将功能传递给远程命令

[英]Powershell Passing Function to remote command

我正在尝试将本地函数传递给远程代码,但未获得任何成功。 下面是我的代码。

#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

此代码将源文件复制到目标。 已创建功能来验证复制文件的md5和。 当我运行脚本时,脚本运行良好,但未调用功能代码。 调用函数还有其他需要做的事情吗?


更新

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

上面的代码抛出以下错误:

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

由于该函数是在您自己的本地范围内定义的,因此您还必须将其传递给远程会话。 通过测试,它似乎是作为字符串传递的,但是您可以使用[scriptblock]::Create()在远程会话中“重新创建”它:

Invoke-Command -Session $session -ScriptBlock { 
    param( $source, $destination, $application, $comparefunc)
    # do stuff
    # Now invoke the function that was provided
    [ScriptBlock]::Create($comparefunc).Invoke($source,$destination)
} -ArgumentList $site_path_local, $backup_path, $app, ${function:comparehash}

我发现$ Using命令可以在这里成为您的朋友。 如果使远程PSSession保持打开状态,则可以一次转移所有需要的功能,然后调用最后一个调用/使用这些功能的脚本块。

function comparehash($source, $destination)
{
    Write-Output "I like hash";
    if( $source -eq $destination) { PackThePipe $source; }
}

function PackThePipe($hash)
{
    Write-Output "Pushing $hash through the pipe";
}


$session = New-PSSession -ComputerName $srv -port 22 -Credential $cred  –Authentication CredSSP

#create the functions we need remotely
Invoke-Command $session { Invoke-Expression $Using:function:comparehash.Ast.Extent.Text; }
Invoke-Command $session { Invoke-Expression $Using:function:PackThePipe.Ast.Extent.Text; }

#call the scriptblock that utilizes those functions
Invoke-Command $session { $s = "12345"; $d="12345"; comparehash $s $d; }

Remove-PSSession $session;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM