Hi All
so i have around 55 offices globally, i now need to test which office a computer belongs to (which is contained in DN / $ouResult ) and write to a file that will contain the office devices.
Now whats the best way to say if it does not match office one check office2,3,4,5…
i.e.
if it does not match
if ($ouResult -like “OU=Newcastle”)
move onto
if ($ouResult -like “*OU=Office2”)
until it finds a match
<pre>$ComputerName = $env:COMPUTERNAME
$searcher = New-Object System.DirectoryServices.DirectorySearcher($root)
$searcher.Filter = "(&(objectClass=computer)(name=$ComputerName))"
[System.DirectoryServices.SearchResult]$result = $searcher.FindOne()
if (!$?)
{
return
}
$dn = $result.Properties["distinguishedName"]
$ouResult = $dn.Substring($ComputerName.Length + 4)
if ($ValueOnly)
{
$ouResult
}
$Serial = gwmi win32_bios |select Serialnumber -ExpandProperty Serialnumber
if ($ouResult -like “OU=Newcastle”)
{
$path = “c:\Newcastle\dds.csv”
try{ Test-Path -EA Stop $path; $Serial | Export-Csv "c:\Newcastle\dds.csv" -NoTypeInformation -Append } catch { $Serial | Export-Csv "c:\Newcastle\dds.csv" -NoTypeInformation }
}
else
{
check list of other offices until match found then write to file belonging to office#
$path = “c:\Toronto\dds.csv”
try{ Test-Path -EA Stop $path; $Serial | Export-Csv "c:\toronto\dds.csv" -NoTypeInformation -Append } catch { $Serial | Export-Csv "c:\toronto\dds.csv" -NoTypeInformation }
}
im assuming theres a better way than a ton of if statements
Is the file path always the same name as the root OU? If so, I would just parse that out and substitute.
But to address your specific questions, create a csv file with your patterns and destinations.
pattern,destination
"*OU=Newcastle*","c:\Newcastle\dds.csv"
"*OU=Office2","c:\Offic2\dds.csv"
...
Load that file at the top of your script.
$tbl=import-csv pathtocsv
Loop through the patterns looking for a match.
foreach ($row in $tbl) {
if ($ouResult -like $row.pattern) {
... Export-Csv $row.destination ...
break
}
}
If you are lucky, as Ron eluded, if they use the same OU path you can do a parse like this:
$dns= "OU=Computers,OU=Newcastle,OU=Locations,DC=mycompany,DC=com","OU=Computers,OU=Toronto,OU=Locations,DC=mycompany,DC=com"
foreach ($dn in $dns) {
$parsed = ($dn.Split(",")[1]).Replace("OU=","")
$parsed
}
Generally switch is used to eliminate redundant code.
switch($ou){
'OU=Computers,OU=Newcastle,OU=Locations,DC=mycompany,DC=com'{'newcastle'}
'OU=Computers,OU=Toronto,OU=Locations,DC=mycompany,DC=com'{'toronto'}
}
#you can even make it a function, place it at the top of your script and use it later in your code
function get-office{
param($ou)
switch($ou){
'OU=Computers,OU=Newcastle,OU=Locations,DC=mycompany,DC=com'{'newcastle'}
'OU=Computers,OU=Toronto,OU=Locations,DC=mycompany,DC=com'{'toronto'}
}
}
get-office $ouresult
Thanks for the suggestions, i went with Rons as the aim of this is to turn into a single button GUI & having the locations in CSV stored on network will allow changes without re-issuing the program.
Here is my Final Script (well without any error handling)
$tbl=import-csv "H:\EAA\DDS\EOL Program\offices.csv" -Delimiter ","
$ComputerName = $env:COMPUTERNAME
$searcher = New-Object System.DirectoryServices.DirectorySearcher($root)
$searcher.Filter = "(&(objectClass=computer)(name=$ComputerName))"
[System.DirectoryServices.SearchResult]$result = $searcher.FindOne()
if (!$?)
{
return
}
$dn = $result.Properties["distinguishedName"]
$ouResult = $dn.Substring($ComputerName.Length + 4)
if ($ValueOnly)
{
$ouResult
}
$Serial = gwmi win32_bios |select Serialnumber -ExpandProperty Serialnumber
foreach ($row in $tbl) {
if ($ouResult -like $row.pattern) {
$serial | Export-Csv $row.destination -NoTypeInformation -append -
break
}
}
CSV content looks like
Pattern, Destination
OU=#London Campus, \global\europe\Transfer\Newcastle\Test\London Campus.csv
OU=Adelaide, \global\europe\Transfer\Newcastle\Test\Adelaide.csv
OU=Amsterdam, \global\europe\Transfer\Newcastle\Test\Amsterdam.csv
OU=Auckland, \global\europe\Transfer\Newcastle\Test\Auckland.csv
OU=Beijing, \global\europe\Transfer\Newcastle\Test\Beijing.csv
OU=Belfast, \global\europe\Transfer\Newcastle\Test\Belfast.csv
OU=Belgrade, \global\europe\Transfer\Newcastle\Test\Belgrade.csv
OU=Berlin, \global\europe\Transfer\Newcastle\Test\Berlin.csv
OU=Bogota, \global\europe\Transfer\Newcastle\Test\Bogota.csv