Login | |
|
 |
Help Please Removing Orphan SHares My script wont Work - 9/4/2007 10:41:39 PM
|
|
 |
|
| |
haybaby212
Posts: 2
Score: 0
Joined: 7/18/2005
Status: offline
|
Hi guys What im looking for is a script to remove orphaned Shares i.e User folder has been deleted from the server but the share still exsists in the registry so still publishs the share is live when its not. I have done this script but it does not work. I need to be able to scan reg then scan folders if folder exists do nothing and if no folder remove share from reg. Any ideas This is what i have so far Const wbemFlagReturnImmediately = &h10 Const wbemFlagForwardOnly = &h20 Const HKEY_LOCAL_MACHINE = &H80000002 Const REG_SZ = 1 Const REG_EXPAND_SZ = 2 Const REG_BINARY = 3 Const REG_DWORD = 4 Const REG_MULTI_SZ = 7 strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") Set colItems = objWMIService.ExecQuery("SELECT Name FROM Win32_ComputerSystem", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly) For Each objItem In colItems strComputer = objItem.Name WScript.Echo WScript.Echo "==========================================" WScript.Echo "Computer: " & strComputer WScript.Echo "==========================================" 'If Not(Mid(strComputer, 7, 3) = "PDT" Or Mid(strComputer, 7, 3) = "SDT" Or Mid(strComputer, 7, 3) = "TDT" Or Mid(strComputer, 7, 3) = "DAT") Then ' WScript.Echo "Script is only intended to work on PDT/SDT/TDT/DAT named servers. The script will now quit." 'Call CreateEvent (999, 1, "RemoveShare_Bulk.vbs" , "Script is only intended to work on SDT/TDT/DAT named servers. The script quit without executing.") ' WScript.Quit ' End If Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") Set objFSO = CreateObject("Scripting.FileSystemObject") Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv") strKeyPath = "SYSTEM\CurrentControlSet\Services\LanmanServer\Shares" oReg.EnumValues HKEY_LOCAL_MACHINE, strKeyPath, arrValueNames, arrValueTypes For i=0 To UBound(arrValueNames) 'WScript.Echo "--------------------------------------------------------" 'WScript.Echo "Value Name: " & arrValueNames(i) Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Share ") 'For Each objItem In colItems 'WScript.Echo "Name: " & objItem.Name 'WScript.Echo "Description: " & objItem.Description 'WScript.Echo "Path: " & objItem.Path 'WScript.Echo "Status: " & objItem.Status 'WScript.Echo "Type: " & objItem.Type strValueName = arrValueNames(i) oReg.GetMultiStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, arrValues For Each strValue In arrValues If Left(strValue, 5) = "Path=" Then WScript.Echo strValue WScript.Echo InStr(1, Mid(strValue, 6) , "\Usercfg\") If InStr(1, Mid(strValue, 6) , "\Usercfg\", vbTextCompare) <> 0 Then ' Config Directory WScript.Echo "CFG" strPath = "\\" & strComputer & "\Usercfg\" & Left(arrValueNames(i) , Len(arrValueNames(i)) - 3) Elseif InStr(1, Mid(strValue, 6), "\Users\", vbTextCompare) <> 0 Then ' User share strPath = "\\" & strComputer & "\Users\" & arrValueNames(i) Elseif InStr(1, Mid(strValue, 6), "\TEMPLA\", vbTextCompare) <> 0 Then ' User share strPath = "\\" & strComputer & "\TEMPLA\" & Mid(arrValueNames(i), 7) Else strPath = "" End If If strPath <> "" Then WScript.Echo strPath If objFSO.FolderExists(strPath) Then Wscript.Echo "Folder exists - share is valid!" Else WScript.Echo strValue WScript.Echo "Value Name: " & arrValueNames(i) If Mid(strComputer, 7, 3) = "PDT" Then Wscript.Echo "Folder does not exist. SKIPPING DELETE as this is a PRIMARY server!!!!!" Else Wscript.Echo "Folder does not exist. Deleting share from registry" oReg.DeleteValue HKEY_LOCAL_MACHINE, strKeyPath, arrValueNames(i) End If End If End If End If Next Next Next
|
|
| |
|
|
|
 |
RE: Help Please Removing Orphan SHares My script wont Work - 9/4/2007 10:57:40 PM
|
|
 |
|
| |
mbouchard
Posts: 1924
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
|
What in the script doesn't work? Also to make the code easier to read, use the code tags, especially if you indent your code.
_____________________________
Mike For useful Scripting links see the Read Me First stickey! Always remember Search is your friend.
|
|
| |
|
|
|
 |
RE: Help Please Removing Orphan SHares My script wont Work - 9/4/2007 10:59:42 PM
|
|
 |
|
| |
haybaby212
Posts: 2
Score: 0
Joined: 7/18/2005
Status: offline
|
Not to sure it seems to list out the file shares that are there ok but does not remove the ones where the folders dont exsist Const wbemFlagReturnImmediately = &h10 Const wbemFlagForwardOnly = &h20 Const HKEY_LOCAL_MACHINE = &H80000002 Const REG_SZ = 1 Const REG_EXPAND_SZ = 2 Const REG_BINARY = 3 Const REG_DWORD = 4 Const REG_MULTI_SZ = 7 strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") Set colItems = objWMIService.ExecQuery("SELECT Name FROM Win32_ComputerSystem", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly) For Each objItem In colItems strComputer = objItem.Name WScript.Echo WScript.Echo "==========================================" WScript.Echo "Computer: " & strComputer WScript.Echo "==========================================" 'If Not(Mid(strComputer, 7, 3) = "PDT" Or Mid(strComputer, 7, 3) = "SDT" Or Mid(strComputer, 7, 3) = "TDT" Or Mid(strComputer, 7, 3) = "DAT") Then ' WScript.Echo "Script is only intended to work on PDT/SDT/TDT/DAT named servers. The script will now quit." 'Call CreateEvent (999, 1, "RemoveShare_Bulk.vbs" , "Script is only intended to work on SDT/TDT/DAT named servers. The script quit without executing.") ' WScript.Quit ' End If Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") Set objFSO = CreateObject("Scripting.FileSystemObject") Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv") strKeyPath = "SYSTEM\CurrentControlSet\Services\LanmanServer\Shares" oReg.EnumValues HKEY_LOCAL_MACHINE, strKeyPath, arrValueNames, arrValueTypes For i=0 To UBound(arrValueNames) 'WScript.Echo "--------------------------------------------------------" 'WScript.Echo "Value Name: " & arrValueNames(i) 'Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Share ") 'For Each objItem In colItems 'WScript.Echo "Name: " & objItem.Name 'WScript.Echo "Description: " & objItem.Description 'WScript.Echo "Path: " & objItem.Path 'WScript.Echo "Status: " & objItem.Status 'WScript.Echo "Type: " & objItem.Type strValueName = arrValueNames(i) oReg.GetMultiStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, arrValues For Each strValue In arrValues If Left(strValue, 5) = "Path=" Then WScript.Echo strValue WScript.Echo InStr(1, Mid(strValue, 6) , "\Usercfg\") If InStr(1, Mid(strValue, 6) , "\Usercfg\", vbTextCompare) <> 0 Then 'Config Directory WScript.Echo "CFG" strPath = "\\" & strComputer & "\Usercfg\" & Left(arrValueNames(i) , Len(arrValueNames(i)) - 3) Elseif InStr(1, Mid(strValue, 6), "\Users\", vbTextCompare) <> 0 Then ' User share strPath = "\\" & strComputer & "\Users\" & arrValueNames(i) Elseif InStr(1, Mid(strValue, 6), "\TEMPLA\", vbTextCompare) <> 0 Then ' User share strPath = "\\" & strComputer & "\TEMPLA\" & Mid(arrValueNames(i), 7) Else strPath = "" End If If strPath <> "" Then WScript.Echo strPath If objFSO.FolderExists(strPath) Then Wscript.Echo "Folder exists - share is valid!" Else WScript.Echo strValue WScript.Echo "Value Name: " & arrValueNames(i) If Mid(strComputer, 7, 3) = "PDT" Then Wscript.Echo "Folder does not exist. SKIPPING DELETE as this is a PRIMARY server!!!!!" Else Wscript.Echo "Folder does not exist. Deleting share from registry" oReg.DeleteValue HKEY_LOCAL_MACHINE, strKeyPath, arrValueNames(i) End If End If End If End If Next Next Next
|
|
| |
|
|
|
| |
|
|
 |
|
 |
|
|