locked
Help with logic RRS feed

  • Question

  • Hello.

    1.  I am searching through a specific OU for computers with a particular model.  Because I do not want to manually enter each model and export the csv, I'm trying to automate it.  I have a folder on the Server that lists the computer models (used for automating driver identification for specific models of computers.

    I have 2 scripts that I can run - on that gets a (-like model) and the other gets the model and computername from an OU.

    EX:  I have a list of folders that contain the computer models (Dell Optiplex 3040 or Dell Optiplex 3060 etc).

    EX:  OU contains computernames (cs-123-GM etc)

    Script that lists all models from the server folder:  (works as it should strips out all the stuff from the foldername to only give me a model identifier.

    $Cnames = Get-childitem -path "\\network\driverstore\Dell Inc" |?{$_.PSIsContainer} | Select-Object -ExpandProperty Name
    foreach ($char in $CNames) {
    $splitchar=($char -split " ") 
    $s = $Splitchar.count + (-1)
    $mod = ($char -split " ")[$s]
    }
    

    Script to iterate through all Computers in the specified OU (works fine, gives me a model and Computer Name)

    $Sbase = "OU=City Hall,OU=Computers,OU=OU1,DC=Domain,DC=Local"
    #$splitOU = ($Sbase -split ",")[0] 
    #$CompOU = ($splitOU -split "=")[1]
    $CompInfo = Get-ADComputer -filter * -SearchBase $Sbase | Select-object -ExpandProperty Name
    $Info = @()
    $count = 0
    $SearchCrit = "3440" #I want the $mod here
    foreach($c in $CompInfo) {
    If(Test-Connection -ComputerName $C -count 1 -Quiet) {
    $splitOU = ($Sbase -split ",")[0] 
    $CompOU = ($splitOU -split "=")[1]
    $BiosInfo = Get-CimInstance -ComputerName $C -ClassName Win32_ComputerSystem -Filter "Model Like '%$SearchCrit%'" 
    $obj = New-Object -TypeName psobject
    $obj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $BiosInfo.Name
    $obj | Add-Member -MemberType NoteProperty -Name Model -Value $BiosInfo.Model
    # add a method to an object
    if($biosInfo.model) {
    $Info += $obj
    $count= $Count + 1
    }
    #$obj | Add-Member -MemberType ScriptMethod -Name "GetName" -Value {$this.ComputerName +' '+$this.Model} -force
    } 
    }
    $fname = -join ($CompOU,"_",$searchCrit,"Comps.csv")
    $info | export-csv c:\temp\CompModels\$fname -force
    

    So, in the foreach computer, I'd like to incorporate the foreach model and have the $mod be put into the variable $searchcrit.  If a computer has the current model, then add it to the Array (iterate through each computer, comparing the model to the current model in script #1

    Ex:

    current computer: cs-123-Gm - is model Optiplex 3010

    current model: 3010

    CS-123-GM gets added to the array to export to csv.

    Current computer: CS-234-gm is model Optiplex 3020

    Current model (iteration) Optiplex 3010

    CS-234-gm does not get added until the model iteration gets to the optiplex 3020, then gets added to the array to be exported to csv.

    I hope you understand what Im trying to do.

    I just need help putting the 2 together.

    Thank you

    Terry

    Thursday, August 8, 2019 9:37 PM

Answers

  • Terry,

    hhmmm .... I'm quite unsure if I got you right. If it is what I think it is you are overcomplicating this.

    If you just want to match a model number with a certain model name you can use hashtables ... like this:

    # here you have the list of model numbers with the according model names:
    $ModelTable = @{ 
        '20EQS2CC00' = 'ThinkPad P50' 
        '20QNS00Q00' = 'ThinkPad P53'
    }
    
    # here you have the determined the model number from query of a particular computer
    $ModelNumberFromBIOS = '20EQS2CC00'
    
    # and that's how you bring it together
    $ModelTable.$ModelNumberFromBIOS
    Regardless of that - I'd recommend to "explore" the output the Win32_ComputerSystem query a little bit more. In my case it contains a property "SystemFamily" what's actually the model name. ;-)

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Friday, August 9, 2019 12:00 AM

All replies

  • Terry,

    hhmmm .... I'm quite unsure if I got you right. If it is what I think it is you are overcomplicating this.

    If you just want to match a model number with a certain model name you can use hashtables ... like this:

    # here you have the list of model numbers with the according model names:
    $ModelTable = @{ 
        '20EQS2CC00' = 'ThinkPad P50' 
        '20QNS00Q00' = 'ThinkPad P53'
    }
    
    # here you have the determined the model number from query of a particular computer
    $ModelNumberFromBIOS = '20EQS2CC00'
    
    # and that's how you bring it together
    $ModelTable.$ModelNumberFromBIOS
    Regardless of that - I'd recommend to "explore" the output the Win32_ComputerSystem query a little bit more. In my case it contains a property "SystemFamily" what's actually the model name. ;-)

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Friday, August 9, 2019 12:00 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Friday, September 6, 2019 12:46 PM