Script To Check Particular Strings From A List of Servers RRS feed

  • Question

  • I am new to PS. The script that I am trying to do is the following:

    1. Check servers listed on a text file

    2. Check if each servers on the list have particular strings from a text file

    3. Properly Display the Output like this:

    Server  | String1 | String2

    Server1| Found   | Not Found

    $Computers = get-content "C:\Users\user\desktop\servers.txt"
    Get-Content C:\Users\user\desktop\servers.txt | ForEach-Object { 
    if( Get-Content -Path "C:\Program Files\Program\client.txt" | Where-Object {$_ -like '*www.technet.com*'} )
    Write-Host "Found"
    if( Get-Content -Path "C:\Program Files\Program\client.txt" | Where-Object {$_ -like '*Company*'} )
    Write-Host "Found"
    Write-Host "Not Found"

    • Edited by ウィルフレッド Wednesday, March 20, 2019 1:54 AM
    • Moved by Bill_Stewart Wednesday, September 4, 2019 7:08 PM This is not "fix/debug/rewrite script for me" forum
    Wednesday, March 20, 2019 1:52 AM

All replies

  • Your first line of code is superfluous. You create a variable but you never use it.

    If you want to query a remote computer you will have to reach it in any way. You don't do this in your code.

    Usually it's a good idea to check if a remote computer is available at all before querying it. This could be a start for you:

    Get-Content -Path C:\Users\user\desktop\servers.txt | 
        ForEach-Object {
            $Query = [PSCustomObject]@{
                Server    = $_ 
            if (Test-Connection -ComputerName $_) {
                $RemoteTextFile = "\\$_\C$\Program Files\Program\client.txt"
                $Query.Connected = $true
                $Query.String1   = Select-String -Pattern 'www.technet.com' -SimpleMatch -Path $RemoteTextFile -Quiet
                $Query.String2   = Select-String -Pattern 'Company'         -SimpleMatch -Path $RemoteTextFile -Quiet
            else {
                $Query.Connected = $false
                $Query.String1   = ''
                $Query.String2   = ''

    Your input text file must contain one remote computer at a line and must not contain any empty row.

    First you check if the remote computer is availabe, then you query the text file for the desired content, then you output the created object.

    Of course as always there is a lot of room for improvement. ;-)

    Live long and prosper!


    • Edited by BOfH-666 Wednesday, March 20, 2019 2:48 AM
    Wednesday, March 20, 2019 2:48 AM