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
unable to get desired output
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.
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.
Re: unable to get desired output
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
- }
- }
Re: unable to get desired output
thank you so much! it worked. could you share me what's the mistake in my script?