Using Variable Value in new cmdlet

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Rob Simmers Rob Simmers 2 years, 3 months ago.

  • Author
    Posts
  • #17940
    Profile photo of Juli
    Juli
    Participant

    I am fairly new to Powershell and need help with a syntax issue. I have a script that I use to process employee terminations. It works well but I am trying add a new piece to it. When the script finishes I need to be able to take the results of the get-mailboxexportrequest -status completed and disable the successfully completed archive.

    In order to do that I have to be able to use the returned 'Mailbox' identity property as this is what get-mailboxexportrequest returns and compare it back to the original list of items to get the correct alias or email address to use in the disable mailbox cmdlet.

    Here is where I am getting stuck. I import a csv file with the following information email address, sip address and sAMAccountName into the variable $mailboxes. I need to be able to take the sAMAccountName from $Mailboxes and use it to get the Mailbox identity for each item in the list into a new variable. I will then be able to use a foreach loop and compare back in a 'Where' clause to get the correct identity parameter to use with the disable cmdlet.

    I have tried the following:

    $mailboxobject = get-mailbox $mailboxes.samaccountname | select-object -property identity (this gives me an error about converting the System.Collections.ArrayList

    I also tried a foreach but that just looped and looped and looped with the Warning about only the first 1000 items being returned

    I am sure I am missing something important here, but am not quite sure what

  • #17941
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    It looks like Get-Mailbox does not accept an array for its Identity parameter, but it may accept pipeline input. (I don't have an Exchange environment up at the moment to know for sure). Try this:

    $mailboxObject = $mailboxes.samaccountname | get-mailbox | select-object -property identity
    
    • #17943
      Profile photo of Juli
      Juli
      Participant

      Thank you, that worked

  • #17944
    Profile photo of Juli
    Juli
    Participant

    Now, that I have the correct syntax, I need to take it a step further. I currently take a text file and using a C# script take the email address, query ldap and create a csv file with the sAMAccountName and the sip address. This file is then used in the script to process the termination. My goal is to get rid of the c# script and do the whole process in Powershell.

    Here is my question.

    Would it be better to query AD and add the values to the array variable created during the file import or would it be better to create a new array variable and include all the necessary information (email address from the first variable and sip, samaccountname and mailbox from the ad query)

  • #17956
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    Since you are just doing a AD-based query and you are going process each user, something like this would work:

    $emailList = Get-Content C:\Terminations\Terminations_8-11-2014.txt
    
    $emailList | foreach{
        # Get-ADUser requires RSAT tools installed
        $user = Get-ADUser -LDAPFilter "(emailaddress=$_)"
        if ($user) {
             #found a user, run term process
        }
        else {
            #didn't find a user with that email, do x
        }
    }

You must be logged in to reply to this topic.