Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Get-ADComputer CanonicalName

$
0
0

The first code snippet works.  I have 2 machines in machines.txt.  One of them is on the 156 subnet and the other is on the 141 subnet.  

 

 

 

$pc = Get-Content "c:\temp\tmp\machines.txt"

 

foreach ($machine in $pc) {$getip = (Test-Connection $machine -Count 1).IPV4Address.IPAddressToString;  

 

if ($getip -LIKE '*156*') {Write-Host "156 subnet"};

 

if ($getip -LIKE '*141*') {Write-Host "141 subnet"}

 

}

 

The problem I'm trying to solve is in our environment machines get moved to different OU's far too often. (don't ask!!)   So I want to write a script that instead of reading a .TXT file will read Active Directory OU's and then move machines back where they should be.

 

Once I get the IP I then need to test the OU the machine is a member of.   So I wrote this but it does not work quite right.

 

$pc = Get-Content "c:\temp\tmp\machines.txt"

 

foreach ($machine in $pc) {$getip = (Test-Connection $machine -Count 1).IPV4Address.IPAddressToString; 

 

if ($getip -LIKE '*156*') { $location = Get-ADComputer $machine -Properties * | Select CanonicalName };  if ($location -LIKE '*Main Campus*') {Write-Host "this is in the correct OU"} ELSE {Write-Host "blah blah blah"}

 

}

It writes "this is in the correct OU" Twice when it should have only written it once.  

So my code is in error someplace....


Viewing all articles
Browse latest Browse all 6937

Trending Articles