Please help with string matching

I am a newbie to code and i have been trying various ways of NotMatch,Match, Like, NotLike etc and cant seem to get this simple task to work.

My task is to look at each server from a list and test if a group is in there , if it isnt it flags it.

Here is the non working code

$Result = @()
$TestResult = @()

foreach($server in (gc c:\Input\ListOfComputers.txt)){

$computer = [ADSI](”WinNT://” + $server + “,computer”)
$Group = $computer.psbase.children.find(”Administrators”)

function getAdmins
{$members = ($Group.psbase.invoke(”Members”) | %{$.GetType().InvokeMember(”Adspath”, ‘GetProperty’, $null, $, $null)}) -replace (‘WinNT://d30.intra/’ + $server + ‘/’), ‘’ -replace (‘WinNT://d30.intra/’, 'd30.intra') -replace (‘WinNT://’, ‘’)
$members
}

$TestResult = ( getAdmins )
Write-Host $TestResult

$Result += Write-Output “SERVER: $server”
$Result += Write-Output ’ ’
$Result += ( getAdmins )
if ($TestResult -NotMatch “*maneng”){$Result += Write-Output “svc_Maneng Missing”}
if ($TestResult -NotMatch “*COG”){$Result += Write-Output “m_DECO_COG_DPEMEA_admins is Missing”}
$Result += Write-Output ‘____________________________’
$Result += Write-Output ’ ’

}
$Result > c:\Output\output2.txt

Invoke-Item c:\Output\output2.txt

and here is the output

SERVER: hwun906

D30/VTIN02/Administrator
D30/Domain Admins
D30/m_DPEMEA_VTI_Administrators
D30/PrihorM
D30/m_DPEMEA_TVR_Administrators
D30/m_DPEMEA_ZGB_Administrators
D30/m_DPEMEA_PRA_Administrators
D30/wisnij01
D30/svc_maneng


SERVER: HWUVN836

D30/VTIN02/Administrator
D30/Domain Admins
D30/m_DPEMEA_VTI_Administrators
D30/PrihorM
D30/m_DPEMEA_TVR_Administrators
D30/m_DPEMEA_ZGB_Administrators
D30/m_DPEMEA_PRA_Administrators
D30/wisnij01
D30/svc_maneng


SERVER: hwuvn879

D30/VTIN02/Administrator
D30/Domain Admins
D30/m_DPEMEA_VTI_Administrators
D30/PrihorM
D30/m_DPEMEA_TVR_Administrators
D30/m_DPEMEA_ZGB_Administrators
D30/m_DPEMEA_PRA_Administrators
D30/wisnij01
D30/svc_maneng


SERVER: vtin02

D30/VTIN02/Administrator
D30/Domain Admins
D30/m_DPEMEA_VTI_Administrators
D30/PrihorM
D30/m_DPEMEA_TVR_Administrators
D30/m_DPEMEA_ZGB_Administrators
D30/m_DPEMEA_PRA_Administrators
D30/wisnij01
D30/svc_maneng


There’s a difference between ($something -notmatch $pattern) and (-not ($something -match $pattern)), when $something is a collection in PowerShell. Using the -notmatch operator means “does this collection contain any elements that don’t match the pattern?”, which is essentially always true in this case.

Also, here you’re probably better off using -like rather than -match, since you’re doing a simple wildcard pattern and not a regular expression. Try this:

if (-not ($TestResult -like '*/svc_Maneng'))
{
    $Result += 'svc_Maneng Missing'
}

if (-not ($TestResult -like '*/m_DECO_COG_DPEMEA_admins'))
{
    $Result += 'm_DECO_COG_DPEMEA_admins is Missing'
}

Hello thanks for your help, i tried the code but i am getting a similar result , it seems to ignore the string :frowning:

SERVER: hwun906

D30/VTIN02/Administrator
D30/Domain Admins
D30/m_DPEMEA_VTI_Administrators
D30/PrihorM
D30/m_DPEMEA_TVR_Administrators
D30/m_DPEMEA_ZGB_Administrators
D30/m_DPEMEA_PRA_Administrators
D30/wisnij01
D30/svc_maneng
svc_Maneng Missing
m_DECO_COG_DPEMEA_admins is Missing


SERVER: HWUVN836

D30/VTIN02/Administrator
D30/Domain Admins
D30/m_DPEMEA_VTI_Administrators
D30/PrihorM
D30/m_DPEMEA_TVR_Administrators
D30/m_DPEMEA_ZGB_Administrators
D30/m_DPEMEA_PRA_Administrators
D30/wisnij01
D30/svc_maneng
svc_Maneng Missing
m_DECO_COG_DPEMEA_admins is Missing


SERVER: hwuvn879

D30/VTIN02/Administrator
D30/Domain Admins
D30/m_DPEMEA_VTI_Administrators
D30/PrihorM
D30/m_DPEMEA_TVR_Administrators
D30/m_DPEMEA_ZGB_Administrators
D30/m_DPEMEA_PRA_Administrators
D30/wisnij01
D30/svc_maneng
svc_Maneng Missing
m_DECO_COG_DPEMEA_admins is Missing


SERVER: vtin02

D30/VTIN02/Administrator
D30/Domain Admins
D30/m_DPEMEA_VTI_Administrators
D30/PrihorM
D30/m_DPEMEA_TVR_Administrators
D30/m_DPEMEA_ZGB_Administrators
D30/m_DPEMEA_PRA_Administrators
D30/wisnij01
D30/svc_maneng
svc_Maneng Missing
m_DECO_COG_DPEMEA_admins is Missing


Hmm… if I’m reading your code correctly, $TestResult should be an array. Try outputting $TestResult.GetType().FullName just before the test. If I’m right, it should come back as either System.String or System.Object. If you get System.String instead, then the code has to change slightly, because you’re working with a single string that contains multiple lines.

Hello,

Here is the latest code - its a mixture of what you said and what was there - this runs through with no errors but the output is the same

but the system type is System.Object()

$Result = @()
$TestResult = @()

foreach($server in (gc c:\Input\ListOfComputers.txt)){

$computer = [ADSI](”WinNT://” + $server + “,computer”)
$Group = $computer.psbase.children.find(”Administrators”)

function getAdmins
{$members = ($Group.psbase.invoke(”Members”) | %{$.GetType().InvokeMember(”Adspath”, ‘GetProperty’, $null, $, $null)}) -replace (‘WinNT://d30.intra/’ + $server + ‘/’), ‘’ -replace (‘WinNT://d30.intra/’, 'd30.intra') -replace (‘WinNT://’, ‘’)
$members
}

$TestResult = ( getAdmins )
Write-Host $TestResult.GetType().FullName

$Result += Write-Output “SERVER: $server”
$Result += Write-Output ’ ’
$Result += ( getAdmins )

#if ($TestResult -notmatch “maneng”){$Result += Write-Output “svc_Maneng Missing”}
#if ($TestResult -notmatch “COG”){$Result += Write-Output “m_DECO_COG_DPEMEA_admins is Missing”}

if (-not ($TestResult -like “'*/svc_Maneng'”)) {$Result += Write-Output “svc_Maneng Missing”}

if (-not ($TestResult -like “'*/m_DECO_COG_DPEMEA_admins'”)){$Result += Write-Output “m_DECO_COG_DPEMEA_admins is Missing”}

$Result += Write-Output ‘____________________________’
$Result += Write-Output ’ ’
}

$Result > c:\Output\output2.txt

Invoke-Item c:\Output\output2.txt

You’ve added an extra set of quotation marks in the “if” statements, which makes the inner (single) quotation marks part of the pattern. Start by copying and pasting the code that I posted.

Hello

It threw up an error when i didnt have them

Please be specific. What was the error message, and what did the code look like when you got that error?

You must provide a value expression on the right-hand side of the ‘-like’ operator
.
At C:\Users\adm_wardl\Documents\show server local admin groups Version 2.ps1:31 ch
ar:28

  • if (-not ($TestResult -like <<<< '*/svc_Maneng')) {$Result += Write-O
    utput “svc_Maneng Missing”}
    • CategoryInfo : ParserError: (:slight_smile: , ParseException
    • FullyQualifiedErrorId : ExpectedValueExpression

Ahh i see what you mean - you had ’ …in my email this came up as &#039

That’s odd… that code looks fine. Well, anyhow, if you want to use double quotes, you need to replace the single quotes instead of just leaving both in.

Ah, yes. The forum software here is a bit wonky with some characters in PowerShell code, and I have a function that HTML-escapes any code that I post so it displays properly. Unfortunately, that may mean you’re seeing the escaped version in emails.

ok - now its writeen like you said. (email was messing up code)

$Result = @()
$TestResult = @()

foreach($server in (gc c:\Input\ListOfComputers.txt)){

$computer = [ADSI](”WinNT://” + $server + “,computer”)
$Group = $computer.psbase.children.find(”Administrators”)

function getAdmins
{$members = ($Group.psbase.invoke(”Members”) | %{$.GetType().InvokeMember(”Adspath”, ‘GetProperty’, $null, $, $null)}) -replace (‘WinNT://d30.intra/’ + $server + ‘/’), ‘’ -replace (‘WinNT://d30.intra/’, 'd30.intra') -replace (‘WinNT://’, ‘’)
$members
}

$TestResult = ( getAdmins )
Write-Host $TestResult.GetType().FullName

$Result += Write-Output “SERVER: $server”
$Result += Write-Output ’ ’
$Result += ( getAdmins )

#if ($TestResult -notmatch “maneng”){$Result += Write-Output “svc_Maneng Missing”}
#if ($TestResult -notmatch “COG”){$Result += Write-Output “m_DECO_COG_DPEMEA_admins is Missing”}

if (-not ($TestResult -like ‘*/svc_Maneng&#039’)) {$Result += Write-Output “svc_Maneng Missing”}

if (-not ($TestResult -like ‘*/m_DECO_COG_DPEMEA_admins’)){$Result += Write-Output “m_DECO_COG_DPEMEA_admins is Missing”}

$Result += Write-Output ‘____________________________’
$Result += Write-Output ’ ’
}

$Result > c:\Output\output2.txt

Invoke-Item c:\Output\output2.txt

OUTPUT

SERVER: hwun906

svc_Maneng Missing
m_DECO_COG_DPEMEA_admins is Missing


SERVER: HWUVN836

svc_Maneng Missing
m_DECO_COG_DPEMEA_admins is Missing


SERVER: hwuvn879

svc_Maneng Missing
m_DECO_COG_DPEMEA_admins is Missing


SERVER: vtin02

D30/VTIN02/Administrator
D30/Domain Admins
D30/m_DPEMEA_VTI_Administrators
D30/PrihorM
D30/m_DPEMEA_TVR_Administrators
D30/m_DPEMEA_ZGB_Administrators
D30/m_DPEMEA_PRA_Administrators
D30/wisnij01
D30/svc_maneng
svc_Maneng Missing
m_DECO_COG_DPEMEA_admins is Missing


Looks like you still have a trailing bit of HTML escape characters in one of the patterns (after Maneng). It’ll be easier if you copy and paste from the forums website.

Since there aren’t any characters in this code that would cause a problem on the forum, here’s an unescaped version:

if (-not ($TestResult -like '*/svc_Maneng'))
{
    $Result += 'svc_Maneng Missing'
}

if (-not ($TestResult -like '*/m_DECO_COG_DPEMEA_admins'))
{
    $Result += 'm_DECO_COG_DPEMEA_admins is Missing'
}

PERFECT - COPYING from the website worked first time - Thanks Dave.

Can i give you points ?

Nope, these are just basic forums. No ratings / marked answers / etc to worry about. :slight_smile: Happy to help!

Hi Dave, I dont think i understand how to use table arrays yet, i have brought a book and its on its way :slight_smile:

In the mean time if you could point me in the right direction i would be greatfull (again)

This code goes to a SQL server and gets some data and displays it in a table  it works great

#Declare the variables (so we can keep it tidy)
$dataSource = “134.239.173.102”
$user = “sa”
$pwd = “master”
$database = “sccm”
$connectionString = “Server=$dataSource;uid=$user; pwd=$pwd;Database=$database;Integrated Security=False;”
$query = "SELECT * FROM sccm "

$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$connection.ConnectionString = “Server=$dataSource;Database=$database;Integrated Security=True;”

$connection.Open()

#Alright. We set up our connection, and opened in.

Time to tell that lousy SQL Server what to do.

$query = “SELECT * FROM sccm”
$command = $connection.CreateCommand()
$command.CommandText = $query
$result = $command.ExecuteReader()

#We’ll get all the people, and worry about how to sort them out later.
#Of course we want this nicely formatted, and so we’ll use a DataTable
#which gives us an in-memory table with the data, and we need to create it first then load it.

$table = new-object “System.Data.DataTable”
$file = New-Object System.IO.StreamWriter “c:\Output\Values.txt”;
$table.Load($result)

#Once we output the information we want to not use the column names, to boring.
#We want our own table, so we will create a variable
#called format that defines how we want our table to look like.

We want the Name column to be named Server Name, and we set a smaller width, and SCCMADGROUP Name to be Identified Sccm Group

$lewis = $.Name
$format = @{Expression={$
.Name};Label=“Server Name”;width=20},@{Expression={$.SCCMADGROUP};Label=“SCCM Group”; width=30},@{Expression={$.IT_Support_Contact};Label=“IT Contact”; width=30}
$format2 = @{Expression={$.Name};Label=“Server Name”;width=20},@{Expression={$.SCCMADGROUP};Label=“SCCM Group”; width=30}
#$table | Where-Object {$.Name -like “*slh” -and $.Born -lt 1990} | format-table $format
#$table | Where-Object {$.SCCMADGROUP -like “*Sele” -and $.Born -lt 1990} | format-table $format | Out-File C:\Users\Iris\Documents\swedes.txt
$table | Where-Object {$.Name -like “*”} | format-table $format
$table | Where-Object {$
.SCCMADGROUP -like “Sele”} | format-table $format | Out-File C:\Output\select.txt
$table | Where-Object {$.SCCMADGROUP -notlike “Sele”-and $.SCCMADGROUP -notlike “NA”} | format-table $format2 | Out-File C:\Output\selected.txt

$connection.Close()
$file.Close()

This code reads server names from a txt file and sees whats admin groups they have, does a quck check to see if they contain two groups and prints a nice little test file at the end it works great 

$Result = @()
$TestResult = @()

foreach($server in (gc c:\Input\ListOfComputers.txt)){

$computer = [ADSI](”WinNT://” + $server + “,computer”)
$Group = $computer.psbase.children.find(”Administrators”)

function getAdmins
{$members = ($Group.psbase.invoke(”Members”) | %{$.GetType().InvokeMember(”Adspath”, ‘GetProperty’, $null, $, $null)}) -replace (‘WinNT://d30.intra/’ + $server + ‘/’), ‘’ -replace (‘WinNT://d30.intra/’, 'd30.intra') -replace (‘WinNT://’, ‘’)
$members
}

$TestResult = ( getAdmins )

Write-Host $TestResult.GetType().FullName

$Result += Write-Output “SERVER: $server”
$Result += Write-Output ’ ’
$Result += ( getAdmins )

if (-not ($TestResult -like ‘*/svc_Maneng’))
{
$Result += ’ svc_Maneng Missing’
}

if (-not ($TestResult -like ‘*/m_DECO_COG_DPEMEA_admins’))
{
$Result += ’ m_DECO_COG_DPEMEA_admins is Missing’
}

And This code is exactly the same as the last one BUT im trying to get rid of the text file as an input and read the server names straight from the sql database instead, so I got the code from PRG1 and PRG2 and combined them but cant get it to read the X part of the for next loop it reads the whole array 

$Result = @()
$TestResult = @()

foreach($server in (gc c:\Input\ListOfComputers.txt)){

$computer = [ADSI](”WinNT://” + $server + “,computer”)
$Group = $computer.psbase.children.find(”Administrators”)

function getAdmins
{$members = ($Group.psbase.invoke(”Members”) | %{$.GetType().InvokeMember(”Adspath”, ‘GetProperty’, $null, $, $null)}) -replace (‘WinNT://d30.intra/’ + $server + ‘/’), ‘’ -replace (‘WinNT://d30.intra/’, 'd30.intra') -replace (‘WinNT://’, ‘’)
$members
}

$TestResult = ( getAdmins )

Write-Host $TestResult.GetType().FullName

$Result += Write-Output “SERVER: $server”
$Result += Write-Output ’ ’
$Result += ( getAdmins )

if (-not ($TestResult -like ‘*/svc_Maneng’)) #Something wrong here
{
$Result += ’ svc_Maneng Missing’
}

if (-not ($TestResult -like ‘*/m_DECO_COG_DPEMEA_admins’))
{
$Result += ’ m_DECO_COG_DPEMEA_admins is Missing’
}