簡體   English   中英

從CSV文件中刪除多個文件或文件夾,這些文件或文件夾包含多列(Powershell)

[英]Delete multiple files or folders from a CSV file that contain more than one columns (Powershell)

我需要有關腳本的幫助,該腳本將刪除服務器(DC)上的AD禁用用戶的主文件夾和漫游配置文件文件夾。 我已經完成的步驟,我創建了一個powershell命令:

Import-Module ActiveDirectory
Get-ADUser -SearchBase "OU=Marked for Deletion,OU=Disable Users,DC=******,DC=com" -Filter * -Property * |
Select-Object -Property homeDirectory,profilePath | Export-CSV -Path .\Remove.csv

此命令導出禁用用戶的主文件夾和漫游配置文件文件夾的屬性。 現在,CSV文件包含兩個逗號,一個是“ homeDirectory”,第二個是“ profilePath”

例

問題是,當我執行此腳本時,出現錯誤。

$folders = Get-Content "C:\lab\remove.csv"

foreach ($homeDirectory in $folders) {
    Remove-Item -Path $homeDirectory -force -Recurse
    }
foreach ($profilePath in $folders) {
    Remove-Item -Path $profilePath -force -Recurse
    }
write-host -foregroundcolor yellow "Delete action complete"

有人可以幫我這個忙,我將不勝感激。

首先,我將像這樣從您的CSV中刪除類型信息:

Import-Module ActiveDirectory
Get-ADUser -SearchBase "OU=Marked for Deletion,OU=Disable Users,DC=******,DC=com" -Filter * -Property * |
Select-Object -Property homeDirectory,profilePath | 
Export-CSV -Path .\Remove.csv -NoTypeInformation

然后,對於您的刪除代碼,我將使用以下代碼:

Import-Csv "C:\lab\remove.csv" | % {
  Remove-Item -Path $_.homeDirectory -force -Recurse
  Remove-Item -Path $_.profilePath -force -Recurse
}

write-host -foregroundcolor yellow "Delete action complete"

代碼的問題在於,您沒有在列中循環,而是按行循環,然后執行兩次。 為此,您需要在逗號處分割行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM