Filtering Powershell output - powershell

I am trying to filter the output of the following PS script;
We use server names like:
SRV-APP-001,
PRD-APP-001,
TST-APP-001
etc...
$strCategory = "computer"
$strOperatingSystem = "Windows*Server*"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.Filter = ("OperatingSystem=$strOperatingSystem")
$colProplist = "name"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}
$colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults)
{
$objComputer = $objResult.Properties;
$objComputer.name
}
The output of this script are all the servers in the Domain.
But I want only see the servers that start with "SRV" or "PRD"
the | where { $_name -like "SRV*"} is not realy working after the $objComputer.name part.
Thank you in advance

Change the filter to:
"(|(name=SRV*)(name=PRD*))(OperatingSystem=Windows*Server*)"

Related

Returning all Users from AD instead of users from groups in powershell

I am trying to ammend the following code to return all users from AD instead of Domain Admins:
Original Code:
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.SearchScope = "Subtree"
$strFilter = "(&(sAMAccountName=Domain Admins))"
$objSearcher.Filter = $strFilter
$DomainAdminDN = ($objSearcher.FindOne()).Properties.distinguishedname
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 10000000
$objSearcher.SearchScope = "Subtree"
$strFilter = "(&(objectCategory=user)(memberOf=$DomainAdminDN))"
$objSearcher.Filter = $strFilter
return ($objSearcher.FindAll())
}
I have made the following changes but it seems to return every object in AD instead of just users in AD
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.SearchScope = "Subtree"
$strFilter = "(&objectCategory=user(sAMAccountName=Users))"
$objSearcher.Filter = $strFilter
$DomainAdminDN = ($objSearcher.FindOne()).Properties.distinguishedname
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 10000000
$objSearcher.SearchScope = "Subtree"
$strFilter = "(&(objectCategory=user)(memberOf=$DomainAdminDN))"
$objSearcher.Filter = $strFilter
return ($objSearcher.FindAll())
}
All Help Welcome!
The ActiveDirectory module is made for this.
$AllUsers = Get-ADUser -Filter * -Server MyDomain -properties *
$AllUsers | Export-CSV -NoTypeInformation 'c:\temp\allusers.csv'
Depending on the size of your domain, this could bog your DC for a minute or two. So if you have thousands of users, do this at an off time.
Instructions on how to load the module are on the Microsoft Docs site
Another alternative to the AD module.
([adsisearcher]"(&(objectClass=User)(objectCategory=Person))").findall().properties
This will list all user objects. The object returned is type System.DirectoryServices.ResultPropertyCollection
Here's a little function to create a PSCustomObject and get rid of the {} around the values.
([adsisearcher]"(&(objectClass=User)(objectCategory=Person))").findall().properties | ForEach-Object {
$ht = [ordered]#{}
foreach($entry in $_.getenumerator()){
$ht.add($entry.name,$($entry.value))
}
[PSCustomObject]$ht
}

Populate Checkedlistbox with Servers

I am trying to populate a list with the servers in my domain, and i have partial success. There are 5 items in my list, which is as many servers as i have.
Unfortunately they are all just called [Collection]
Form is generated with Sapien Powershell Studio
$strCategory = "computer"
$strOperatingSystem = "Windows*Server*"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.Filter = ("OperatingSystem=$strOperatingSystem")
$colProplist = "name"
foreach ($i in $colPropList) { $objSearcher.PropertiesToLoad.Add($i) }
$colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults)
{
$objComputer = $objResult.Properties;
$objComputer.name
$checkedlistbox1.Items.add($objComputer.name)
}
What can I do to have the proper name show up in the checkedlist.
Thanks for any assistance :)
The result object from DirectorySearcher.FindAll() method contains a special property named Properties that returns a typed collection containing the values of properties of the object found in the AD.
This means that you can simply do
. . .
$colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults) {
$checkedlistbox1.Items.add($objResult.Properties['name'][0])
}
I suggest you use Get-ADComputer instead to get the list of your servers.
The you just loop thrue the list and add the servername to your checkedlist
$Servers= Get-ADComputer -Filter {OperatingSystem -Like 'Windows *Server*'} #-Property * #the property flag is not needed if you just want the Name (see comment from Theo)
foreach ($srv in $Servers) {
#Unmark to debug
#$srv.Name
#$srv.OperatingSystem
$checkedlistbox1.Items.add($srv.Name)
}

PowerShell Script to get login details

Hi I have written the following code to get the login user details, but I'm getting only the system name. I also need the username along with it. Please help. Thanks in advance.
function GetList
{ param ([string]$base)
$blah = [ADSI]"$base"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.Filter = "(objectClass=Computer)"
$objSearcher.SearchRoot = $blah
$PropList = "cn","operatingsystem"
foreach ($i in $PropList){$objSearcher.PropertiesToLoad.Add($i)}
$Results = $objSearcher.FindAll()
foreach ($objResult in $Results)
{
$OS = $objResult.Properties.operatingsystem
If ($OS -match "Windows")
{
Echo $objResult.Properties.cn
}
}
}
Maybe $env:USERNAME is thing you are searching for. There is also $env:UserDomain and $env:ComputerName.

Why is Powershell & AD returning newlines?

I have this Powershell code:
$strFilter = "(&(objectCategory=User)(Description=*MD))"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"
$colProplist = "name", "department", "description"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}
$colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults)
{$objItem = $objResult.Properties
$objItem.name + "|" + $objItem.department + "|" + $objItem.description
}
For some reason it's returning new lines. How can I stop this?
The output looks like:
Bob Dole
|
SOME DEPT
|
Bob description
Rick James
|
ANOTHER DEPT
|
Rick description
Changing the last block to:
foreach ($objResult in $colResults)
{$objItem = $objResult.Properties
$colResults.Properties|%{($_.name,$_.department,$_.description) -join "|"}
}
Doesn't give me anything, just a bunch of:
||
||
||
||
||
||
I also tried just:
$colResults.Properties|%{($_.name,$_.department,$_.description) -join "|"}
And only got:
||
You have to be aware that $objItem.attribute is always a collection. I suspect that this is root cause of the problems you are seeing. Try this:
foreach ($objResult in $colResults)
{$objItem = $objResult.Properties
$objItem.name[0] + "|" + $objItem.department[0] + "|" + $objItem.description[0]
}
The problem is that this can give your errors if any of these attributes is empty. To avoid it (and get the same experience) you can use unary -join (with -f to make it easier to read):
foreach ($objResult in $colResults)
{$objItem = $objResult.Properties
'{0}|{1}|{2}' -f #(
(-join $objItem.name)
(-join $objItem.department)
(-join $objItem.description)
)
}
You could just join them instead:
($objItem.Name,$objItem.Department,$objItem.Description) -join "|"
That should give you your desired output.
Alternatively you could use string formatting like this:
"{0}|{1}|{2}" -f $objItem.Name, $objItem.Department, $objItem.Description
Edit: Ok, this runs on my machine and returns the results you wanted:
$strFilter = "(&(objectCategory=User)(Description=*MD))"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"
$colProplist = "name", "department", "description"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}
$colResults = $objSearcher.FindAll()
$colResults.Properties|%{($($_['name']),$($_['department']),$($_['description'])) -join "|"}

Using PowerShell to get all active-domain computers

I'm using this script(below) to get all computers on the domain; but it seems like it stops at 1000 on the dot, when I know there is clearly more. How would I go about getting the complete list?
$strCategory = "computer"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.Filter = ("(objectCategory=$strCategory)")
$colProplist = "name"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}
$colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults)
{$objComputer = $objResult.Properties; $objComputer.name >> allcomps.csv}
try adding
$objSearcher.PageSize = 2000
http://technet.microsoft.com/en-us/library/ff730959.aspx