0
PowerShell을 사용하여 파일에서 텍스트를 추가하거나 제거합니다. 나는 내 파일에서 재미있는 텍스트를 계속 얻고있다.PowerShell을 사용하여 텍스트 파일에서 텍스트 추가 또는 제거
이것은 텍스트 파일에서 줄을 제거했을 때만 발생하며 새 줄을 추가하려고하면 재미있는 텍스트가 나타납니다.
이
‣ 潍 慨 浭 摡 䴠 橡 摩 义 䱃 아래 참조 :
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="";
}
내가 파일에서 텍스트의 줄을 제거하고 새 사용자를 추가
, 그것은 모든 재미 텍스트 문자열입니다䑕 ⁅ 䅍 䡔 䅃/单 剅 ㅭ 㘳 㐰 012다음 정보가 포함 된 텍스트 파일이 있습니다.
- // 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이 제거하고 광고 할 공간이없는 경우 공간 만 확보하십시오.
내가 텍스트 파일에서 줄을 제거 할 수있는 방법 그들입니다. 예를 들어 그들이 두 줄이라면 나는 제거하지 않을 것이지만, 텍스트 사이에 하나의 빈 줄이 있다면 나는 그것을 놓고 버릴 생각입니다. – hello
@hello - 질문을 완전히 이해할 수 있을지 확신하지 못합니다. 라인 컨텐츠를 서로 비교하거나 정규 표현식을 사용하여 특정 컨텐츠를 찾고이를 기반으로 삭제/제거를 수행 할 수 있어야합니다. 당신이 여기서 묻고있는 것을 보여줄 수 있습니까? – ProfessionalAmateur
상단에 더 많은 정보를 추가했습니다. – hello