我正在創建一個 PowerShell 腳本,以根據一些 AD 資訊創建一個唯一 ID。這是我到目前為止所擁有的。
Import-Module -Name ActiveDirectory
$username = Read-Host -Prompt ("Please Enter the Username")
$startdate = Read-Host -Prompt ("Please Enter the user's Start Date (Format: Feb 2022 = 022022)")
基本上,我想創建另一個名為 $UserID 的變數,它將連接 Active Directory 用戶的給定名稱的第一個字母 Active Directory 用戶的姓氏的第一個字母,但小寫 $startdate。例如,如果我在 AD 中輸入 jdoe 作為 John Doe 的用戶名,并且 startdate 是 022022,那么 UserID 應該是 Jd022022。
有沒有辦法在 PowerShell 中連接它?
uj5u.com熱心網友回復:
您可以使用.tolower()和.Substring()方法來執行此操作。
Import-Module -Name ActiveDirectory
$username = Read-Host -Prompt ('Please Enter the Username')
$startdate = Read-Host -Prompt ("Please Enter the user's Start Date (Format: Feb 2022 = 022022)")
$user = Get-ADUser -Identity $username
$finalvariable = $user.GivenName.Substring(0,1) $user.Surname.Substring(0,1).tolower() $startdate
$finalvariable
您可以將其用作具有如下引數的可重用函式。
昵稱
function Generate-NewID {
<#
.SYNOPSIS
Nicknamer
.DESCRIPTION
Provides nickname given username and date.
.PARAMETER user
The samAccountName of the User.
The specified date.
.PARAMETER date
The specified date.
.EXAMPLE
Generate-NewID -user mjones -date 010122
> Mj010122
.NOTES
Place additional notes here.
.OUTPUTS
Nickname in the correct format.
#>
param
(
[Parameter(Mandatory = $true,
ValueFromPipeline = $true,
HelpMessage = 'Please Enter the Username')]
[Object]$user,
[Parameter(Mandatory = $true,
ValueFromPipeline = $true,
HelpMessage = "Please Enter the user's Start Date (Format: Feb 2022 = 022022)")]
[Object]$date
)
Import-Module -Name ActiveDirectory
try {
## test if AD user exists
$null = Get-ADUser -Identity $user
}
catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] {
##can't find that user
Write-Warning -Message 'There was an error finding that user.'
}
catch {
## a different issue
## if you are seeing this you need to remove the
## "$null =" in the above try{} block
Write-Warning -Message 'Other issues...'
}
finally {
$inputuser = Get-ADUser -Identity $user
$startdate = $date
$output = $inputuser.GivenName.Substring(0, 1) $inputuser.Surname.Substring(0, 1).tolower() $startdate
$inputuser = $null
$user = $null
}
$output
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/492576.html
下一篇:僅從提及關鍵字的字串中提取數字
