PowerShell Function Primer
Richard Siddaway at IT Knowledge Exchange answers a very basic question that many new PowerShell scripters run into - Once I create a function, how do I use it.
Most of my scripts end up being functions because I will eventually combine them into modules
My recent script
function get-logedonuser { param ( [string]$computername = $env:COMPUTERNAME ) Get-WmiObject -Class Win32_LogonSession -ComputerName $computername | foreach { $data = $_ $id = $data.__RELPATH -replace """", "'" $q = "ASSOCIATORS OF {$id} WHERE ResultClass = Win32_Account" Get-WmiObject -ComputerName $computername -Query $q | select @{N="User";E={$($_.Caption)}}, @{N="LogonTime";E={$data.ConvertToDateTime($data.StartTime)}} } }Prompted a comment about how could you run this script.
First off I would save it to a file – get-logedonuser.ps1
You can then either
(read more)
If you are running PowerWF, it is very easy to leverage a function you find on the internet. Simply paste the function into a script block in the workflow and then either call it from that script block or from elsewhere in your workflow.
4 weeks ago - link

