使用PowerShell从文本文件中添加或删除文本
我正在使用PowerShell添加或删除文件中的文本。我的文件里一直有有趣的文字 只有当我从文本文件中删除该行,并且当我尝试添加新行时,才会得到有趣的文本使用PowerShell从文本文件中添加或删除文本,powershell,powershell-2.0,Powershell,Powershell 2.0,我正在使用PowerShell添加或删除文件中的文本。我的文件里一直有有趣的文字 只有当我从文本文件中删除该行,并且当我尝试添加新行时,才会得到有趣的文本 cls IMPORT-MODULE ActiveDirectory $fileLocation = "E:\Script\MatchCadTest\ptc.opt"; function addUser( $username=''){ $user = Get-ADUser -Identity $username -ErrorAct
cls
IMPORT-MODULE ActiveDirectory
$fileLocation = "E:\Script\MatchCadTest\ptc.opt";
function addUser( $username=''){
$user = Get-ADUser -Identity $username -ErrorAction SilentlyContinue;
$userFullName = $user.Name;
$empty = [string]::IsNullOrEmpty($userFullName);
if ( !($empty) ){
$userExisted = Get-Content $fileLocation | Select-String "$username" -quiet
if( ! $userExisted ){
$newLocation = "E:\Script\MatchCadTest\backup\";
if((Test-Path -Path $fileLocation)) {
Copy-Item "$fileLocation" "$newLocation"
}
$date = Get-Date -Format "d-M-y Hms";
$newName = "ptc_$date.opt"
Rename-Item "$newLocation\ptc.opt" $newName
# Add-Content $fileLocation ""
Add-Content -Path $fileLocation -Value "# $userFullName";
Add-Content -Path $fileLocation -Value "INCLUDE MATHCAD USER $username";
Write-Host "User has been added to file. Please restart the service." -BackgroundColor Green -ForegroundColor Black
}
else{
Write-Host "User already existed" -BackgroundColor Red -ForegroundColor White
}
}
}
function removeUser( $username=''){
$user = Get-ADUser -Identity $username -ErrorAction SilentlyContinue;
# $user
$userFullName = $user.Name;
$empty = [string]::IsNullOrEmpty($userFullName);
if ( !($empty) ){
$userExisted = Get-Content $fileLocation | Select-String "$username" -quiet
if( $userExisted ){
$remove="# $userFullName";
$removeUser = (Get-Content $fileLocation);
$removeUser | where {$_ -ne $remove};
$remove="INCLUDE MATHCAD USER $username";
$removeUser | where {$_ -ne $remove}
$removeUser |Out-File $fileLocation;
#$removeUser = (Get-Content $fileLocation) | where {$_ -ne $remove} | Out-File $fileLocation;
#$content = Get-Content $fileLocation
#$content | Foreach {$_.TrimEnd()} | Set-Content $fileLocation
Write-Host "User removed" -BackgroundColor Green -ForegroundColor Black
}
else{
Write-Host "User does not existed" -BackgroundColor Red -ForegroundColor White
}
}
else{
Write-Host "User not found in ad" -BackgroundColor Red -ForegroundColor White
}
}
$option=''
while ( $option -ne 0){
Write-Host "What would you like to do?"
Write-Host "1= Add new user"
Write-Host "2= Remove user"
Write-Host "0= No (Exit)"
$option = Read-Host "Select option"
$username = Read-Host "Please enter Username"
if( $option -eq 0 ){
exit 1
}
elseif( $option -eq 1){
addUser($username);
}
elseif ( $option -eq 22){
removeUser ($username);
}
else{
cls
Write-Host
Write-Host " Invaild Choice " -BackgroundColor Red #-ForegroundColor White
Write-Host
}
#Reset
$option=444;
$username="";
$userFullName="";
$user="";
$empty="";
}
当我从文件中删除一行文本并添加新用户时,这都是一个有趣的文本字符串:
见下文
"潍慨浭摡䴠橡摩义䱃䑕⁅䅍䡔䅃⁄单剅洠ㅭ㘳㐰ല
我有一个包含以下信息的文本文件
- // Full name 1
- User ID of User 1
- // Full name 2
- User ID of User 2
* // Full name 3
* User ID of User 3
* // Full name 4
* User ID of User 4
* // Full name 5
* User ID of User 5
* // Full name 6
* User ID of User 6
* // Full name 7
* User ID of User 7
* // Full name 8
* User ID of User 8
如果您看到用户5和6或7和8有额外的空间,我不会删除这些空间,只在空间中添加广告。我认为您需要为添加内容设置编码
尝试:添加内容-编码UTF8-路径$fileLocation-值$userFullName 它们是我可以从文本文件中删除行的任何方法。例如,如果它们是两行,我不想删除它们,但如果它们是文本之间的一行空白,我就不想留下。@hello-我不确定我是否完全理解这个问题。您应该能够相互比较行内容,或者使用正则表达式查找特定内容并基于该内容执行删除/删除。你能举个例子说明你在问什么吗?我在顶部添加了更多的信息。@hello-这应该是直截了当的。创建一个变量,并在每次读取行时将其存储在变量中。您读取的每一行都将其与变量中的行进行比较。然后做一个类似的比较:如果line1==变量,line1=={delete line}或者{do nothing}这只是伪代码,但我想你明白了。