简体   繁体   中英

Get data from CSV to delete users folders from multiple servers

I am very new to powershell and situation is that I have some unneeded users folder that must be deleted on different servers. Path on all servers is fe "\\server1\\hiddenshare$\\username" My CSV looks like:

ServerName,FolderName
SERVER1,AAAA
SERVER2,AAA1
SERVER3,AAA2

And I am trying to run this code:

$path = "C:\temp\servers_folders.csv"
$ServerName = Import-Csv -Path $path
$FolderName = Import-CSV -Path $path
Import-Csv -Path $path | % {
  Remove-Item -Path \\$ServerName\hiddenshare$\$FolderName -Recurse
    }

After all I get this error:

Remove-Item : Cannot find path '\\  \$\  ' because it does not exist.
At C:\temp\servers_folders.csv.ps1:5 char:3
+   Remove-Item -Path \\$ServerName\hiddenshare$\$FolderName -Recurse
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (\\  \usr$\  :String) [Remove-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand

you dont need to import your csv more than once. Inside the foreach block you have to reference the current item by $_

try this, remove -whatif if it's ok

$servers=import-csv .\servers.csv

$servers | %{
    remove-item -path \\$($_.servername)\users$\$($_.FolderName) -whatif
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM