Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7068215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:17:48+00:00 2026-05-28T05:17:48+00:00

PowerShell noob question here. I have groups of servers that are listed in separate

  • 0

PowerShell noob question here. I have groups of servers that are listed in separate lists. Each server has an error log that I scan for certain errors. However, each group of servers has its own unique set of errors that I scan using a specific search string for that server group. What I’m basically doing is creating a condition based on the server list name and mapping the appropriate search string for that server group.

Something like this:

$SERVER_LST_1 = "$pwd\servers\servers_1.lst"
$SERVER_LST_2 = "$pwd\servers\servers_2.lst"
$SERVER_LST_3 = "$pwd\servers\servers_3.lst"

$SEARCH_STR_1 = "Error text for server group 1"
$SEARCH_STR_2 = "Error text for server group 2"
$SEARCH_STR_3 = "Error text for server group 3"

$Servers1 = Get-Content $SERVER_LST_1
ForEach ($Server1 in $Servers1)
{
  $StartupErrorLog1 = Get-ChildItem -Path \\$Server1\$LOG_PATH -Include StartupError.log -Recurse | Select-String -notmatch $SEARCH_STR_1
}
$Servers2 = Get-Content $SERVER_LST_2
ForEach ($Server2 in $Servers2)
{
  $StartupErrorLog2 = Get-ChildItem -Path \\$Server2\$LOG_PATH -Include StartupError.log -Recurse | Select-String -notmatch $SEARCH_STR_2
}
$Servers3 = Get-Content $SERVER_LST_3
ForEach ($Server3 in $Servers3)
{
  $StartupErrorLog3 = Get-ChildItem -Path \\$Server3\$LOG_PATH -Include StartupError.log -Recurse | Select-String -notmatch $SEARCH_STR_3
}

I would like to make the code more efficient by not using so many conditions. Is there a cleaner approach to map the search strings to their appropriate server group by using less code? Hope that makes sense.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:17:48+00:00Added an answer on May 28, 2026 at 5:17 am

    You can refactor it into something like below:

    $groups = @{
    "$pwd\servers\servers_1.lst"="Error text for server group 1";
    "$pwd\servers\servers_2.lst"="Error text for server group 2";
    "$pwd\servers\servers_3.lst"="Error text for server group 3";
    }
    $startupErrors = @{}
    $groups.keys | %{
        $key = $_
        gc $key | %{
            $startupErrors[$_] = Get-ChildItem -Path \\$_\$LOG_PATH -Include StartupError.log -Recurse | Select-String -notmatch $groups["$key"]
        }
    }
    

    Basically, using an HashTable to associate the search text and the server group. Also, I have given only the refactoring solution, but the Get-ChildItem and Select-String may not be doing what you want to.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basic powershell question here - I have two computers in the c:\temp\servers.txt file and
First, yes I am a noob to scripting and powershell as that will become
A powershell question: I want to extract each line in a character stream produced
I have a simple powershell script that gets ran daily to compress and move
I have a powershell script that runs a console .exe application. The console application
My powershell profile has a custom powershell prompt that unfortunately causes $lastexitcode values to
In PowerShell I have this function: function RunSQLServerQuery { param ( [string]$server = .,
I have a powershell script ( setup.ps1 ), that we use as the entry
I have powershell version 2.0.0.1082 on my server. I want to import data from
I'm using PowersHell to automate iTunes but find the error handling / waiting for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.