Running script based on results

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 4 months ago.

  • Author
    Posts
  • #6169

    by Demonic240 at 2012-08-28 12:04:30

    Hey all,

    I have two powershell scripts that I am trying to run for proper documentation. The first script is used to find inactive users (based on our policy) and disable them.

    [quote]
    Get-QADUser -SearchRoot "domain/container" -SearchScope 'OneLevel' -sizeLimit 0 -NotLoggedOnFor 90 -LastChangedBefore (Get-Date).adddays(-30) | Disable-QADUser | Select-Object logonName, DisplayName | export-csv \\location.csv [/quote]

    The second script is something that we are using to document the notes field in AD without overwriting the notes that are already there.

    [quote]
    $note = "Account disabled due to inactivity on "
    function AddNote($User)
    {
    $search = New-Object DirectoryServices.DirectorySearcher([ADSI]“”)
    $search.filter = “(&(objectClass=user)(sAMAccountName=$user))”
    $results = $search.Findall()

    foreach($result in $results){
    $userEntry = $result.GetDirectoryEntry()
    $CNotes = Get-QADUser $User
    $MNotes = $CNotes.notes
    if ($Mnotes.length -lt 1) {Set-QADUser -Identity $User -notes "$note"}
    else {$NoteResults = $MNotes
    $NoteResults = "$NoteResults`r`n"
    $NoteResults = $NoteResults + "$note"
    Set-QADUser -Identity $User -notes "$NoteResults"}
    }
    }

    Function AddNotes
    {
    process{
    foreach($user in $_){
    AddNote($User)
    }
    }
    }

    Get-Content “.\users.txt” | AddNotes[/quote]

    I'm having a difficult time feeding the affected users from the first script into the second script. Thank you in advance for any assistance.

    by DonJ at 2012-08-28 12:46:21

    Ok, what difficulty are you having? What does Users.txt contain?

    by Demonic240 at 2012-08-28 12:49:50

    [quote="DonJ"]Ok, what difficulty are you having? What does Users.txt contain?[/quote]

    users.txt is a plain text file that only contains usernames for the users being modified. I tried running the second script automatically after the export of the first one and setting the get-content to the exported file, but the first two lines of the exported file aren't usernames. I was trying to find a way to run the function from script 2 against each user in script 1.

    by poshoholic at 2012-08-28 19:44:46

    You'll want to use Import-Csv to get the contents of the CSV file you create in the first step instead of Get-Content. That will give you objects with properties. Then the easiest approach without updating your AddNote function would be to use ForEach-Object, like this:

    Import-Csv C:\myCsvFile.csv | ForEach-Object {AddNote $_.LogonName}

    With this solution you don't need to create an AddNotes processing function. Alternatively you could keep AddNotes around and internally pass $_.LogonName instead of just $_ into the AddNote function and that would work too. There are a lot of other possibilities here, such as converting AddNote to an advanced function with a parameter that takes pipeline input by name or by value, which would remove the need for AddNotes and ForEach-Object. It's up to you if you want to go that route or not.

    by Demonic240 at 2012-08-29 06:14:18

    [quote="poshoholic"]You'll want to use Import-Csv to get the contents of the CSV file you create in the first step instead of Get-Content. That will give you objects with properties. Then the easiest approach without updating your AddNote function would be to use ForEach-Object, like this:

    Import-Csv C:\myCsvFile.csv | ForEach-Object {AddNote $_.LogonName}

    With this solution you don't need to create an AddNotes processing function. Alternatively you could keep AddNotes around and internally pass $_.LogonName instead of just $_ into the AddNote function and that would work too. There are a lot of other possibilities here, such as converting AddNote to an advanced function with a parameter that takes pipeline input by name or by value, which would remove the need for AddNotes and ForEach-Object. It's up to you if you want to go that route or not.[/quote]

    Could I add the | ForEach-Object {AddNote $_.LogonName} command to the end of the first script? I'll have multiple scripts running to disable accounts in specific OU's and would like to not have to create an add notes script for each one.

    by poshoholic at 2012-08-29 06:32:54

    Sure. You don't need to export to csv and then import the data back in at all. You simply need the ForEach-Object call to indicate you want the LogonName property to pass through into the AddNote call. With that approach, you could also drop the Select-Object statement since you don't really need to isolate only one or two properties.

    by Demonic240 at 2012-08-29 07:04:41

    [quote="poshoholic"]Sure. You don't need to export to csv and then import the data back in at all. You simply need the ForEach-Object call to indicate you want the LogonName property to pass through into the AddNote call. With that approach, you could also drop the Select-Object statement since you don't really need to isolate only one or two properties.[/quote]

    edit: Was able to add the function to the one script. My test looks like it's working now! Thank you very much 🙂

    by poshoholic at 2012-08-29 07:27:53

    Glad I could help. I've gone ahead and marked this as solved since it's working for you.

You must be logged in to reply to this topic.