Author Posts

April 24, 2014 at 1:08 am

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
____________________________

April 24, 2014 at 3:59 am

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'
}

April 24, 2014 at 4:08 am

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

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
____________________________

April 24, 2014 at 4:27 am

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.

April 24, 2014 at 4:48 am

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

April 24, 2014 at 5:41 am

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.

April 24, 2014 at 5:44 am

Hello

It threw up an error when i didnt have them

April 24, 2014 at 5:45 am

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

April 24, 2014 at 5:56 am

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: (:) [], ParseException + FullyQualifiedErrorId : ExpectedValueExpression

April 24, 2014 at 5:59 am

Ahh i see what you mean – you had ' ....in my email this came up as '

April 24, 2014 at 6:00 am

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.

April 24, 2014 at 6:02 am

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.

April 24, 2014 at 6:03 am

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'')) {$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
____________________________

April 24, 2014 at 6:05 am

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.

April 24, 2014 at 6:07 am

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'
}

April 24, 2014 at 6:13 am

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

Can i give you points ?

April 24, 2014 at 6:16 am

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

April 28, 2014 at 11:24 am

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

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'
}