Page 1 of 1

unable to get desired output

Posted: Fri Jan 29, 2016 1:11 am
by shr564
I am looking for 2 strings in log files of multiple servers. I am able to get the result through a command-let but not via script. The script I created does not give me the result for individual servers with the server name instead it gives a positive output even when the string is not present on one server. I'm new to scripting. I don't know what I am doing wrong here. please help.

$servers = get-content d:\path.txt
$str1 = read-host "enter month and date"
foreach ($server in $servers)
{
$presence = get-content -path $servers | select-string "backup completed" | select-string $str1

if($presence) {write-host "completed on $server" }
else {write-host "incomplete on $server"}
}

desired output:
completed on abc
completed on def
incomplet on dekj

Re: unable to get desired output

Posted: Fri Jan 29, 2016 1:24 am
by jvierra
No idea what is in your log or servers file but here is how you will likely have to do it:

Code: Select all

$servers = get-content d:\path.txt
$str1 = read-host "enter month and date"
$pattern="backup completed.*$str1"
foreach ($server in $servers){
if(get-content -path $servers | select-string $pattern){
write-host "completed on $server" -fore green
}else{
write-host "incomplete on $server" -fore red
}
}

Re: unable to get desired output

Posted: Fri Jan 29, 2016 9:59 pm
by shr564
thank you so much! it worked. could you share me what's the mistake in my script?

Re: unable to get desired output

Posted: Fri Jan 29, 2016 10:12 pm
by jvierra
Pattern match was not really functional.

help select-string -full