I am trying to get common resources from a bunch of DCs ($DCs). I can run a function (called “Get-Resources”) on a local one and the Function works. How do I structure a Function, though, with Invoke-Command?
If it is a custom function (written by you or someone else), then it needs to be available on each one of the servers in $DCs. If it is available, try this:
I just noticed the ComputerName value is wrong as well. You are using For-EachObject, so you should use $_. I have made the correction in my previous post.
ah, not there’s too many DCs to copy that (Function) onto. Is there another method to leverage invoke-Command to gather essentially the same info, per server (mem, cpu, at least)?
You can take the contents of the function and pass it into the scriptblock as a string. Inside the scriptblock that’s running on the remote machine, you will create a new scriptblock from that string and then invoke it. If you need to pass arguments to the function then some changes would be required. Here’s a non-destructive demo you can run to test this. This assumes you have a variable that holds an array of server names