2011-11-16 58 views
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=""; 
} 

當我刪除文本從文件中的行,並添加新的用戶,這都是一個有趣的文本字符串:

見下

‣濰慨浭摡䴠橡摩਍義䱃30⁅䅍䡔䅃/單剅洠ㅭ㘳㐰ല

我有一個包含以下信息的文本文件。

- // 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有額外的空間,我不會刪除和廣告只是sinlge空間。

回答

0

我認爲你需要設置編碼爲您Add-Content

嘗試:Add-Content -Encoding UTF8 -Path $fileLocation -Value "# $userFullName";

+0

是他們的任何方式,我可以刪除文本文件行。例如,如果他們是兩條線我不會刪除他們,但如果他們是文本之間的單一空白行,那麼我不會離開它。 – hello

+0

@hello - 我不確定我完全理解這個問題。您應該能夠將線內容相互比較,或使用正則表達式來查找特定內容並根據它執行刪除/刪除。你能提供一個你在這裏問的例子嗎? – ProfessionalAmateur

+0

我在頂部添加了更多信息。 – hello