TextBox.Text gets saved as System.Object

45 Views Asked by At

I started writing a script to copy specific attributes from one AD-User and create a new User with those attributes and a different name. So far so well, it worked in the past. I deleted a codeblock that didnt get used since I never implemented it, and now i always get an error. I found out where the error is, but I have no clue how it came to be

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")


Function CopyUser($copyfrom, $_firstname, $_lastname){
    Write-Host $_firstname
    Write-Host $_firstname.GetType().FullName
    Write-Host $_lastname
    Write-Host $_lastname.GetType().FullName
    $mailpre = $_firstname.Substring(0,1).ToLower() + "." + $_lastname.ToString().ToLower()
    Write-Host $mailpre
    $samaccountname = SearchValidShort $_firstname $_lastname
    $displayname = $_firstname + " " + $_lastname
    $firstname = $_firstname
    $lastname = $_lastname
    $logon_name = $mailpre
    $password = ''
    $description = 'DOM-ADMIN'
    $DN = 'OU=Service Accounts,OU=User,OU=ORG,DC=ndsedv,DC=de'
    $enable = $true
    
    $copy = Get-ADUser -Identity $copyfrom -Properties *
    $params = @{'SamAccountName' = $samaccountname;
                'Instance' = $copyfrom;
                'DisplayName' = $displayname;
                'GivenName' = $firstname;
                'SurName' = $lastname;
                'Description' = $copy.Description;
                'Enabled' = $enable;
                'UserPrincipalName' = $logon_name + "";
                'Initials' = $name;
                'EmailAddress' = $logon_name + "";
                'Title' = $copy.Title;
                'Department' = $copy.Department;
                'Manager' = $copy.Manager;
                'city' = $copy.city;
                'company' = $copy.company;
                'country' = $copy.country;
                'postalCode' = $copy.postalCode;
                'streetAddress' = $copy.streetAddress;
                'AccountPassword' = (ConvertTo-SecureString -AsPlainText $password -Force);


    } 
    New-ADUser -Name $name @params
    $params
    $copy.memberOf | %{Add-ADGroupMember $_ $samaccountname}

    
    Write-Host $copy
}

Function SearchValidShort($_firstname, $_lastname){
    $givenInitials = (Get-ADUser -Filter {enabled -eq $true -and Initials -like "*"} -Properties Initials).Initials
    $newInitials = $_firstname.Substring(0,2).ToUpper() + $_lastname.Substring(0,2).ToUpper()
    $_givenInitials = ""
    $lastNameTemp = 2
    while($givenInitials -contains $newInitials){
        $newInitials = $_firstname.Substring(0,2).ToUpper() + $_lastname.Substring(0,1).ToUpper() + $_lastname.Substring($lastNameTemp,1).ToUpper()
        $lastNameTemp += 1
    }
    return $newInitials
}


$objForm = New-Object System.Windows.Forms.Form
$objForm.StartPosition = "CenterScreen"
$objForm.Size = New-Object System.Drawing.Size(675,220)
$objForm.Text = "AD Nutzer kopieren"
$objForm.Backcolor="white"
$objForm.KeyPreview = $True


$objLabelInitials = New-Object System.Windows.Forms.Label
    $objLabelInitials.Location = New-Object System.Drawing.Size(20,20)
    $objLabelInitials.Size = New-Object System.Drawing.Size(320,20)
    $objLabelInitials.Text = "Bitte geben sie das Kürzel des zu kopierenden Benutzers an:"
    $objForm.Controls.Add($objLabelInitials)

$objTextBoxInitials = New-Object System.Windows.Forms.TextBox
    $objTextBoxInitials.Location = New-Object System.Drawing.Size(340,20)
    $objTextBoxInitials.Size = New-Object System.Drawing.Size(300,20)
    $objForm.Controls.Add($objTextBoxInitials)

$objLabelNewFirstname = New-Object System.Windows.Forms.Label
    $objLabelNewFirstname.Location = New-Object System.Drawing.Size(20,50)
    $objLabelNewFirstname.Size = New-Object System.Drawing.Size(320,20)
    $objLabelNewFirstname.Text = "Bitte geben sie Vornamen des Neuen Benutzers ein:"
    $objForm.Controls.Add($objLabelNewFirstname)

$objTextBoxNewFirstname = New-Object System.Windows.Forms.TextBox
    $objTextBoxNewFirstname.Location = New-Object System.Drawing.Size(340,47)
    $objTextBoxNewFirstname.Size = New-Object System.Drawing.Size(300,20)
    $objForm.Controls.Add($objTextBoxNewFirstname)


$objLabelNewLastname = New-Object System.Windows.Forms.Label
    $objLabelNewLastname.Location = New-Object System.Drawing.Size(20,80)
    $objLabelNewLastname.Size = New-Object System.Drawing.Size(320,20)
    $objLabelNewLastname.Text = "Bitte geben sie den Nachnamen des Neuen Benutzers ein:"
    $objForm.Controls.Add($objLabelNewLastname)

$objTextBoxNewLastname = New-Object System.Windows.Forms.TextBox
    $objTextBoxNewLastname.Location = New-Object System.Drawing.Size(340,77)
    $objTextBoxNewLastname.Size = New-Object System.Drawing.Size(300,20)
    $objForm.Controls.Add($objTextBoxNewLastname)


$SubmitButton = New-Object System.Windows.Forms.Button
    $SubmitButton.Location = New-Object System.Drawing.Size(232,140)
    $SubmitButton.Size = New-Object System.Drawing.Size(150,23)
    $SubmitButton.Text = "Nutzer kopieren"
    $SubmitButton.Name = "Nutzer kopieren"
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
    {CopyUser $objTextBoxInitials.Text $objTextBoxNewFirstname.Text $objTextBoxNewLastname.Text, $objForm.Close()}
    })
    $SubmitButton.Add_Click({CopyUser $objTextBoxInitials.Text $objTextBoxNewFirstname.Text $objTextBoxNewLastname.Text, $objForm.Close()})
    $objForm.Controls.Add($SubmitButton)

[void] $objForm.ShowDialog()

If I run this code I always get the same errors:

Testfirstname
System.String
Testlastname 
System.Object[]
t.system.object[]
Es ist nicht möglich, eine Methode für einen Ausdruck aufzurufen, der den NULL hat.
In C:\Users\administrator.WEILDE2K\Desktop\copyADUser.ps1:55 Zeichen:5
+     $newInitials = $_firstname.Substring(0,2).ToUpper() + $_lastname. ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

I tried using .ToString() in the CopyUser function, which stopped the error being thrown there, but I still get the wrong output. Before that I would get the same error as in the SearchValidShort function.

Does anyone have an idea why the $_lastname type is system.object and knows how to fix it?

Thanks in advance

0

There are 0 best solutions below