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:
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.
When running this script as a regular user, the credential box pops up. I fill in Administrator credentials, yet I get the following error:
[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?