none
Pipeline Input Help! RRS feed

  • Question

  • Hi guys,

    I'm having a bit of difficulty turning a script of mine into a re distributable tool and I've spent the best part of two days on it so here I am looking for some guidance.

    Basically I am using Quest-AD cmdlets to copy users whilst adding unique information to them as they are created.

    When I attempt to turn this into a parametrised cmdlet I have an issue in getting the get-qaduser cmdlet to recognise the "modelID" argument supplied by import-csv.

    #Create function
    function new-bulkaduser {
        [CmdletBinding()]    
    
    #Setup Import and Set Variable
    #Make sure path is reachable 
    param(
          [Parameter(Mandatory=$true,
                    ValueFromPipeline=$true,
                    ValueFromPipelineByPropertyName=$true)]                             
                [string[]]$userlist='localhost'
    )
    
    Begin {
    
    #import AD and Quest Snap-In
    import-module activedirectory
    Add-PSSnapin Quest.ActiveRoles.ADManagement
    
    }
    
    Process {
    
    ForEach ($User in $Userlist)
    {
        $source=$User.ModelID
        $fn=$User.FirstName
        $ln=$User.LastName
        $pw=$User.UserPassword
        $un=$User.SamAccountName
        $ip=$User.IpPhone
        $gn=$User.Group
        $pc = (Get-QADUser -identity $source).parentcontainer
        $domain = 'PINKE.NET'
     
    
    get-qaduser -includeallproperties -identity $source
    
    }
    
    }
    
    }

    And a .csv example below:

    ModelID,Name,SamAccountName,DisplayName,FirstName,LastName,IPPhone,UserPassword,group
    cmnaa,TestUserx1,TUserx1,Test Userx1,Testx1,Userx1,6661,B1xgBuxxx01,UK Service Desk

    Any help would be appreciated! 

    Thanks!



    • Edited by Skullicious Monday, August 11, 2014 3:56 PM
    Monday, August 11, 2014 2:33 PM

Answers

  • Your userlist contains blank entries.  Just write trace statements to see what is actually being sent.

    The following is wrong. If you set a default then"Mandatory" is not used.

    It should be this way:

    Param(
         [Parameter(          Mandatory=$true,
              
    ValueFromPipeline=$true,
              
    ValueFromPipelineByPropertyName=$true     )]$userlist
    )

    A CSV is not [string[]]  it is [Object[]]

    Start like this and fix all errors before adding to it.

    function new-bulkaduser{
        [CmdletBinding()]    
        Param(
            [Parameter(
                Mandatory=$true,
                ValueFromPipeline=$true,
                ValueFromPipelineByPropertyName=$true
            )][object[]]$userlist
        )
    
        Begin{
            Add-PSSnapin Quest.ActiveRoles.ADManagement
        }
    
        Process{
            
            foreach($user in $userlist){
                Write-Host "Processing ID: $($user.ModelID)" -ForegroundColor green
                Get-QADUser -identity $user.ModelID
            }
            
        }
    }

    Be careful with code format so errors are not lost.



    ¯\_(ツ)_/¯

    • Marked as answer by Skullicious Tuesday, August 12, 2014 11:20 AM
    Tuesday, August 12, 2014 8:41 AM

All replies

  • There is no such field as ModelId in your CSV file:

    See: identity, Name, SamAccountName, DisplayName, FirstName, LastName, IPPhone, UserPassword, group

    Forget about building CmdLets.  Start by getting good tested code to work at a prompt then use what you have learned to create a simple function.  Once you have a woking function then try to learn how to make an Advanced Function.


    ¯\_(ツ)_/¯

    Monday, August 11, 2014 2:43 PM
  • Sorry  my bad!

    There is a "modelID" in my csv, that was from me fiddling around earlier.

    Monday, August 11, 2014 3:56 PM
  • What is the error?  How do you know it doesn't work.  What is in ModelID?  Is it a name/samname/DN/UPN?  What is it?


    ¯\_(ツ)_/¯

    Monday, August 11, 2014 4:13 PM
  • Hey jrv,

    The error is that get-qaduser thinks I am passing it a null/empty value so that it cannot run:

    get-qaduser -includeallproperties -identity $source

    The model ID is a samAccountName but I don't think it's getting as far as to accept that as a property.

    I don't think the foreach loop is doing anything with the pipeline input.. Do you know how can I test this?


    Tuesday, August 12, 2014 8:24 AM
  • Your userlist contains blank entries.  Just write trace statements to see what is actually being sent.

    The following is wrong. If you set a default then"Mandatory" is not used.

    It should be this way:

    Param(
         [Parameter(          Mandatory=$true,
              
    ValueFromPipeline=$true,
              
    ValueFromPipelineByPropertyName=$true     )]$userlist
    )

    A CSV is not [string[]]  it is [Object[]]

    Start like this and fix all errors before adding to it.

    function new-bulkaduser{
        [CmdletBinding()]    
        Param(
            [Parameter(
                Mandatory=$true,
                ValueFromPipeline=$true,
                ValueFromPipelineByPropertyName=$true
            )][object[]]$userlist
        )
    
        Begin{
            Add-PSSnapin Quest.ActiveRoles.ADManagement
        }
    
        Process{
            
            foreach($user in $userlist){
                Write-Host "Processing ID: $($user.ModelID)" -ForegroundColor green
                Get-QADUser -identity $user.ModelID
            }
            
        }
    }

    Be careful with code format so errors are not lost.



    ¯\_(ツ)_/¯

    • Marked as answer by Skullicious Tuesday, August 12, 2014 11:20 AM
    Tuesday, August 12, 2014 8:41 AM
  • Hey jrv,

    The error is that get-qaduser thinks I am passing it a null/empty value so that it cannot run:

    get-qaduser -includeallproperties -identity $source

    The model ID is a samAccountName but I don't think it's getting as far as to accept that as a property.

    I don't think the foreach loop is doing anything with the pipeline input.. Do you know how can I test this?


    Hope there is no space in ModelID in CSV ?

    Import-Csv C:\Temp\test.csv | %{
    $source = $_.ModelID
    Get-QADUser -Identity $source
    }

    I got the result.

    Import-Csv C:\Users\904870\Desktop\test.csv | %{
    $source = $_.ModelID
    #Get-QADUser -IncludeAllProperties -Identity $source
    
    (Get-QADUser -Identity $source).ParentContainer
    }
    This worked as well.

    If you are passing null values you should see this error

    Get-QADUser : Cannot validate argument on parameter 'Identity'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command
    again.

    Worth to check your $erroractionpreference value


    Regards Chen V [MCTS SharePoint 2010]


    • Edited by Chen VMVP Tuesday, August 12, 2014 8:50 AM
    Tuesday, August 12, 2014 8:47 AM
  • Hey Chen,

    Thanks!

    I can pass values from import-csv to the get-qaduser cmdlet with no issues by either script or by a one-liner.

    The issue I am experiencing is when creating an advance function with parameters to accept pipeline input by property so to the end user it would look like...

    import-csv C:\newuser.csv | new-bulkADusers

    Thanks!


    Tuesday, August 12, 2014 9:35 AM
  • Hey Chen,

    Thanks!

    I can pass values from import-csv to the get-qaduser cmdlet with no issues by either script or by a one-liner.

    The issue I am experiencing is when creating an advance function with parameters to accept pipeline input by property so to the end user it would look like...

    import-csv C:\newuser.csv | new-bulkADusers

    Thanks!



    the problem is in your design of the function and your use of the PowerShell syntax.  Start with the version I posted and you will see the issue you are having.

    ¯\_(ツ)_/¯

    Tuesday, August 12, 2014 10:28 AM
  • Hey jrv,

    Thanks for the amended code!

    There were no spaces in my modelID column but passing the parameter as [object] instead of [string] done the trick.

    Thanks again!

    Tuesday, August 12, 2014 11:19 AM
  • Hey jrv,

    Thanks for the amended code!

    There were no spaces in my modelID column but passing the parameter as [object] instead of [string] done the trick.

    Thanks again!

    It didn't done anything dudie.  You can't coerce a CSV into a string and have it work.

    I suspect a course in PowerShell fundamentrals would help.  YOU need to learn the basics before trying to do these fancy things.


    ¯\_(ツ)_/¯

    Tuesday, August 12, 2014 12:44 PM
  • Thanks for the tip.
    Tuesday, August 12, 2014 2:38 PM