Using the Select-String Cmdlet, you can determine whether or not a specific string value exists in a text file. PowerShell will return each line in the text file that includes the target string. You can add the -quiet parameter to get back a True if the string is found and nothing if the string is not found. Another Select-String parameter that you might find useful is -casesensitive, which performs a case-sensitive search.

Following is a crude script I wrote to find the existence of three strings in Edgetransport.exe.config file on multiple Hub transport servers. It basically searches for the string in the file, compares it with a standard value and returns a ‘Match’ or ‘No match’ according to what it found.

$CheckServers = Get-Content Servers.txt
foreach( $CSrv in $CheckServers )
    {
    Write-host $Csrv -fore cyan
    $config = get-content “\\$CSrv\D$\Program Files\Microsoft\Exchange Server\Bin\Edgetransport.exe.config”
    $Ver1 = $config | Select-String “EnableResourceMonitoring”
    $Ver2 = ‘    <add key=”EnableResourceMonitoring” value=”True” />’
    if ($ver1 -like $ver2)
        {write-host Match -fore green}
    Else
        {write-host No Match -fore red}
    $Wer1 = $config | Select-String “DatabaseCheckPointDepthMax”
    $Wer2 = ‘    <add key=”DatabaseCheckPointDepthMax” value=”536870912″ />’
    if ($Wer1 -like $Wer2)
        {write-host Match -fore green}
    Else
        {write-host No Match -fore red}
    $Xer1 = $config | Select-String “DatabaseMaxCacheSize”
    $Xer2 = ‘    <add key=”DatabaseMaxCacheSize” value=”1073741824″ />’
    if ($Xer1 -like $Xer2)
        {write-host Match -fore green}
    Else
        {write-host No Match -fore red}
    Write-host
    write-host “Press any key to check next server”
    write-host “”
    $x = $host.UI.RawUI.ReadKey(“NoEcho,IncludeKeyDown,AllowCtrlC”)
    }

To run the script, create a file called Servers.txt, populate it with server names, save the code given above to a .ps1 file and run the .ps1 file.

– Thanks, Jinesh.

Advertisements