ForEach loop does not working. Error: Cannot convert value to type System.String.


hi,

i have written function event logs multiple machine below. getting error 'cannot convert value type system.string.' when passing text file of computers using $comp = get-content -path 'file path'.

$comp = import-csv -path 'csv path'

get-eventlogreport -computer $comp -before '10/10/2016' -after 10/09/2016 -logname 'system' -csvpath c:\3.csv -newest 5

scripts:   

function get-eventlogreport
{
[cmdletbinding()]
param
(

        [parameter(mandatory=$true)][string]$computers,
        [parameter(mandatory=$true)][string]$before,
        [parameter(mandatory=$true)][string]$after,
        [parameter(mandatory=$true)][string]$csvpath,
        [parameter(mandatory=$true)][string]$logname,
        [parameter(mandatory=$false)][int]$newest
        
)
foreach ($computer in $computers)
{
write-verbose 'processing event logs on each server' -verbose
if (!($newest))
{
get-eventlog -computername $computers -logname $logname -entrytype warning,error | select-object -property * | export-csv -path $csvpath -notypeinformation
}
else
{
get-eventlog -computername $computers -logname $logname -newest $newest -entrytype warning,error | select-object -property * | export-csv -path $csvpath -notypeinformation
}
}
write-verbose 'event log processed , exported csv file' -verbose
}


i sure issue foreach unable fix since morning.

script working fine when using -computername $env:computername

thanks in advance

roushan

since $computers array, use [string[]].

richard mueller - mvp enterprise mobility (identity , access)



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Azure MFA with Azure AD and RDS

Failed to setup initiator portal. Error status is given in the dump data.

Invalid pointer on gpresult /h gpreport.html