简体   繁体   English

PowerShell Invoke-Command的高级功能

[英]PowerShell Invoke-Command on an advanced function

I have an advanced function Copy-FilesHC that is available in a module file. 我具有模块文件中提供的高级功能Copy-FilesHC This function copies some files from the Source to the Destination folder and generates some output for in a log file. 此功能将某些文件从“ Source复制到“ Destination文件夹,并在日志文件中生成一些输出。

The function works fine locally: 该函数在本地工作正常:

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

It also works on a remote machine: 它也可以在远程机器上工作:

# 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

However, I can't seem to figure out how I can have it pass the output to a log file like in the first command. 但是,我似乎无法弄清楚如何使输出像第一个命令一样将输出传递到日志文件。 When I try the following, it fails: 当我尝试以下操作时,它将失败:

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.

As indicated here I thought the $ sign for the ScriptBlock was incorrect. 如此处所示我认为ScriptBlock$ sign不正确。 But this way I don't need to put my advanced function in a ScriptBlock to copy it over as it now happens automatically while it's only available within the module. 但是通过这种方式,我不需要将高级功能放在ScriptBlock中进行复制,因为它现在自动发生,而仅在模块内可用。 So I just need to find out how to capture the output in the log file. 因此,我只需要找出如何在日志文件中捕获输出即可。

Thank you for your help. 谢谢您的帮助。

Found the solution just a few minutes ago: 几分钟前找到了解决方案:

# 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

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

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