unable to get desired output

Ask your 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.
This topic is 8 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
shr564
Posts: 6
Last visit: Mon Oct 03, 2016 11:02 pm

unable to get desired output

Post 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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: unable to get desired output

Post by jvierra »

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. $str1 = read-host "enter month and date"
  3. $pattern="backup completed.*$str1"
  4. foreach ($server in $servers){
  5.     if(get-content -path $servers | select-string $pattern){
  6.         write-host "completed on $server" -fore green
  7.     }else{
  8.         write-host "incomplete on $server" -fore red
  9.     }
  10. }
User avatar
shr564
Posts: 6
Last visit: Mon Oct 03, 2016 11:02 pm

Re: unable to get desired output

Post by shr564 »

thank you so much! it worked. could you share me what's the mistake in my script?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: unable to get desired output

Post by jvierra »

Pattern match was not really functional.

help select-string -full
This topic is 8 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked