在文本文件中查找随机文本,然后在显示后将其删除 - PowerShell
Finding random text in text file then removing that after showing it - PowerShell
我正在尝试制作一个自动脚本,其中 powershell 脚本从文本文件中选择一个名称,然后删除它选择的名称,以便它在发送给用户时不会重复。我承认我在 PowerShell 方面没有太多经验,目前只有一行代码,但其余代码将围绕它展开,我似乎无法在堆栈或 google 的任何地方找到它得到具体答案。这是我目前所拥有的:
Get-Random -InputObject (Get-Content "F:\PowerShell\Name Library.txt")
有没有办法让它暂时保存名字几秒钟,然后从列表中删除这个名字,然后删除保存的名字,以便第二天它可以从列表中选择新的名字?
我建议采用以下方法:
- 将文件读入内存
- 选择随机线
- 发送电子邮件
- 用剩余行列表覆盖文件
可能看起来像:
# Read in the list:
$ListOfNames = Get-Content "F:\PowerShell\Name Library.txt"
# Split into two lists, one with a random line, one with the rest
$Random,$Rest = $ListOfNames.Where({$_.ReadCount -eq ($ListOfNames.ReadCount |Get-Random)},'Split')
# Send mail to $Random here
# ...
# Write remaining names back to file
$Rest |Set-Content "F:\PowerShell\Name Library.txt" -Force
补充一下,我从来不喜欢永久删除任何东西。所以如果我正在处理你正在做的事情,我会创建一个对象并将它存储在一个文件中
#You will only have to do this top section once, after that you will import the xml file created below to import the object
#region Initial Import
$ListOfNames = Get-Content C:\TEMP\test.txt
$nameTracking = @()
foreach($name in $ListOfNames)
{
$trackingObj = New-Object -TypeName psobject
$trackingObj | Add-Member -MemberType NoteProperty -Name Name -Value $name
$trackingObj | Add-Member -MemberType NoteProperty -Name EmailSent -Value $false
$trackingObj | Add-Member -MemberType NoteProperty -Name DateSent -Value $null
$nameTracking += $trackingObj
}
#endregion
#After the xml file is created the first time you will execute the following to import the names:
#$nameTracking = Import-Clixml -Path C:\temp\trackingSet.xml
$Random = $nameTracking | where {$_.EmailSent -eq $false} | Get-Random
# Send mail to $Random here, remember to access the name you'll have to use the property of $Random.Name
Send-MailMessage <your parameters here>
#Now set to the EmailSent/DateSent on the object
($nameTracking | where {$_.name -eq $Random.Name}).EmailSent = $true
($nameTracking | where {$_.name -eq $Random.Name}).DateSent = Get-Date
$nameTracking | Export-Clixml -Path C:\temp\trackingSet.xml
这样做的一个好处是,您始终可以在屏幕上打印漂亮的 table 对象内容。只需在导入 XML 后输入 $nameTracking,您就会得到一个 table,其中包含每个用户的详细信息,如果他们收到了电子邮件以及发送日期。
您可以通过添加更多成员来进一步改进此对象以包含您可能需要的信息,您甚至可以存储包含人员姓名的电子邮件地址,以便更轻松地发送电子邮件。
我正在尝试制作一个自动脚本,其中 powershell 脚本从文本文件中选择一个名称,然后删除它选择的名称,以便它在发送给用户时不会重复。我承认我在 PowerShell 方面没有太多经验,目前只有一行代码,但其余代码将围绕它展开,我似乎无法在堆栈或 google 的任何地方找到它得到具体答案。这是我目前所拥有的:
Get-Random -InputObject (Get-Content "F:\PowerShell\Name Library.txt")
有没有办法让它暂时保存名字几秒钟,然后从列表中删除这个名字,然后删除保存的名字,以便第二天它可以从列表中选择新的名字?
我建议采用以下方法:
- 将文件读入内存
- 选择随机线
- 发送电子邮件
- 用剩余行列表覆盖文件
可能看起来像:
# Read in the list:
$ListOfNames = Get-Content "F:\PowerShell\Name Library.txt"
# Split into two lists, one with a random line, one with the rest
$Random,$Rest = $ListOfNames.Where({$_.ReadCount -eq ($ListOfNames.ReadCount |Get-Random)},'Split')
# Send mail to $Random here
# ...
# Write remaining names back to file
$Rest |Set-Content "F:\PowerShell\Name Library.txt" -Force
补充一下,我从来不喜欢永久删除任何东西。所以如果我正在处理你正在做的事情,我会创建一个对象并将它存储在一个文件中
#You will only have to do this top section once, after that you will import the xml file created below to import the object
#region Initial Import
$ListOfNames = Get-Content C:\TEMP\test.txt
$nameTracking = @()
foreach($name in $ListOfNames)
{
$trackingObj = New-Object -TypeName psobject
$trackingObj | Add-Member -MemberType NoteProperty -Name Name -Value $name
$trackingObj | Add-Member -MemberType NoteProperty -Name EmailSent -Value $false
$trackingObj | Add-Member -MemberType NoteProperty -Name DateSent -Value $null
$nameTracking += $trackingObj
}
#endregion
#After the xml file is created the first time you will execute the following to import the names:
#$nameTracking = Import-Clixml -Path C:\temp\trackingSet.xml
$Random = $nameTracking | where {$_.EmailSent -eq $false} | Get-Random
# Send mail to $Random here, remember to access the name you'll have to use the property of $Random.Name
Send-MailMessage <your parameters here>
#Now set to the EmailSent/DateSent on the object
($nameTracking | where {$_.name -eq $Random.Name}).EmailSent = $true
($nameTracking | where {$_.name -eq $Random.Name}).DateSent = Get-Date
$nameTracking | Export-Clixml -Path C:\temp\trackingSet.xml
这样做的一个好处是,您始终可以在屏幕上打印漂亮的 table 对象内容。只需在导入 XML 后输入 $nameTracking,您就会得到一个 table,其中包含每个用户的详细信息,如果他们收到了电子邮件以及发送日期。
您可以通过添加更多成员来进一步改进此对象以包含您可能需要的信息,您甚至可以存储包含人员姓名的电子邮件地址,以便更轻松地发送电子邮件。