Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Delete network shares listed in a text file- Multiprocessing and not sequentially

$
0
0

1..5 | %{md c:\temp$_ -ErrorAction SilentlyContinue} | Out-Null

$shares = dir c:\temp*

set-content -path C:\shares.txt -value $shares

 

The above script creates folders

c:\temp1

c:\temp2

c:\temp3

c:\temp4

c:\temp5

 

The below script works fine if we have the local folders in the shares.txt (as above) but it is not working while i put the list of network shares in it (as in \\servername\sharename)

 

 

$shareslist = gc c:\shares.txt

$resultoutput = “C:\result.txt”

$Errorservers = “C:\failedservers.txt”

$i=1

foreach($share in $shareslist){

Write-host "Deleting share no: $i and share name $share"

start-job -name test {

param ($share,$result,$Errorservers,$resultoutput)

        try 

        {

        remove-item -path $share  -force -r -ErrorAction stop  

        $result=$true 

        Write-host "Deleted successfully"

        }

        catch 

        {

        $share + ” , ” + $_.Exception.message | Out-file $Errorservers -append

        $result=$false

        Write-host "Deletion failed as no share exists "

 

         } 

        [pscustomobject]@{

        Share = $share

        Found = $result

        } | out-file $resultoutput -Append

    } -Argumentlist $share,$result,$Errorservers,$resultoutput 

 

 

$i++

}


Viewing all articles
Browse latest Browse all 6937

Trending Articles