1

I am trying to Invoke a function block that does not require additional arguments in the Invoke-Command cmdlet.

$hostList = (Read-Host "Enter IP addresses/hostnames of hosts you want to connect to (Comma seperated)").Split(',');
    foreach($_ in $hostList){
       Invoke-Command -ComputerName $_ -Credential Administrator -ScriptBlock{$Function:PCInfo}
    }

My Function block looks something like this (testing purposes):

Function PCInfo{
       $winVersion = gwmi -class Win32_OperatingSystem | select Caption,Version,CSName,OSArchitecture
       $processorInfo = gwmi -class Win32_Processor | select Name,Manufacturer,MaxClockSpeed
       $diskInfo = gwmi -class Win32_LogicalDisk | select @{Label=’Drive Letter’;Expression={$_.DeviceId}}, @{Label=’Size (GB)’;Expression={"{0:N2}" -f ($_.Size/1GB)}}, @{Label=’Free Space (GB)’;Expression={"{0:N2}" -f($_.freespace/1GB)}} 
       $routingTable = gwmi win32_IP4RouteTable | select Destination,Mask,@{Label=’Next Hop’;Expression={$_.NextHop}} -uniq | format-table 
       $timeObject = gwmi -class Win32_OperatingSystem
}

I have two problems occuring when using this code:

  1. This does not result in any output. I searched SO and found similar questions but I can only find similar scenarios where the function uses additional arguments. Seeing as this is a stand-alone type of function I can't figure out why it doesn't work.

  2. When running this script as a regular user, the credential box pops up. I fill in Administrator credentials, yet I get the following error:

Credential prompt

[127.0.0.1] Connecting to remote server 127.0.0.1 failed with the following error message : Access is denied.*

I wonder why this happens, seeing as I fill in (local) Administrator credentials?

1 Answers1

0

To the first question, the scope of your remote script won't understand local functions or variables. Perhaps instead try this:

# Create a ScriptBlock to contain the content.
$script = 
{ 
   # WinVersion
   $WinVersion = gwmi -class Win32_OperatingSystem | select Caption,Version,CSName,OSArchitecture
   # ProcessorInfo
   $ProcessorInfo = gwmi -class Win32_Processor | select Name,Manufacturer,MaxClockSpeed
   # DiskInfo
   $DiskInfo = gwmi -class Win32_LogicalDisk | select @{Label=’Drive Letter’;Expression={$_.DeviceId}}, @{Label=’Size (GB)’;Expression={"{0:N2}" -f ($_.Size/1GB)}}, @{Label=’Free Space (GB)’;Expression={"{0:N2}" -f($_.freespace/1GB)}} 
   # RoutingTable
   $RoutingTable = gwmi win32_IP4RouteTable | select Destination,Mask,@{Label=’Next Hop’;Expression={$_.NextHop}} -uniq 
   # TimeObject
   $TimeObject = gwmi -class Win32_OperatingSystem 

   # Write all results to ScriptBlock's output
   $WinVersion, $ProcessorInfo, $DiskInfo, $RoutingTable, $TimeObject
}

$hostList = (Read-Host "Enter IP Address or Host (Comma separated)").Split(',');
foreach($_ in $hostList)
{
   $output = Invoke-Command -ComputerName $_ -Credential Administrator -ScriptBlock $script

   #Output can be used in this scope before the next iteration of the loop.
   $output
}

You could also define the function in your remote script, or pass content as an argument, but this seemed more pragmatic since the ScriptBlock is just a container for these gwmi calls. It is up to you as to how to handle or use that output once you receive it. Be sure to check the code for notes about scope!

To your second question... see this Super User question for powershell remoting as a non-administrator. You will need to configure non-administrator remoting (as an administrator) from the local machine, even if you have administrator credentials to access remote machine.

Community
  • 1
  • 1
Anthony Neace
  • 25,013
  • 7
  • 114
  • 129
  • Well I should add; My actual function is alot more lines that just this one. I'll update the OP. I'll look into the link you posted in the meantime! –  Sep 17 '13 at 18:06
  • 1
    @SiemHermans Just saw your edit, this should still work for you. You aren't doing anything fancy with your function, so it is essentially a container for that content -- just like the ScriptBlock. If you're planning on using that output in your script, you should probably store it in a variable. I'll edit my post with an example. – Anthony Neace Sep 17 '13 at 18:10
  • I have re-written the gwmi statements to accept variables for a $computername and $credentials, thus circumventing the original Invoke-Command problem. However the second problem stands. I have enabled the local Admin account, executed the steps you gave in your link, and added Everyone to the Remote Management User group. The error still stands. I must be missing something... –  Sep 17 '13 at 18:34