Link to home
Start Free TrialLog in
Avatar of Mirceyhun Musayev
Mirceyhun Musayev

asked on

detect non-domain computers in my network

I want to detect non-domain computers in my network. Found scripts for this purposes. Which one - detects online workstations via Ip range and next one detects are they joined to domain or not. But i cant join them for properly working ) can anyone help?
$firstThree = "10.20.51"

1..254 | ForEach-Object {

    $address = "$($firstThree).$_"

    If (Test-Connection -ComputerName $address -Count 1 -Quiet) {

        Write-Host "$address is up" -ForegroundColor Green

    } Else {

        Write-Host "$address is down" -ForegroundColor Red

    }

}

Open in new window






if (Test-Connection -ComputerName $computer -Count 1 -Quiet)
{
  if ((gwmi win32_computersystem).partofdomain)
  {
    write-host -fore green "I am domain joined!"
  }
  else
  {
    write-host -fore red "Ooops, workgroup!"
  }
}

Open in new window

Avatar of Fethi ABASSI
Fethi ABASSI
Flag of Tunisia image

you can join them by adding below line:
Add-Computer -ComputerName $computers -Domain "YourDomainName" -Restart
Avatar of Andy Bartkiewicz
Andy Bartkiewicz

I would use radius to protect your network and only let domain computers connect
Scanning the network using ping  resp. Test-Connection requires that the default setting of the Windows Firewall is changed, as by default pings are not answered (at least for client OS). That's the first flaw.

The second flaw is that your WMI call checks the local machine, not remote. You need to include the computername parameter.

You should not only check if the machine is part of a domain, but of your domain.

Assuming ping works, your script would have to look like
$firstThree = '10.20.51'
$domain = 'mydomain.com'

1..254 | ForEach-Object {
    $address = $firstThree + '.' + $_
    If (Test-Connection -ComputerName $address -Count 1 -Quiet) {
        Write-Host  -f green "$address is up"  -NoNewLine
        $sysInfo = Get-WMIObject Win32_ComputerSystem -ComputerName $address
        if ( $sysinfo.partOfDomain -and $sysinfo.Domain -eq $domain )
        {
            Write-Host -f green ' and domain member'
        } else {
            Write-Host -f yellow ' but not domain-joined, trying to add now'
            Add-Computer -ComputerName $address -Domain $domain -Restart
        }
    } Else {
        Write-Host -f red "$address is not reachable" 
    }
}

Open in new window

Carefully consider whether you want to use -Restart when adding the machine to the domain - this will force a reboot, which might be too intrusive.
You will probably need to add the following parameters to Add-Computer
-LocalCredential -UnjoinDomainCredential -Credential
If Add-Computer is anything like NETDOM, you won't be able to join another domain directly
Suggest change line 8 to add the -ErrorAction SilentlyContinue

 $sysInfo = Get-WMIObject Win32_ComputerSystem -ComputerName $address -ErrorAction SilentlyContinue

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial