Powershell Script - Why no Output?

I have downloaded the below Powershell script from Microsoft - BUT, running it results in no output, not even the error that is included within the script.

 
function LoggedOnUser {
#Requires -Version 2.0            
[CmdletBinding()]            
 Param             
   (                       
    [Parameter(Mandatory=$true,
               Position=0,                          
               ValueFromPipeline=$true,            
               ValueFromPipelineByPropertyName=$true)]            
    [String[]]$ComputerName
   )#End Param

Begin            
{            
 Write-Host "`n Checking Users . . . "
 $i = 0            
}#Begin          
Process            
{
    $ComputerName | Foreach-object {
    $Computer = $_
    try
        {
            $processinfo = @(Get-WmiObject -class win32_process -ComputerName $Computer -EA "Stop")
                if ($processinfo)
                {    
                    $processinfo | Foreach-Object {$_.GetOwner().User} | 
                    Where-Object {$_ -ne "NETWORK SERVICE" -and $_ -ne "LOCAL SERVICE" -and $_ -ne "SYSTEM"} |
                    Sort-Object -Unique |
                    ForEach-Object { New-Object psobject -Property @{Computer=$Computer;LoggedOn=$_} } | 
                    Select-Object Computer,LoggedOn
                }#If
        }
    catch
        {
            "Cannot find any processes running on $computer" | Out-Host
        }
     }#Forech-object(ComputerName)       
            
}#Process
End
{

}#End

}#LoggedOnUser

Open in new window


I'm running it like this:

 .\Get-LoggedOnUsername.ps1 -ComputerName martin-pc

Many Thanks
Mark
LVL 13
Mark DamenERP System ManagerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
Running that only creates the function.  You then have to run the function.  Best to run the script as (notice the space after the first dot).
. .\Get-LoggedOnUsername.ps1

Open in new window


Then run the function.
LoggedOnUser -ComputerName martin-pc

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mark DamenERP System ManagerAuthor Commented:
Excellent, many thanks for the prompt reply that worked.

2 more quick questions if I may, I will accept your first reply as the answer and award points.

How would I pipe a computer list into this function?
How would I supply a text file computer list into this function?

Many Thanks
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
To understand that, look into the PARAM section:
 Param             
   (                       
    [Parameter(Mandatory=$true,
               Position=0,                          
               ValueFromPipeline=$true,            
               ValueFromPipelineByPropertyName=$true)]            
    [String[]]$ComputerName
   )#End Param

Open in new window

That says that you can use the pipeline to provide the parameter. It is a string (array), so it understands both
"server1", "server2"  | LoggedOnUser
get-content c:\temp\serverlist.txt | LoggedOnUser

Open in new window

0
Mark DamenERP System ManagerAuthor Commented:
Amazingly quick response, answer provided worked first time!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.