Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Remote Removal Of Windows Shares

$
0
0

Hi,

I am trying to automate a migration process involving thousands of user shares. In the past they were all entered as \\server\username$ & I am trying to move to DFS with just a few shares per drive. Once I can get this script to work, I will use Import-Csv to pass the data in chunks, the problem is getting this to work.

cls
Write-Host "********************************************************************"
Write-Host "**                       User Admin Tool                      **"
Write-Host "********************************************************************"
$ServerName = (Read-Host "Please Enter The Target Server")
$ShareName = (Read-Host "Please Enter The User Share To Remove")
write-host "Retrieving user objects...." -ForegroundColor Magenta
start-sleep -s 1
#([wmi]'$ServerName\root\cimv2:Win32_Share="$ShareName"').Delete()
Get-WmiObject Win32_Share -Filter "Name='$ServerName\$ShareName'" | Remove-WmiObject
write-host "Saving user objects...." -ForegroundColor Blue
start-sleep -s 1

The line that is #'d out will work if I replace the variables with real data, but will not resolve the variables. The line below that (Get-WmiObject) will resolve the variables, but gives the following error;

Get-WmiObject : Invalid query "select * from Win32_Share where Name='\\Server\joebloggs$'"
At Z:\PS Scripts\RemoveUserShare.ps1:10 char:1
+ Get-WmiObject Win32_Share -Filter "Name='$ServerName\$ShareName'" | Remove-WmiOb ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObje 
   ctCommand

Any help will be greatly appreciated, although I am sure it is probably something simple.

Thanks,

Peaca



Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>