/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" PowerShell and Active Directory Part 8 (ACL's) "," Signing PowerShell Scripts "," PowerShell and Active Directory Part 7 "," PowerShell and Active Directory Part 6 "," Windows PowerShell Video: Next Generation Command ... "," PowerShell and Active Directory Part 5 "," PowerShell and Active Directory Part 4 (TypeData) "," PowerShell and MOM2005 part 2 : Updating Settings "," PowerShell Boolean FileMode "," Working with Fixed Length delimited Text files in ... "," ")

Monday, August 14, 2006

 


PowerShell Orphan share remover Tool (Update)



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 :


Comments: Post a Comment



<< Home

Archives

October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006  

$Links = ("PowerShell RC1 Docs"," PowerShell RC1 X86"," PowerShell RC1 X64"," Monad GettingStarted guide"," Monad Progamming Guide"," Monad SDK"," Monad videos on Channel 9"," MSH Community Workspace"," scripts.readify.net "," MonadSource"," www.reskit.net"," PowerShell Blog"," Under The Stairs"," computerperformance powershell Home"," proudlyserving"," MSH on wikipedia"," MSHWiki Channel 9"," Keith Hill's Blog"," Precision Computing"," PowerShell for fun"," MSH Memo (Japanese)"," monadblog")

find-blog -about "PowerShell","Monad" | out-Technorati.
find-blog -contains "","" | out-Technorati.
Google
 
Web mow001.blogspot.com

This page is powered by Blogger. Isn't yours?