[英]Can I remove a line of a text file after a foreach ($line) in Powershell
I have a Powershell script for our Exchange online that updates our archiving and litigation settings for mailboxes (see the relevant part below).我有一个用于我们的 Exchange Online 的 Powershell 脚本,用于更新我们的邮箱归档和诉讼设置(请参阅下面的相关部分)。
Each line within the text file is a user's mailbox文本文件中的每一行都是一个用户的邮箱
$mailboxes = Get-Content "\\SERVER\c$\Scripts\Enable archiving and litigation\new_mailboxes.txt" #Read text file with applicable users
foreach ($line in $mailboxes) {
Write-host "Enable Archiving for $line"
get-mailbox $line | enable-mailbox -archive
Write-host "Enable Litigation for $line"
get-mailbox $line | set-mailbox -litigationHoldEnabled $true -litigationholdduration 2555
Write-host "Enable Quota for $line"
get-mailbox $line | set-mailbox -prohibitsendquota 4.5GB -prohibitsendreceivequota 5.0GB -issuewarningquota 4GB
Write-host "Enable Quota for $line"
get-mailbox $line | set-mailbox -retentionpolicy "ORG Retention Policy"
Write-host "Enable retention policy for $line"
get-mailbox $line | set-mailbox -RetainDeletedItemsFor 30.00:00:00
Write-Host "Removing the line $line"
$mailboxes | Where-Object {$_ -notmatch $line} | Set-Content "\\SERVER\c$\Scripts\Enable archiving and litigation\new_mailboxes.txt"
}
The issue I'm running into is that the file will still save with a few lines intact, for example the text file will contain:我遇到的问题是该文件仍将保存几行完整的,例如文本文件将包含:
Ross, Bob
Bloggs, Joe
Once the script is run the text file will then be saved with content like:脚本运行后,文本文件将保存如下内容:
Bloggs, Joe
Have you tried using Add-Content
instead of Set-Content
?您是否尝试过使用
Add-Content
而不是Set-Content
?
In your case at each step in the loop it overwrites the file.在您的情况下,在循环的每个步骤中,它都会覆盖文件。
If you want to reduce your original file you have to do somthing like:如果要减少原始文件,则必须执行以下操作:
copy-item "\\SERVER\c$\Scripts\Enable archiving and litigation\new_mailboxes.txt" "\\SERVER\c$\Scripts\Enable archiving and litigation\new_mailboxes_tmp.txt"
$mailboxesTmp = Get-Content "\\SERVER\c$\Scripts\Enable archiving and litigation\new_mailboxes_tmp.txt"
$mailboxesTmp | Where-Object {$_ -notmatch $line} | Set-Content "\\SERVER\c$\Scripts\Enable archiving and litigation\new_mailboxes.txt"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.