我正在創建這樣的用戶名:名字的前 3 個字母,然后是 4 個隨機生成的數字。瑞恩史密斯 = RYA4859。我從這個 PowerShell 命令中獲取亂數:
隨機獲取 - 最小 1000 - 最大 10000
我需要知道如何創建一個腳本,在生成用戶名后將其添加到 .txt 檔案中。我還希望腳本首先檢查 .txt 檔案以查看隨機生成的數字是否已經存在,如果存在,則生成一個新的不存在的 4 位數字,然后將其添加到 .txt 檔案中。
流程應該是:
如果數字存在,則生成隨機 4 位數字檢查 txt 檔案 - 如果不存在,則生成新數字 - 附加檔案并將生成的數字添加到檔案
uj5u.com熱心網友回復:
你想運行一個 do...until 回圈,直到隨機生成的數字不存在于你的文本檔案中
$file = "C:\users.txt"
$userId = "RYA"
# get the contents of your text file
$existingUserList = Get-Content $file
do
{
$userNumber = Get-Random -Minimum 1000 -Maximum 10000
# remove all alpha characters in the file, so only an array of numbers remains
$userListReplaced = $existingUserList -replace "[^0-9]" , ''
# the loop runs until the randomly generated number is not in the array of numbers
} until (-not ($userNumber -in $userListReplaced))
# concatenates your user name with the random number
$user = $userId $userNumber
# appends the concatenated username into the text file
$user | Out-File -FilePath $file -Append
沒有 3 個字符的前綴
$file = "C:\users.txt"
# get the contents of your text file
$existingUserList = Get-Content $file
do
{
$userNumber = Get-Random -Minimum 1000 -Maximum 10000
# remove all alpha characters in the file, so only an array of numbers remains
$userListReplaced = $existingUserList -replace "[^0-9]" , ''
# the loop runs until the randomly generated number is not in the array of numbers
} until (-not ($userNumber -in $userListReplaced))
# appends the concatenated username into the text file
$userNumber| Out-File -FilePath $file -Append
uj5u.com熱心網友回復:
注意:與在未排序的陣列中查找匹配元素相比,哈希表通常會在更短的時間內找到鍵。這種性能差異隨著元素數量的增加而增加。雖然對已排序陣列的二分搜索在性能上可能會更接近,但排序程序本身可能會對性能造成重大影響,并增加代碼的復雜性。
問題評論中描述的代碼版本與以下代碼之間的主要區別在于,我將新用戶名附加到檔案而不是覆寫檔案,并在末尾添加了一個回圈反復詢問代碼是否應該繼續。
function RandomDigits {
[CmdletBinding()]
param (
[Parameter()]
[int]$DigitCount = 2
)
$RandString = [string](Get-Random -Minimum 100000 -Maximum 10000000)
$RandString.Substring($RandString.Length-$DigitCount)
}
function GenUserName {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0)]
[string]$Prefix
)
"$Prefix$(RandomDigits 4)"
}
function ReadAndMatchRegex {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0)]
[string]$Regex,
[Parameter(Mandatory = $true, Position = 1)]
[string]$Prompt,
[Parameter(Mandatory = $false, Position = 2)]
[string]$ErrMsg = "Incorrect, please enter needed info (Type 'exit' to exit)."
)
$FirstPass = $true
do {
if (-not $FirstPass) {
Write-Host $ErrMsg -ForegroundColor Red
Write-Host
}
$ReadText = Read-Host -Prompt $Prompt
$ReadText = $ReadText.ToUpper()
if($ReadText -eq 'exit') {exit}
$FirstPass = $false
} until ($ReadText -match $Regex)
$ReadText
}
$Usernames = @{}
$UsernameFile = "$PSScriptRoot\Usernames.txt"
if(Test-Path -Path $UsernameFile -PathType Leaf) {
foreach($line in Get-Content $UsernameFile) { $Usernames[$Line]=$true }
}
do {
Write-Host
$UserPrefix = ReadAndMatchRegex '^[A-Z]{3}$' "Please enter 3 letters for user's ID"
do {
$NewUserName = GenUserName $UserPrefix
} while ($Usernames.ContainsKey($NewUserName))
$NewUserName | Out-File $UsernameFile -Append
$UserNames[$NewUserName]=$true
$UserNames.Keys
$Continue = ReadAndMatchRegex '^(Y|y|YES|yes|Yes|N|n|NO|no|No)$' 'Continue?[Y/N]'
} while ($Continue -match '^(Y|y|YES|yes|Yes)$')
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/466223.html
