Support Forums

unable to get desired output

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.
User avatar
shr564
Posts: 4
Joined: Sat Nov 14, 2015 2:11 am

unable to get desired output

Postby shr564 » Fri Jan 29, 2016 1:11 am

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
User avatar
jvierra
Posts: 9698
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: unable to get desired output

Postby jvierra » Fri Jan 29, 2016 1:24 am

No idea what is in your log or servers file but here is how you will likely have to do it:

$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
}
}
User avatar
shr564
Posts: 4
Joined: Sat Nov 14, 2015 2:11 am

Re: unable to get desired output

Postby shr564 » Fri Jan 29, 2016 9:59 pm

thank you so much! it worked. could you share me what's the mistake in my script?
User avatar
jvierra
Posts: 9698
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: unable to get desired output

Postby jvierra » Fri Jan 29, 2016 10:12 pm

Pattern match was not really functional.

help select-string -full

Return to “Windows PowerShell”

Who is online

Users browsing this forum: No registered users and 3 guests