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