Reputation: 29
I am trying to make an automated script where the powershell script picks a name from the text file and then deletes that name it picked so that it doesnt duplicate on sending to users. I admit I have not got a lot of experience on PowerShell and currently only have one line of code but its going to be what the rest of the code revolves around, I cant seem to find it anywhere on stack or google to get the specific answer. Here is what i have so far:
Get-Random -InputObject (Get-Content "F:\PowerShell\Name Library.txt")
Is there a way to make it save the name temporarily for a few seconds then delete the name from the list then remove the saved name so that for the next day it can pick something new from the list?
Upvotes: 0
Views: 220
Reputation: 150
Just to add to the above, I never like removing anything permanently. So if I was working on what you were doing, I'd create an object and store it in a file
#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
A nice thing about this too is you can always print on screen a nice table of the contents of the object. Just type in $nameTracking after importing the XML and you'll get a table with the details of each user, if they've received an email and what date it was sent.
You can further improve this object by adding more members to contain information you may want you can even store the email address with the persons name in it for easier email messaging.
Upvotes: 0
Reputation: 174525
I'd suggest the following approach:
Could look something like:
# 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
Upvotes: 1