question

cornenietnodig-5642 avatar image
0 Votes"
cornenietnodig-5642 asked juliaAnn-2756 commented

remove foldershare remotely

We have shared folders which must be copied and deleted regurally. So i made a powershell script and try to do copy and delete the share but i can only copy the complete folderstructure but not delete it and not move it remotely. When all subfolders and files are copied to another server then the rootfolder share is empty but cannot be deleted remotely not in a batchfile with cmd not with powershell and not with robocopy. Process is in use. But when i make a new share end trie it is also not deleted, just the subfolders. When i delete the complete patch remotely then it can be deleted but not the share. \\servername\share what can be deleted is: \\servername\patch to share\share or \\servername\e$\patch Is there somewhere a setting that prevents deleting rootfolder shared folders?

windows-server-powershell
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Do you want to remove the share or the shared folder? If you remove the folder only the share can still be found but not accessible.

0 Votes 0 ·

Same Problem Coupon I was Facing But now it is completely solved


0 Votes 0 ·
RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

You don't have to pipe anything. Put this in the script block (change the share name, of course!):

 $ShareName = "Junk"
 $p = (Get-SmbShare $ShareName).Path
 Remove-SmbShare $ShareName -Force
 Remove-Item -Path $p
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

Have you tried using Invoke-Command to run a scriptblock on the remote machine? You can use (in the scriptblock) Get-SmbShare to see if any users are connected to the share before you remove it with Remove-SmbShare. Once the share is gone you can remove the directory.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cornenietnodig-5642 avatar image
0 Votes"
cornenietnodig-5642 answered RichMatheisen-8856 edited

Hi Rich,

Can you give an example? but the share cannot be in use because it is also when making a new share, brand new en then put some files and folders in it. The files and subfolders are removed but not the share itself.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You can try this:

 Invoke-Command -ComputerName "REMOTE-MACHINE" -ScriptBlock { Remove-SmbShare -Name "SHARE-NAME" -Force}

Note that the SHARE-NAME is the name, not the UNC.

EDIT: Corrected the Invoke-Command to use the correct parameter for the remote machine.

0 Votes 0 ·
cornenietnodig-5642 avatar image
0 Votes"
cornenietnodig-5642 answered

Invoke-Command -Server "REMOTE-MACHINE" -ScriptBlock { Remove-SmbShare -Name "SHARE-NAME" -Force}

Invalid argument. A parametre cannot be found.

I filled Remote-machine with the server name and share-name with the share-name

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cornenietnodig-5642 avatar image
0 Votes"
cornenietnodig-5642 answered RichMatheisen-8856 commented

I can do it like this but not the other way around.


$Local="\\c:\temp\test"
$Remote="\\server\test"

If (Test-Path $Local) {
Move-Item -Path $Local -Destination $Remote -Confirm
Write-Host "Move Successful"
}
Else {
Write-Host "$Remote : can not be found"
}



I think it has something to do with protection on a server share to remotely remote or delete that share.

With the full path it is succeeding.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

My bad. The "-Server" in the example I suggested you try is incorrect. The parameter is -ComputerName

I'll correct my earlier comment, too.

0 Votes 0 ·
cornenietnodig-5642 avatar image
0 Votes"
cornenietnodig-5642 answered cornenietnodig-5642 commented

Invoke-Command -ComputerName "REMOTE-MACHINE" -ScriptBlock { Remove-SmbShare -Name "SHARE-NAME" -Force}

This does indeed remove the share but then i cannot remove the folder remotely anymore because the share does not excists after above command. So when i first move the subfolders and files then remove the smbshare with above command, the how do i delete the headfolder without giving in the whole path?

Let's assume the share is \\server\test and the path is \\server\d$\department\test en has several subfolders and files. The goal is to copy or move all subolders and files to the destination \\server1\share.
I do not know the "department in advance so cannot give the whole path with department in it.

I can empty the share: \\server\test but the testfolder remains, then remove the share test, that leaves the folder test to remove: \\server\d$\department\test

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi RichMatheisen-8856,

An exampe would be nice, i don't now how to put a get-smbshare and pipe the path into a removesmbshare and all of that in a scriptblock..

I have searched the internet but cannot find examples of get-smbshare with the combination of using the path as input for remote-item of removev-smbshare.

Thanks

0 Votes 0 ·
RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

Do all the work in the script block of the Invoke-Command!

Before you remove the share, use Get-SMBShare and get the path value for the share. Then remove the share and, using the path you retrieved before removing the share, use Remove-Item to remove the directory.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cornenietnodig-5642 avatar image
0 Votes"
cornenietnodig-5642 answered

Hi, almost there i think. Your answer was the solution for removing the folder inclusive the subfolders however i would like to make the sharename variable so let Powershell ask me for the name and use that input as the $sharename.
What i have now is not finding the sharename, it copies the folder and subfolders with copy-item but the part the invoke command does not work, Powershell cannot find property 'Name'equal to ''.It leaves it empty.
How can we use the input from:
param ($naam)
if ($naam -eq $null){
$naam = Read-Host -Prompt "Please enter user logon name"
}

Invoke-Command -ComputerName "server" -ScriptBlock { $ShareName = "$naam"
$p = (Get-SmbShare $ShareName).Path
Remove-SmbShare $ShareName -Force
Remove-Item -Path $p -recurse -force
}


and use this input for the invoke-command scriptblock?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

This should allow you to use "$naam" in the script block:

 $naam = Read-Host -Prompt "Please enter user logon name"
    
 Invoke-Command -ComputerName "server" -ScriptBlock {$p = (Get-SmbShare $Using:naam).Path
         Remove-SmbShare $Using:naam -Force
         Remove-Item -Path $p -recurse -force
     }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cornenietnodig-5642 avatar image
0 Votes"
cornenietnodig-5642 answered RichMatheisen-8856 commented

This works indeed!

Almost there. I have 2 shares with 2 different names but on the same server which must be copied and then deleted and unshared. I thought this can be done to ask twice about the sharename (it is maybe not efficient but could work for a beginner...) However when i execute them seperatly they work but when starting it in Powershell-ise it gives a copy-item syntax on the first $naam, syntax filename or volume name is wrong.

But what it does is removing the share and folders but not copy-item!, so this is tricky...


$naam = Read-Host -Prompt "Please enter user logon name"
$Source = "\\server\$naam\"
$Target="\\server\path\Users\$naam\"
Get-ChildItem -Path $Source -Recurse | copy-Item -Destination $Target -Force
Invoke-Command -ComputerName "server" -ScriptBlock {$p = (Get-SmbShare $Using:naam).Path
Remove-SmbShare $Using:naam -Force
Remove-Item -Path $p -recurse -force
}
$naam = Read-Host -Prompt "fill in sharename"
$Source = "\\server\$naam\"
$Target="\\server\otherpath\$naam\"
Get-ChildItem -Path $Source -Recurse | Copy-Item -Destination $Target -Force
Invoke-Command -ComputerName "server" -ScriptBlock {$p = (Get-SmbShare $Using:naam).Path
Remove-SmbShare $Using:naam -Force
Remove-Item -Path $p -recurse -force
}

Can i make something better out of this?

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I didn't think you could pipe the output of Get-ChildItem into Copy-Item because Copy-Item only a string or string array for the "Path" parameter. You can pipe the "FullName" value (which is a string) from Get-ChildItem.

0 Votes 0 ·