I wrote simple function in a script to enable logging in my powershell script. But I am getting error while calling function. Below is my script. If I call function immediately after function, it is working fine. Pls help me out.
Function recordlog
{
Param($status,$info,$logpath)
$Data = get-date -format g
$Data = "$Data`t$Env:USerName`t$status`t$info"
write-host $Date
Add-Content -Path $logpath -Value $Data
}
foreach ($comp in $computername) {
recordlog "Information" "Initializing PSRemote Session to $comp" $logfile
$s= new-pssession -computerName $comp
recordlog "Information" "Successfully established PSRemote Session to $comp" $logfile
$filepathlog = $filepath+$comp+".txt"
invoke-command -session $s -argumentlist $Eventlogname,$filepathlog -scriptblock {param($Eventlogname,$filepathlog)
$TimeAgo = (Get-Date).addhours(-24)
$Properties='Timegenerated','EntryType','EventID','timewritten','machinename'
$ErrorActionPreference = "Inquire"
recordlog "Information" "Querying $Eventlogname for error or warnings that got logged in last 24 hours on $comp" $logfile
Get-EventLog -logname $Eventlogname -EntryType Error,Warning -After $timeago | format-table -property $properties | out-file -filepath $filepathlog |sort pscomputername | ft -groupby pscomputername}
remove-pssession -session $s
}