PowerShell Invoke-Command在高级功能上

时间:2015-01-30 08:26:55

标签: powershell remoting invoke-command

我有一个模块文件中提供的高级函数Copy-FilesHC。此函数将一些文件从Source复制到Destination文件夹,并在日志文件中生成一些输出。

该功能在本地工作正常:

Copy-FilesHC -Source $Src -Destination $Des *>> $Log

它也适用于远程计算机:

# For remote use we need to make it available first
Import-Module (Get-Command Copy-FilesHC).ModuleName

Invoke-Command -Credential $Cred -ComputerName $Host -ScriptBlock ${Function:Copy-FilesHC} -ArgumentList $LocalSrc, $LocalDes

但是,我似乎无法弄清楚如何将输出传递给日志文件,就像在第一个命令中一样。当我尝试以下操作时,它会失败:

Invoke-Command -Credential $Cred -ComputerName $Host -ScriptBlock ${Function:Copy-FilesHC *>> $Log} -ArgumentList $LocalSrc, $LocalDes

Invoke-Command : Cannot validate argument on parameter 'ScriptBlock'. The argument is null. Provide a vali
d value for the argument, and then try running the command again.

如上所示here我认为$ sign的{​​{1}}不正确。但是这样我就不需要将我的高级函数放在ScriptBlock中来复制它,因为它现在自动发生,而它只在模块中可用。所以我只需要了解如何捕获日志文件中的输出。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

几分钟前找到解决方案:

# For remote use we need to make it available first
Import-Module (Get-Command Copy-FilesHC).ModuleName

Invoke-Command -Credential $Cred -ComputerName $Host -ScriptBlock ${Function:Copy-FilesHC} -ArgumentList $LocalSrc, $LocalDes *>> $Log