This blog has moved to http://ThePowerShellGuy.com
Greetings /\/\o\/\/
I needed to clean some Orphan Shares on our servers,
but when I did lookup the MSH script, I make last year I found that the stats did not work anymore in PowerShell, also it had a boolean parameter, since we have a Switch argument now in PowerShell that would be better,
So I Updated the
MSH Orphan share remover Tool To PowerShell,
I changed the $doChange Bool to a [Switch], (new in powerShell RC1
see PowerShell AD site Finder) also the -eq behavour when checking an array changed,
so I needed to change the to -contains (for the example I did leave them in but I switched also to keeping count in the script)
The Update script looks like this :
# OrphanShares.PS1
# Checks / Removes Orphan Shares
# /\/\o\/\/ 2005
# V2 updated to PowerShell RC1
param (
[string] $Computer = ".",
[Switch] $Remove
)
# Connect to registry
$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $Computer)
$key = $Reg.OpenSubKey("SYSTEM\CurrentControlSet\Services\LanmanServer\Shares", $True)
# get shares in Registry :
$Regshares = $Key.GetValueNames()
# get Existing shares :
$shares = @();get-WMIObject -computer $Computer win32_share | foreach {$shares += $_.name}
# Compare the Lists :
$Exist = 0
$Orphan = 0
$deleted = 0
$RegShares | foreach {
if ($shares -eq $_) {
"$_ Exists"
$Exists += 1
}Else {
"$_ is an Orphan"
$Orphan += 1
# Only if DoChange is True Delete The Orphan Share
if ($Remove.IsPresent) {$Key.DeleteValue($_);"$_ Deleted !!";$deleted +=1}
}
}
# show some statistics :
"Existing : $Exists"
"Orphan : $Orphan"
"Deleted : $Deleted"
# changed -eq to -contains but not used anymore
# "Orphan : ";($RegShares | foreach {"$_ $($shares -contains $_)"} | findstr "False" | measure-object).count
# "Existing : ";($RegShares | foreach {"$_ $($shares -contains $_)"} | findstr "True" | measure-object).count
For more information about Orphan shares and the script see the old post,
Enjoy,
Greetings /\/\o\/\/
Tags :
Monad msh PowerShell