Removing Network Drives with Specific IP Using VBScript (Run as Administrator)
Removing Network Drives with Specific IP Using VBScript (Run as Administrator)
This blog post explains how to create a VBScript that removes network drives containing a specific IP address, and how to run it with administrator privileges.
Introduction
Sometimes, you may need to disconnect network drives that are no longer accessible or connected to a specific server. This VBScript automates the process of finding and removing such drives based on their IP address.
VBScript Code
Option Explicit
' Function to check if the script is running with administrator privileges
Function IsAdmin()
Dim objShell, strUser, strDomain
Set objShell = CreateObject("WScript.Shell")
strUser = objShell.ExpandEnvironmentStrings("%USERNAME%")
strDomain = objShell.ExpandEnvironmentStrings("%USERDOMAIN%")
IsAdmin = (strUser <> "" And strDomain <> "")
Set objShell = Nothing
End Function
' Request administrator privileges if not already running as admin
If Not IsAdmin() Then
CreateObject("Shell.Application").ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """", "", "runas", 1
WScript.Quit
End If
' IP address of the network drives to remove
Const TARGET_IP = "192.168.0.100" ' Replace with the actual IP address
' Function to get the output of the 'net use' command
Function GetNetUseResult()
Dim objShell, objExec, strResult
Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec("%comspec% /c net use")
strResult = objExec.StdOut.ReadAll()
GetNetUseResult = strResult
Set objExec = Nothing
Set objShell = Nothing
End Function
' Subroutine to find and remove network drives with the specified IP
Sub RemoveDrivesWithIP()
Dim strNetUseResult, arrLines, strLine, strDrive, strRemote
strNetUseResult = GetNetUseResult()
arrLines = Split(strNetUseResult, vbCrLf)
For Each strLine In arrLines
If InStr(strLine, TARGET_IP) > 0 Then
strDrive = Trim(Left(strLine, 12)) ' Extract drive letter (max 12 characters)
strRemote = Trim(Mid(strLine, 13, 40)) ' Extract remote path (from 13th character, max 40 characters)
If strDrive <> "" Then
CreateObject("WScript.Shell").Run "%comspec% /c net use " & strDrive & " /delete /y", 0, True
WScript.Echo strDrive & " drive (" & strRemote & ") removed."
End If
End If
Next
WScript.Echo "Operation completed."
End Sub
' Call the subroutine to remove the drives
RemoveDrivesWithIP()
Explanation
* Administrative Privileges:
* The IsAdmin() function checks if the script is running with administrator privileges.
* If not, it uses Shell.Application.ShellExecute to relaunch the script with administrator rights.
* Target IP Address:
* The TARGET_IP constant stores the IP address of the drives to be removed.
* Important: Replace "192.168.0.100" with the actual IP address.
* net use Output:
* The GetNetUseResult() function executes the net use command and returns its output.
* Drive Removal:
* The RemoveDrivesWithIP() subroutine processes the net use output.
* It iterates through each line and checks if it contains the TARGET_IP.
* If found, it extracts the drive letter and remote path using Left and Mid functions, accounting for the fixed-width columns in the net use output.
* The net use drive_letter /delete /y command is used to disconnect the drive.
* Confirmation messages are displayed.
How to Use
* Save the Script:
* Copy the code into a text editor like Notepad.
* Save the file with a .vbs extension (e.g., remove_network_drives.vbs).
* Modify the IP Address:
* Change the value of the TARGET_IP constant to the IP address you want to target.
* Run as Administrator:
* Right-click the saved .vbs file and select "Run as administrator."
* If prompted by User Account Control (UAC), click "Yes."
Important Notes
* This script will remove all network drives containing the specified IP address.
* Double-check the IP address before running the script to avoid accidentally disconnecting important drives.
* Disconnecting network drives may result in data loss if files are currently in use. Back up important data beforehand.
* The script now accounts for the fixed width output of the net use command, and should be much more accurate.
댓글
댓글 쓰기