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.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
shr564
Posts: 6
Joined: Sat Nov 14, 2015 2:11 am

unable to get desired output

Post by 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: 12674
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: unable to get desired output

Post by 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:
  1. $servers = get-content d:\path.txt
  2.  
  3. $str1 = read-host "enter month and date"
  4.  
  5. $pattern="backup completed.*$str1"
  6.  
  7. foreach ($server in $servers){
  8.  
  9.     if(get-content -path $servers | select-string $pattern){
  10.  
  11.         write-host "completed on $server" -fore green
  12.  
  13.     }else{
  14.  
  15.         write-host "incomplete on $server" -fore red
  16.  
  17.     }
  18.  
  19. }

User avatar
shr564
Posts: 6
Joined: Sat Nov 14, 2015 2:11 am

Re: unable to get desired output

Post by 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: 12674
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: unable to get desired output

Post by jvierra » Fri Jan 29, 2016 10:12 pm

Pattern match was not really functional.

help select-string -full

Locked