Share via


Unable to find type [System.Management.Automation.ParameterBindingValidationException]. Make sure that the assembly that contains this type is loaded.

Question

Saturday, July 23, 2016 5:04 PM

Hi All,

For some reasons, I can't find the cause of this error. I'm trying to add new users into my AD Lab Environment. I tried this in the Production environment and it's giving me the same error.

The two variables, $oupath and $officers are having trouble. Any help are greatly appreciated. Nobody responded my question in Linkedin so I posted this here, hope someone can help me out....

See below:

==================================================================================

PS C:\Users\Administrator> $Error Unable to find type [System.Management.Automation.ParameterBindingValidationException]. Make sure that the assembly that contains this type is loaded. At C:\PowerShell\ADDS-SPRINT_v2.5.ps1:667 char:30 +                              New-ADUser -Path $oupath ` +                              ~~~~~~~~~~~~~~~~~~~~~~~~~~     + CategoryInfo          : InvalidOperation: (System.Manageme...dationException:TypeName) [], RuntimeException     + FullyQualifiedErrorId : TypeNotFound  

  PS C:\Users\Administrator> $Error[0].exception.Message Unable to find type [System.Management.Automation.ParameterBindingValidationException]. Make sure that the assembly that contains this type is loaded.

PS C:\Users\Administrator> $Error[0].FullyQualifiedErrorId TypeNotFound

PS C:\Users\Administrator> $Error[0].TargetObject

FullName     : System.Management.Automation.ParameterBindingValidationException
Name         : System.Management.Automation.ParameterBindingValidationException
AssemblyName :
IsArray      : False
IsGeneric    : False
Extent       : System.Management.Automation.ParameterBindingValidationException

============================================================== PS C:\Users\Administrator> $Error[0].ErrorDetails

PS C:\Users\Administrator> $Error[0].Exception.Source System.Management.Automation

PS C:\Users\Administrator> $Error[0].Exception.InnerException

PS C:\Users\Administrator> $Error[0].Exception.ErrorRecord Unable to find type [System.Management.Automation.ParameterBindingValidationException]. Make sure that the assembly that contains this type is loaded. At C:\PowerShell\ADDS-SPRINT_v2.5.ps1:667 char:30 +                              New-ADUser -Path $oupath ` +                              ~~~~~~~~~~~~~~~~~~~~~~~~~~     + CategoryInfo          : InvalidOperation: (System.Manageme...dationException:TypeName) [], ParentContainsErrorRecordException     + FullyQualifiedErrorId : TypeNotFound

PS C:\Users\Administrator> $Error[0].Exception.Data

Name                           Value                                                                                                                 
                                                                                                                                            
System.Management.Automatio... {Organizational-Unit, Description, CSV-File, Select-Options...}

Function Organizational-Unit
{
$script:csvPath = "C:\PowerShell\$CSVfilename2.csv"

$script:oupath="OU=Agents,OU=Marketing,DC=Adatum,DC=com"

$script:officers="OU=Officers,OU=Marketing,DC=Adatum,DC=com"

    cls
write-host " "`n `n
     
Write-Host "*****************************" -ForegroundColor Green
write-host `n
write-host "  1  :  A G E N T S         " -ForegroundColor Yellow #-BackgroundColor Gray
Write-host `n
write-host "  2  :  O F F I C E R S     " -ForegroundColor Red #-BackgroundColor White
Write-host `n
Write-Host "*****************************" -ForegroundColor Green
Write-host `n

write-host "Press [CTRL C] to exit." -BackgroundColor white -ForegroundColor black
Write-host `n

Write-host "CSV Filename:  " -NoNewline
Write-host "$csvPath"

Write-host `n
write-host "Enter number to specify which OU" -NoNewline -ForegroundColor Cyan
$ou = Read-Host " "


    Switch($ou)
    {

        1 {   # FOR AGENTS OU
            
                              
           $NewUsers = import-csv $csvPath
           if($? -eq $false) { Write-host `n `t "Error: Could not find CSV file 'C:\PowerShell\$CSVfilename2.CSV'" -ForegroundColor Red 
                 Write-host `n `t "Please type in CSV filename CORRECTLY."
                 $error.clear()
                 CSV-File
                 Break;
                        
           }

           
           else {

                write-host ""
                write-host "|  Adding New Users to AGENTS OU ]   |" -ForegroundColor Yellow
                write-host ""
                write-host `n

                $script:ntpwd = NT-Password
                $emailpassword = Email-Password
               
                    ForEach($user in $NewUsers)
                    {
                         $gn = ($user.GivenName)
                         $ln = ($user.SurName)
                         $n = ($user.name)
                         $dname = ($user.DisplayName)
                         $nt = ($user.SamAccountName)
                         $emailadd = ($user.EmailAddress)
                         $userPN = ($user.UserPrincipalName)
                         #$agents = ($user.Path)

                         Try {

                             New-ADUser -Path $oupath `
                             -GivenName $gn `
                             -Surname $ln `
                             -Name $n `
                             -DisplayName $dname `
                             -SamAccountName $nt `
                             -AccountPassword (ConvertTo-SecureString "$ntpwd" -AsPlainText -Force) `
                             -EmailAddress $emailadd `
                             -UserPrincipalName $userPN `
                             -Description $Description2 `
                             -Enabled $true -ChangePasswordAtLogon $true
                                
                              $count++      
                              continue;                      }  #Try

             
                         Catch [Microsoft.ActiveDirectory.Management.ADIdentityAlreadyExistsException]
                         {
                                Write-host ""
                                Write-host "       The specified account already exists.                          " -ForegroundColor Yellow
                                Write-host ""
                                Write-host "  Duplicate name" -ForegroundColor Yellow -NoNewline
                                write-host " [$n]" -NoNewline
                                Write-host " cannot be added into the same OU.     " -ForegroundColor Yellow
                                Write-host ""
                                #Write-host `n
                            
                                Write-host "Please check your CSV file" -ForegroundColor Green
                                Write-host `n `n

                                if($count -gt 0) { $count-- }

                                else { continue;   }
                                                            }  #Catch
             
                         Catch [Microsoft.ActiveDirectory.Management.ADException]
                         {
                                $x = $error[$count].CategoryInfo.Targetname

                                Write-host "DUPLICATE > [ $x ]"`n -BackgroundColor black
                                Enter-Credentials-Agents
                                Retrieve-Users
                                Continue-Query
                                break;                      }  #Catch
                                
                          Catch [System.Management.Automation.ParameterBindingValidationException]
                          {
                                
                                Write-Warning -Message 
                                "The system has detected some empty fields on your CSV file.
                                 Please open your CSV file with Notepad and delete the empty or white spaces with commas."
                                continue;
                          
                          }
                                 

                  } #For
                
                   
                             Retrieve-Users
                             Continue-Query
                             break;


            } #Else


        } #Switch AGENTS

#//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        2 {   #FOR OFFICERS OU

             $NewUsers = import-csv $csvPath
             if($? -eq $false) { Write-host `n `t "Error: Could not find CSV file 'C:\PowerShell\$CSVfilename2.CSV'" -ForegroundColor Red 
                 Write-host `n `t "Please type in CSV filename CORRECTLY."
                 $error.clear()
                 CSV-File
                 Break;   }

           
             else {

                write-host "" -ForegroundColor Yellow
                write-host "|  Adding New Users to OFFICERS OU  |"
                write-host "" -ForegroundColor Yellow
                write-host `n

                $script:ntpwd = NT-Password
                $emailpassword = Email-Password

               
                ForEach($user in $NewUsers)
                {
                     $gn2 = ($user.GivenName)
                     $ln2 = ($user.SurName)
                     $n2 = ($user.name)
                     $dname2 = ($user.DisplayName)
                     $nt2 = ($user.SamAccountName)
                     $emailadd2 = ($user.EmailAddress)
                     $userPN2 = ($user.UserPrincipalName)
                     #$officers = ($user.Path)

                      Try {

                             New-ADUser -Path $officers `
                             -GivenName $gn2 `
                             -Surname $ln2 `
                             -Name $n2 `
                             -DisplayName $dname2 `
                             -SamAccountName $nt2 `
                             -AccountPassword (ConvertTo-SecureString "$ntpwd" -AsPlainText -Force) `
                             -EmailAddress $emailadd2 `
                             -UserPrincipalName $userPN2 `
                             -Description $Description2 `
                             -Enabled $true -ChangePasswordAtLogon $true
                                       
                              $count++
                              continue;       }

                        Catch [Microsoft.ActiveDirectory.Management.ADIdentityAlreadyExistsException]
                        {
                                Write-host ""
                                Write-host "       The specified account already exists.                " -ForegroundColor Yellow
                                Write-host ""
                                Write-host "  Duplicate name [$n] cannot be added into the same OU.     " -ForegroundColor Yellow
                                Write-host ""
                                Write-host `n
                            
                                Write-host "Please check your CSV file" -ForegroundColor Green
                                Write-host `n `n

                                if($count -gt 0) { $count-- }

                                else { continue;   }
                                                            }  #Catch    
             
                         Catch [Microsoft.ActiveDirectory.Management.ADException]
                         {
                                $y = $error[$count].CategoryInfo.Targetname

                                Write-host "DUPLICATE > [ $y ]"`n -BackgroundColor black
                                Enter-Credentials-Officers
                                Retrieve-Users
                                Continue-Query
                                break;                      } #Catch  
                                
                         
                         Catch [System.Management.Automation.ParameterBindingValidationException]
                         {
                                
                                Write-Warning -Message 
                                "The system has detected some empty fields on your CSV file.
                                 Please open your CSV file with Notepad and delete the empty or white spaces with commas."
                                continue;
                          
                         }      
                                 

                  } #For
                
                   
                             Retrieve-Users
                             Continue-Query
                             break;

                } #Else

        } #Switch Officers


        #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\#

       Default
       {
                Write-host " "`n
                Write-Warning "INVALID input."

                Write-host `n "Try again? [y or n]" -ForegroundColor Yellow -NoNewline
                $ans = read-host " "

                if($ans -eq 'y')
                {

                    Organizational-Unit
                
                    Continue-Query

                }

                elseif($ans -eq 'n')
                {
                    Continue-Query
                }

                else
                {
            
                    Write-host `n "ERROR: Invalid input again." -ForegroundColor DarkRed -BackgroundColor white `n
                    Continue-Query
                }
            


         } #Default


    } #Switch Main


} #Function Organizational-Unit

All replies (11)

Saturday, July 23, 2016 7:02 PM ✅Answered

This is your error:

Catch [System.Management.Automation.ParameterBindingValidationException]
{
    
    Write-Warning -Message
    "The system has detected some empty fields on your CSV file.
                                 Please open your CSV file with Notepad and delete the empty or white spaces with commas."
    continue;
    

You are likely using the wrong version of PowerShell.  That exception is likely from PS 3 or later.

\(ツ)_/


Saturday, July 23, 2016 10:17 PM ✅Answered

You should avoid line continuation characters in script as they get lost real easy.

Here is how to make easy to use and read and manage code when CmdLets have too many parameters.

$splat=@{
    Path=$oupath
    GivenName=$agentFN
    Surname=$agentLN 
    DisplayName=$agentDN 
    Name=$agentFullName 
    SamAccountName=agentUN 
    AccountPassword=(ConvertTo-SecureString $ntpwd -AsPlainText -Force) 
    UserPrincipalName=$agentUPN 
    Description=4Description2 
    EmailAddress=$agentmail 
    Enabled=$true 
    ChangePasswordAtLogon=$true
}
New-ADUser @splat

This is also additive.  You can still add parameters after or before the line

All CmdLets and Advanced functions can be "splatted".  See "help about_splatting"

\(ツ)_/


Saturday, July 23, 2016 6:56 PM

Sorry but we cannot fix third party tools.  You need to contact the author for assistance.

Be sure you are using the correct version of PowerShell and that it is installed correctly with all patches and updates.

\(ツ)_/


Saturday, July 23, 2016 7:05 PM | 2 votes

No - the error is because that is not a valid exception type.

Change it to [System.Management.Automation.ParameterBindingException]

\(ツ)_/


Saturday, July 23, 2016 9:40 PM

Sorry but we cannot fix third party tools.  You need to contact the author for assistance.

Be sure you are using the correct version of PowerShell and that it is installed correctly with all patches and updates.

\(ツ)_/

I'm actually the author of the entire script. :)

Just found it too. Anyway it's not really important as I suppressed the $errorActionPreference to 'SilentlyContinue'. The script ran pretty well with the output files: 

C:\PowerShell\CE4D\NTLOGIN_EMAIL-ADDRESS_CE4D.csv
C:\PowerShell\CE4D\PASSWORDS_CE4D.txt

It's now good to go. Thank you very much for the help :)

Catch [System.Management.Automation.ParameterBindingValidationException]
{
    
    Write-Warning -Message
    "The system has detected some empty fields on your CSV file.
                                 Please open your CSV file with Notepad and delete the empty or white spaces with commas."
    continue;  }
    

Saturday, July 23, 2016 10:09 PM

The error type is not really accessible on any version of PowerShell.  It appears that some systems like SC has it and it is usable in SC workflows or from DSC.  Not sure why it is not visible.

You actually need to fix that as it can cause run-ons with unpredictable results.  Just use the fix I posted and then ad a general purpose trap for all unspecified exceptions.

Your fix is a very bad bail out as we should never run will no protection.  Bad things can happen.

Catch
{
    Throw # rethrow the exception and leave
    return
    #or
    Write-Host "$_" -fore Red  # post the message and leave
}

\(ツ)_/


Monday, July 25, 2016 6:15 PM

When using $splat, can i still call any variable in it?  like for example $agentUN


Monday, July 25, 2016 6:21 PM

When using $splat, can i still call any variable in it?  like for example $agentUN

"Call any variable"  What does this mean?

\(ツ)_/


Monday, July 25, 2016 6:25 PM

Here's the entire code for Function.

Function Organizational-Unit
{
$script:csvPath = "C:\PowerShell\$CSVfilename2.csv"

    cls
write-host " "`n `n
     
Write-Host "*****************************" -ForegroundColor Green
write-host `n
write-host "  1  :  A G E N T S         " -ForegroundColor Yellow #-BackgroundColor Gray
Write-host `n
write-host "  2  :  O F F I C E R S     " -ForegroundColor Red #-BackgroundColor White
Write-host `n
Write-Host "*****************************" -ForegroundColor Green
Write-host `n

write-host "Press [CTRL C] to exit." -BackgroundColor white -ForegroundColor black
Write-host `n

Write-host "CSV Filename:  " -NoNewline
Write-host "$csvPath"


Write-host `n
write-host "Enter number to specify which OU" -NoNewline -ForegroundColor Cyan
$ou = Read-Host " "

    Switch($ou)
    {
        1 {  # FOR AGENTS OU
                                  
           $NewUsers = import-csv $csvPath
           if($? -eq $false) { Write-host `n `t "Error: Could not find CSV file 'C:\PowerShell\$CSVfilename2.CSV'" -ForegroundColor Red 
                 Write-host `n `t "Please type in CSV filename CORRECTLY."
                 $error.clear()
                 CSV-File
                 Break;     } #if 
           
           else{
                    write-host ""
                    write-host "|  Adding New Users to AGENTS OU ]   |" -ForegroundColor Yellow
                    write-host ""
                    write-host `n

                    $script:ntpwd = NT-Password
                    $emailpassword = Email-Password
               
                    ForEach($user in $NewUsers)
                    {
                         $gn = ($user.GivenName)
                         $ln = ($user.SurName)
                         $n = ($user.name)
                         $dname = ($user.DisplayName)
                         $nt = ($user.SamAccountName)
                         $emailadd = ($user.EmailAddress)
                         $userPN = ($user.UserPrincipalName)

                         Try{
                                 New-ADUser -Path $agents `
                                 -GivenName $gn `
                                 -Surname $ln `
                                 -Name $n `
                                 -DisplayName $dname `
                                 -SamAccountName $nt `
                                 -AccountPassword (ConvertTo-SecureString "$ntpwd" -AsPlainText -Force) `
                                 -EmailAddress $emailadd `
                                 -UserPrincipalName $userPN `
                                 -Description $Description2 `
                                 -Enabled $true -ChangePasswordAtLogon $true -ErrorAction STOP
                                
                                  $count++      
                                  continue;                      }  #Try

                                            
                         Catch [Microsoft.ActiveDirectory.Management.ADException]
                         { #outside
                                   Try {
                                   $script:un1 = $gn.Substring(0,2)+"."+$ln
                                   $script:upn1 = $un1+$lab
                                   $script:email1 = $gn.Substring(0,2)+$lab
                                                                   
                                       New-ADUser -Path $agents `
                                         -GivenName $gn `
                                         -Surname $ln `
                                         -Name $n `
                                         -DisplayName $dname `
                                         -SamAccountName $un1 `
                                         -AccountPassword (ConvertTo-SecureString "$ntpwd" -AsPlainText -Force) `
                                         -EmailAddress $email1 `
                                         -UserPrincipalName $upn1 `
                                         -Description $Description2 `
                                         -Enabled $true -ChangePasswordAtLogon $true -ErrorAction STOP 
                                
                                         $count++      } #Try

                                   Catch [Microsoft.ActiveDirectory.Management.ADException]
                                   { #inside

                                       $script:un2 = $gn.Substring(0,3)+"."+$ln
                                       $script:upn2 = $un2+$lab
                                       $script:email2 = $gn.Substring(0,3)+$lab
                                     
                                       New-ADUser -Path $agents `
                                         -GivenName $gn `
                                         -Surname $ln `
                                         -Name $n `
                                         -DisplayName $dname `
                                         -SamAccountName $un2 `
                                         -AccountPassword (ConvertTo-SecureString "$ntpwd" -AsPlainText -Force) `
                                         -EmailAddress $email2 `
                                         -UserPrincipalName $upn2 `
                                         -Description $Description2 `
                                         -Enabled $true -ChangePasswordAtLogon $true
                                
                                         $count++        } #Catch inside                                                                                          
                                                            
                         }  #Catch outside

                         Catch [Microsoft.ActiveDirectory.Management.ADIdentityAlreadyExistsException]
                         {
                                Write-host ""
                                Write-host "       The specified account already exists.                          " -ForegroundColor Yellow
                                Write-host ""
                                Write-host "  Duplicate name" -ForegroundColor Yellow -NoNewline
                                write-host " [$n]" -NoNewline
                                Write-host " cannot be added into the same OU.     " -ForegroundColor Yellow
                                Write-host ""
                                #Write-host `n
                            
                                Write-host "Please check your CSV file" -ForegroundColor Green
                                Write-host `n `n

                                if($count -gt 0) { $count-- }

                                else { continue;   }
                                                            }  #Catch
                                
                  } #For
                
                             $script:totalcount = $count
                             Retrieve-Users
                             Continue-Query
                             break;

            } #Else


        } #Switch AGENTS

#//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        2 {   #FOR OFFICERS OU

             $NewUsers = import-csv $csvPath
             if($? -eq $false) { Write-host `n `t "Error: Could not find CSV file 'C:\PowerShell\$CSVfilename2.CSV'" -ForegroundColor Red 
                 Write-host `n `t "Please type in CSV filename CORRECTLY."
                 $error.clear()
                 CSV-File
                 Break;   }  #if

             else{
                   
                    write-host ""
                    write-host "|  Adding New Users to OFFICERS OU ]   |" -ForegroundColor Yellow
                    write-host ""
                    write-host `n

                    $script:ntpwd = NT-Password
                    $emailpassword = Email-Password
           
                    ForEach($user in $NewUsers)
                    {
                         $gn = ($user.GivenName)
                         $ln = ($user.SurName)
                         $n = ($user.name)
                         $dname = ($user.DisplayName)
                         $nt = ($user.SamAccountName)
                         $emailadd = ($user.EmailAddress)
                         $userPN = ($user.UserPrincipalName)

                         Try{  #Try outside
                             New-ADUser -Path $agents `
                             -GivenName $gn `
                             -Surname $ln `
                             -Name $n `
                             -DisplayName $dname `
                             -SamAccountName $nt `
                             -AccountPassword (ConvertTo-SecureString "$ntpwd" -AsPlainText -Force) `
                             -EmailAddress $emailadd `
                             -UserPrincipalName $userPN `
                             -Description $Description2 `
                             -Enabled $true -ChangePasswordAtLogon $true -ErrorAction STOP
                                
                              $count++      
                              continue;                      }  #Try outside            
                                 
                         Catch [Microsoft.ActiveDirectory.Management.ADException]
                         { #outside

                               Try{ #inside
                                   $script:nt1 = $gn.Substring(0,2)+"."+$ln
                                   $script:ntupn1 = $nt1+$lab
                                   $script:emailadd1 = $gn.Substring(0,2)+$lab
                                                                   
                                   New-ADUser -Path $agents `
                                     -GivenName $gn `
                                     -Surname $ln `
                                     -Name $n `
                                     -DisplayName $dname `
                                     -SamAccountName $nt1 `
                                     -AccountPassword (ConvertTo-SecureString "$ntpwd" -AsPlainText -Force) `
                                     -EmailAddress $emailadd1 `
                                     -UserPrincipalName $ntupn1 `
                                     -Description $Description2 `
                                     -Enabled $true -ChangePasswordAtLogon $true -ErrorAction STOP 
                                
                                     $count++      } #try inside

                               Catch [Microsoft.ActiveDirectory.Management.ADException]
                               { #inside

                                   $script:nt2 = $gn.Substring(0,3)+"."+$ln
                                   $script:ntupn2 = $nt2+$lab
                                   $script:emailadd2 = $gn.Substring(0,3)+$lab
                                     
                                   New-ADUser -Path $agents `
                                     -GivenName $gn `
                                     -Surname $ln `
                                     -Name $n `
                                     -DisplayName $dname `
                                     -SamAccountName $nt2 `
                                     -AccountPassword (ConvertTo-SecureString "$ntpwd" -AsPlainText -Force) `
                                     -EmailAddress $emailadd2 `
                                     -UserPrincipalName $ntupn2 `
                                     -Description $Description2 `
                                     -Enabled $true -ChangePasswordAtLogon $true
                                
                                     $count++        } #Catch inside
                                                                                                                                                                                        
                         }  #Catch outside

                         Catch [Microsoft.ActiveDirectory.Management.ADIdentityAlreadyExistsException]
                         {
                                Write-host ""
                                Write-host "       The specified account already exists.                          " -ForegroundColor Yellow
                                Write-host ""
                                Write-host "  Duplicate name" -ForegroundColor Yellow -NoNewline
                                write-host " [$n]" -NoNewline
                                Write-host " cannot be added into the same OU.     " -ForegroundColor Yellow
                                Write-host ""
                                #Write-host `n
                            
                                Write-host "Please check your CSV file" -ForegroundColor Green
                                Write-host `n `n

                                if($count -gt 0){ $count-- }

                                else { continue;  }                                                          
                                                            }  #Catch
                                
                   } #For
                
                             $script:totalcount = $count
                             Retrieve-Users
                             Continue-Query
                             break;

            } #else


       } #Switch Officers


#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\#

     Default
     {
                Write-host " "`n
                Write-Warning "INVALID input."

                Write-host `n "Try again? [y or n]" -ForegroundColor Yellow -NoNewline
                $ans = read-host " "

                if($ans -eq 'y')
                {
                    Organizational-Unit
                    Continue-Query         }

                elseif($ans -eq 'n')
                {
                    Continue-Query         }

                else
                {
                    Write-host `n "ERROR: Invalid input again." -ForegroundColor DarkRed -BackgroundColor white `n
                    Continue-Query
                }
            
         } #Default


    } #Switch Main


} #Function Organizational-Unit

Monday, July 25, 2016 6:30 PM

That does not explain your question.  You have to explain what you are asking as it doesn't make much sense.  What does "call a variable" ?  Are you asking how to assign the  variables?  Nothing is different.  The variables get assigned as before only we place them in a "splat" so they are easier to manage.

\(ツ)_/


Friday, May 4, 2018 5:16 PM

I was having the same issue, using a current version of PowerShell, and this is the answer that fixed my problem.