Powershell Help!

Jay_2

2[H]4U
Joined
Mar 20, 2006
Messages
3,583
Hi,

I have the following code that I have put together

Code:
$ou = [ADSI]"LDAP://OU=test,OU=domain,DC=local,DC=co,DC=uk"
$files = ("C:\Scripts\$(get-date -f yyyy-MM-dd-hh-mm-ss)-loggedin.csv")

<# edit the above variables to suit your environment #>

$vers = (get-host | select-object Version | Format-Wide | Out-String).trim()
cls
echo " "
echo "This script must be run as a domain admin in Powershell 3.0 or above"
echo " "
write-host  "You are currently running Powershell version " -nonewline
write-host "$vers" -NoNewline
write-host " as user " -NoNewLine
Write-Host "$env:UserName"
echo " "
echo "It will output details of who is logged into all systems in the following OU" $ou
echo " " 
echo "And write the results to $files"
echo " "
Write-Host "Press any key to run the script ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
cls
foreach ($child in $ou.psbase.Children) { 
    if ($child.ObjectCategory -like '*computer*') { $strComputers += $child.Name } }

foreach ($strComputer in $strComputers) {
gwmi win32_computersystem -comp $strComputer | select Username,Caption,Manufacturer,Model | Export-Csv -append -path ($files)}
Echo "Export Completed"

This loops through the computer in the OU and checks for a few details about that PC. The issues I have is that I can't get it to recursively check sub / child OUs. Any ideas?

Thanks
 
It looks as if you may need to set the searchscope to tell it to look through the child OUs.

For instance...when I am looking at my OU

-SearchBase 'OU=XXXX,DC=VVVV,DC=VVVVV,DC=VVVV,DC=VVVl'

and need to see all the sites that belong to me, I use

-SearchScope subtree


ss64.com explains searchscope as:

-SearchScope
The scope of an AD search.
Possible values for this parameter are:
Base or 0 Search only the current path or object.
OneLevel or 1 Search the immediate children
Subtree or 2 Search the current path/object and all children

Subtree (or 2) tells the command to recursively search all children
 
I was bored so I cleaned up your code a bit and turned it into a function.

Ran out of time before I could mess with recursive. But personally, get the Quest ADCmdlets. It will make things a ton easier as it already has a recurse switch

get-qadcomputer -searchroot "OU=Computers,DC=something,DC=com" -recurse | ft name, lastlogontimestamp

Code:
function Company-GetUsersLoggedIn {
 
<#
.SYNOPSIS
.Description
.PARAMETER OU
.PARAMETER Recurse
.EXAMPLE
#>
 
    [CmdletBinding()]
    param (
    [Parameter(Mandatory=$false,HelpMessage="Sets the OU for the search")][string[]]$OU="OU=base,DC=company,DC=com",
    [Parameter(Mandatory=$false,HelpMessage="Turns on Recursive searching")][switch]$Recurse
    )
    $files = ("C:\Temp\$(get-date -f yyyy-MM-dd-hh-mm-ss)-loggedin.csv")
    $vers = (get-host | select-object Version | Format-Wide | Out-String).trim()
    $fou = [ADSI]"LDAP://$ou" -
    cls
   
    if ($vers -lt "3.*") {  # Simple Error Checking
            Write-Warning "This script must be run in Powershell 3.0 or above, Please upgrade and try again"
            write-Warning  "You are currently running Powershell version $vers as user $env:UserName"
            return
    }
 
    write-Host "It will output details of who is logged into all systems in the following OU $fou.name"
    write-Host "And write the results to $files"
    #Write-Host "Press any key to run the script ..."
    #$x = $host.UI.RawUI.ReadKey("Nowrite-verbose,IncludeKeyDown")
 
    foreach ($child in $fou.psbase.Children) {
        if ($child.ObjectCategory -like '*computer*') {
             $strComputers += $child.Name
             Write-verbose "Adding $child.name"
             }  
         }
 
    foreach ($strComputer in $strComputers) {
    Write-Verbose "Adding $StrComputer to file..."
    gwmi win32_computersystem -comp $strComputer -ErrorAction SilentlyContinue | select Username,Caption,Manufacturer,Model | Export-Csv -append -noclobber -path ($files)
    }
    write-Host "Export Completed"
 
}
 
Company-GetUsersLoggedIn
 
Back
Top