I have written a few advanced functions that take advantage of remoting cmdlets such as Invoke-Command or Get-WMIObject and I have a general question regarding how to write these functions.
One function I have uses an external command and executes against the local computer by default. I have added credential and computername parameters and if these are specified then the command is executed using Invoke-Command against one or several remote computers.
Would you include Invoke-Command in your function or would it be better to use Invoke-Command outside the function? I also have some functions that call certain WMI cmdlets and use similar parameters for remoting capabilities.
Invoke-Command embedded in function:
Get-Something -computername server01 -credential domain\user01
Using Invoke-Command outside the function:
Invoke-Command -scriptblock {Get-Something} -computername server01 -credential domain\user01
I like my way but I can see that some may say it is not best practice. Please explain why you think one way is better than the other. Also please keep in mind that these functions may be shared with others that do not have a deep knowledge of Powershell.
Thanks!