Command with -whatif returns different object?

Welcome Forums General PowerShell Q&A Command with -whatif returns different object?

This topic contains 6 replies, has 3 voices, and was last updated by

 
Participant
3 months, 3 weeks ago.

  • Author
    Posts
  • #105301

    Participant
    Points: 2
    Rank: Member

    Good Afternoon,

    I have some code that selects a number of users to add them into an AD security group, if I use -whatif on 'Add-QADGroupMember' the PSObject I create is correct and just writes the SAMAccountName to a CSV. If I remove the -whatif parameter and actually carry out the action to add the user to the AD security group then it writes the entire user object (all attributes) to the CSV but I am not sure why?

    Please could you help, here is the code:

    Add-PSSnapin Quest.ActiveRoles.ADManagement | Out-Null
    Connect-QADService "Domain" | Out-Null
    
    $GroupName = 'MyADGroupName'
    $CurrentGroupMembers =  Get-QADGroupMember -Identity $GroupName | ? {$_.ObjectClass -eq 'user'} | Select-Object -ExpandProperty SamAccountName
    
    # Get Random List of Users (100)
    
    $Items = Get-QADUser -SearchRoot "Domain/OU" -SizeLimit 0 | ? {$CurrentGroupMembers -notcontains $_.SamAccountName}  | Sort-Object -Property Name | Sort-Object{Get-Random} | Select-Object -First 100
    
    $CustomOutput = ForEach ($Item in $Items) {
        $User = Get-QADUser -Identity $Item.SAMAccountName
        Add-QADGroupMember -Identity $GroupName -Member $User -proxy
         New-Object PSObject -Property @{
            SamAccountName = $Item.samAccountName
         }
    } 
    
    $CustomOutput | Export-Csv c:\users\myuser\desktop\$(Get-Date -Format 'ddMMyyyy').csv -NoClobber -NoTypeInformation
    

    Thanks very much for any help you can provide

  • #105307

    Keymaster
    Points: 1,625
    Helping HandTeam Member
    Rank: Community Hero

    Because that's the output of the New- command.

    • #105310

      Participant
      Points: 2
      Rank: Member

      Hi Don,

      Could you explain a bit more please,

      if line 13 is:

      Add-QADGroupMember -Identity $GroupName -Member $User -proxy -whatif

      then my $CustomObject variable is of type PSObject and just contains the SAMAccountNames in a list that gets written to CSV

      if line 13 is:

      Add-QADGroupMember -Identity $GroupName -Member $User -proxy

      then my $CustomObject variable is of type array and contains all user attributes in a list that gets written to CSV

      Thanks for your help so far

  • #105311

    Participant
    Points: 165
    Helping Hand
    Rank: Participant

    Does the Add-QADGroupMember command output anything when you test it alone? If so, you may need to explicitly discard the output.
    Also, I wouldn't recommend making a PSObject like that. PSObjects are primarily a wrapper type used in a lot of backend powershell code. Items passing to the output stream or through the pipeline are wrapped in a PSObject wrapper in most cases, and if it's already a PSObject they can end up... a bit odd, sometimes.

    Instead, this is much quicker and more versatile:

    # Rather than this:
    New-Object PSObject -Property @{
            SamAccountName = $Item.samAccountName
         }
    # Do this:
    [PSCustomObject]@{
        SamAccountName = $Item.SamAccountName
    }
    • #105326

      Participant
      Points: 2
      Rank: Member

      Hi Joel,

      I have modified the code to use the more versatile method of creating a PSObject but this has not fixed it unfortunately.

      What I can tell you is that when using the -whatif parameter my $CustomOutput is of type PSObject, but if I omit -whatif then my $CustomOutput is of type System.Array?

      If I run the 'Add-QADGroupMember' command on its own it looks like it returns the full user object that it is performing the operation on in an array so maybe that is what is causing the problem?

      Thanks for any further insight you can provide

  • #105320

    Keymaster
    Points: 1,625
    Helping HandTeam Member
    Rank: Community Hero

    Whatever gets written to the pipeline will end up in your CSV. When New-QADGroupMember runs, what's it output?

    If you need to suppress the output of a command, pipe it to Out-Null.

    • #105338

      Participant
      Points: 2
      Rank: Member

      That fixed it, thanks very much Don. I suppose what I did not understand is that I thought the PSCustomObject I create only contained one field but then I realise I create the $CustomObject variable above the loop so it will contain anything that is output via a command that is run within the loop.

      I added the following comment after 'Out-Null' to the code to ensure I remember it next time:

      $CustomOutput = ForEach ($Item in $Items) {
          Add-QADGroupMember -Identity $GroupName -Member $Item.SamAccountName -proxy | Out-Null #Required to strip any output the command generates when run so that it is not included in the $CustomOutput variable
               [PSCustomObject] @{
              SamAccountName = $Item.samAccountName
           }
      } 
      
      $CustomOutput | Export-Csv c:\users\user\desktop\$(Get-Date -Format 'ddMMyyyy').csv -NoClobber -NoTypeInformation
      

The topic ‘Command with -whatif returns different object?’ is closed to new replies.