Powershell模块 - 参数类型不匹配

时间:2018-04-16 12:28:25

标签: powershell parameters

我正在创建一个PS模块,根据我的公司命名方案确定New-AdUser中使用的用户名,但是当我传递firstname和lastname参数时,会出现错误,说明参数类型不匹配< / p>

Function Get-ADUsername {
Params(
[String] $FirstName,
[String] $LastName
)

# Determine the length of the first and last names
# Then depending on the outcome change the format of the names to become the username
if(($LastName).length -ge 6 -and ($FirstName).length -ge 2)
{
    $Username = $($Lastname).SubString(0,6) + $($Firstname).SubString(0,2)
}
elseif(($LastName).length -le 6 -and ($FirstName).length -ge 2)
{
    $Username = $($Lastname) + $($Firstname).SubString(0,2)
}
elseif(($LastName).length -le 6 -and ($FirstName).length -le 2)
{
    $Username = $($Lastname).SubString(0,6) + $($Firstname)
}

# Test if the username already exists
# If yes it removes the last letter and adds a number
# It keeps increasing the number depending if the username is in use
Try
{
Get-ADUser -Identity $Username -ErrorAction Stop
$usernameLength = $Username.Length - 1
$Username = $Username.SubString(0,$usernameLength)
[string]$Usernamenumber = "2"
    do
    {
        $Username = $Username + $Usernamenumber
        $Check = Get-ADUser -Identity $Username
        $Usernamenumber++
    }
    while($Check)
}
Catch
{
    break;
}
Return $Username
}

0 个答案:

没有答案