Newbie looking for some help…Get-ADComputer

Welcome Forums General PowerShell Q&A Newbie looking for some help…Get-ADComputer

Viewing 22 reply threads
  • Author
    Posts
    • #266819
      Participant
      Topics: 1
      Replies: 9
      Points: -7
      Rank: Member

      Hi, I have a task to look for computers in all 25 different computer OUs for computers with no description and then email the result per each OU to 25 different people. I am a total newbie in PS and just took a PS class recently.  Below is what I have.  I run the “Get-ADComputer” 25 times and then has a 25 Send email steps below (each to be sent to different people).  This is way overkill.  I’ve looked at the “For each object” command and try creating script for it but don’t know how i can have it loop and run each ou  and then create 25 files to email each person.

      THIS IS THE SCRIPT (I cut off all the repetitive lines) and sub some of generic names into the fields.

      REPEAT 23 MORE TIMES

      REPEAT EMAIL STEPS 23 MORE TIMES WITH DIFFERENT MAILTO

      #THIS IS THE SCRIPT when I try using the ForEach -Object loop.  It runs and then grabs all 25 different OUS and creates one txt #file but I want 25 different files so I can email the 25 different individuals

      This just outputs everything into 1 file.

      • This topic was modified 1 month ago by grokkit. Reason: code formatting
    • #266831
      Participant
      Topics: 5
      Replies: 170
      Points: 656
      Helping Hand
      Rank: Major Contributor

      Move your pipe to Out-File inside the Foreach-Object loop and make the filename a variable so it can change each iteration.

    • #266846
      Participant
      Topics: 1
      Replies: 9
      Points: -7
      Rank: Member

      Sorry, not sure how to make a filename with variable.  I’ve added some more to the script.  When it prompts for site name.  Let’s say I enter USA then when I run the script it runs all 25 OU’s but just gives me one text file “USA_10-29_2020_nodescription.txt”.  I need 25 different files.  One UK_10-29_2020_nodescription.txt”, one Paris_10-29_2020……etc.  The “read-host” is probably useless here.

      $date = Get-Date -format MM_dd_yyyy
      $siteName = Read-Host “Enter the site name”
      $csrName = $siteName + “_” + $date

      $ou | ForEach-Object {
      Get-ADComputer -SearchBase $_ -Filter ‘Description -notlike “*”‘ | Select-Object Name,Description |
      Out-File $(“c:\Test\” + $csrName + “_nodescription.txt”)
      }

    • #266849
      Participant
      Topics: 5
      Replies: 170
      Points: 656
      Helping Hand
      Rank: Major Contributor

      The filename needs to change each iteration.  Simple way is to just number them like this:

       

    • #266855
      Participant
      Topics: 1
      Replies: 9
      Points: -7
      Rank: Member

      I am getting this after I pasted your script but the below line is the same.  Not sure what your “$x++” stands for.

      Get-ADComputer : Error parsing query: ‘Description -notlike “*”’ Error Message: ‘syntax error’ at position: ’22’.

    • #266882
      Participant
      Topics: 5
      Replies: 170
      Points: 656
      Helping Hand
      Rank: Major Contributor

      $x++ increments $x by 1.  It’s the same as $x = $x + 1 or $x += 1.  Recommend reviewing assignment operators: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_assignment_operators?view=powershell-5.1

      I wasn’t even looking at your Get-ADComputer call, but based on the error you reported, I think your description argument is not correct.  I’m not even sure what it is doing since * is a wildcard for anything so it read description is not like anything….  I’m not sure what you are trying to filter but you might be able to remove the filter parameter.  If not, take a look at this documentation for how to construct: https://docs.microsoft.com/en-us/previous-versions/windows/server/hh531527(v=ws.10)

       

    • #266918
      Participant
      Topics: 9
      Replies: 676
      Points: 2,674
      Helping Hand
      Rank: Community Hero

      Also please take a few moments to read the guide on posting code, and then update your post accordingly. Thanks!

    • #266933
      Participant
      Topics: 1
      Replies: 9
      Points: -7
      Rank: Member

      Doug Maurer, is it possible to edit my 1st post? I can’t seem to find an option to edit it.

    • #266963
      Participant
      Topics: 9
      Replies: 676
      Points: 2,674
      Helping Hand
      Rank: Community Hero

      Sometimes when someone posts a long link it can push the edit button off the visible page. One of the moderators will need to adjust that first. Thanks for wanting to, though!

    • #267071
      Participant
      Topics: 25
      Replies: 206
      Points: 772
      Helping Hand
      Rank: Major Contributor

      check your editor.

      it looks like you have the Microsoft broken double quotes IE the Description -notlike “*”

      vs Description -notlike “*”

      easy to muck up, but the microsoft broken quotes aren’t actual doublequotes

    • #267110
      Participant
      Topics: 1
      Replies: 9
      Points: -7
      Rank: Member

      David S, wow good eyes! I looked at it again and I still can’t tell the difference with those “*” they look the same to me.
      However, I had a copy of the codes from a previous script so copied and paste it again and it ran and output me 25 files! I am half way there.

      Now I need to send these 25 files to 25 different people.
      Is there a way to run a similiar foreach loop instead of writing the send email strings 25 times?

      Also, the 25 files output was defined with a varible by adding 1 so now the filenames are 10_30_2020_nodescription1.txt,
      10_30_2020_nodescription 2.txt up to 25.txt. Is there anyway to set the variable so it can name according to country?
      say 10_30_2020_nodescriptionUSA.txt, 10_30_2020_nodescriptionUK.txt, etc?

    • #267131
      Participant
      Topics: 5
      Replies: 170
      Points: 656
      Helping Hand
      Rank: Major Contributor

      Just to be clear $OU contains a list of countries (as numbers) in your code right?  If so, $_ or $PSItem will have the number inside the loop for the current country.  If you want to convert that to text, you’ll have to build a hashtable or some other data structure to lookup the country name.

    • #267155
      Participant
      Topics: 1
      Replies: 9
      Points: -7
      Rank: Member

      Mike R, correct the OU’s are all in different countries. I just changed it to number and letter to make it easier and shorter.

      Ok, I’ll have to google $_, $PSItem, hastable, etc. You just lost me. Thanks! I do appreciate the guidance so at least I know what to look for.

    • #267191
      Participant
      Topics: 25
      Replies: 206
      Points: 772
      Helping Hand
      Rank: Major Contributor

      ugh, now that i looked at my message, even more annoyed.

      looks like its the forum software doing the converting from standard double quotes to the broken ones.

      mental note when copying text from the forum to be on the lookout for.

    • #267203
      Participant
      Topics: 9
      Replies: 676
      Points: 2,674
      Helping Hand
      Rank: Community Hero

      Not when it’s formatted as code ^^

    • #267800
      Participant
      Topics: 1
      Replies: 9
      Points: -7
      Rank: Member

      Is there a foreach loop for sending files to 25 different people or an easier way than scripting 25 times of the sendmail instances and substituting the MAILTO?

    • #267809
      Participant
      Topics: 5
      Replies: 170
      Points: 656
      Helping Hand
      Rank: Major Contributor

      Is there a foreach loop for sending files to 25 different people or an easier way than scripting 25 times of the sendmail instances and substituting the MAILTO?

      Yes.  Get-Help about_foreach

      The online version of the documentation can be found here: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_foreach?view=powershell-5.1

      i.e.

       

      • This reply was modified 4 weeks, 1 day ago by Mike R..
    • #267836
      Participant
      Topics: 1
      Replies: 9
      Points: -7
      Rank: Member

      What about the $attachment in the sendmail section. How do I change the $attachment to point to different files per email?

    • #268511
      Participant
      Topics: 5
      Replies: 170
      Points: 656
      Helping Hand
      Rank: Major Contributor

      How do you know which person gets which attachment?  If it’s hardcoded like John Doe gets OU – 1 and Jane Smith get’s OU -2 … then I would create a hashtable of that and iterate over the array of hashtables.  Like this:

       

      • This reply was modified 4 weeks, 1 day ago by Mike R..
      • This reply was modified 4 weeks, 1 day ago by Mike R..
    • #268523
      Participant
      Topics: 1
      Replies: 9
      Points: -7
      Rank: Member

      yeah it’s confusing as I would need to know who gets what file unless i line them up that John always get 1.txt, Jane gets 2.txt, etc.

      If it possible to do a Read-host for the person’s email so if someone enters [email protected] then ok run this function sendemail such that “jsmith” gets this email attachment.

    • #268814
      Participant
      Topics: 5
      Replies: 170
      Points: 656
      Helping Hand
      Rank: Major Contributor

      How do you know who gets the file.  So if you prompted with a read-host, how would you know what to type.  I guess what I need to understand is the business rules you are trying to implement here.  If you want to prompt the user with a Read-Host, it is very easy to do.  Whatever the user enters is returned as a string and it can be assigned to a variable.  Get-Help Read-Host

    • #268838
      Participant
      Topics: 1
      Replies: 9
      Points: -7
      Rank: Member

      I need to run the Get-ADComputer query for 25 OUs for different countries and outfile a .txt file for computers with no descriptions for each OU. This I’ve done with the help here with the for-each loop.

      After the 25 files are outputted then I need to email the 25 attachments to different people such that USA.txt goes to John Smith, UK.txt goes to Jane Smith, etc. I wasn’t sure how this can be done except manually coding them in different MAILTO with each attachment so was thinking if READ HOST can identify if John Smith enters his email then it can somehow grab the correct attachment.

    • #268874
      Participant
      Topics: 25
      Replies: 206
      Points: 772
      Helping Hand
      Rank: Major Contributor

      there isn’t any way around it, you have to store “somewhere” whom gets what attatchment, if thats in code via hash-table, a database somewhere, or a simple csv file.

      All a read-host will do is take user input and store the test string as variable.

      if you won’t be changing whom gets what file ever, then you can do it in code, if you need to make changes frequently, you need to store that association elsewhere.

Viewing 22 reply threads
  • You must be logged in to reply to this topic.