Create and delete share on remote computer.

Home Forums Scripting General Scripting Create and delete share on remote computer.

This topic contains 3 replies, has 3 voices, and was last updated by Avatar Anonymous 10 years, 5 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • Avatar
    igor7
    Member
    #145397

    Hi, all!
    I need create share on some folder on remote computer, perform some task on this share and then delete this share (not the folder itself). I’ working on windows XP professional computer that connected to Windows 2000 professional computer. My goal is create share on W2K computer from Windows XP. This is not very complicated. I found solution on Scripting Guy site. Here the code for create share:

    Const FILE_SHARE = 0
    Const MAXIMUM_CONNECTIONS = 25

    strComputer = “atl-ws-01”
    Set objWMIService = GetObject _
    (“winmgmts:\” & strComputer & “rootcimv2”)

    Set objNewShare = objWMIService.Get(“Win32_Share”)

    errReturn = objNewShare.Create _
    (“C:Public”, “PublicShare”, FILE_SHARE, _
    MAXIMUM_CONNECTIONS, “Public share for Fabrikam employees.”)

    [/CODE]

    And here example for delete share:

    [CODE]
    strComputer = “atl-ws-01”
    Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\” & strComputer & “rootcimv2”)
    Set colShares = objWMIService.ExecQuery _
    (“Select * from Win32_Share Where Name = ‘PublicShare'”)
    For Each objShare in colShares
    objShare.Delete
    Next
    wscript.Sleep 3000
    WScript.Echo “Done”
    WScript.Quit
    [/CODE]

    Also it is possible create share on remote computer using wmic.exe:

    [CODE]
    @echo on
    wmic /node:Computer name /user:”domainusername” /password:user password share call create “”, “Share Description”, “Maxsimum Connections”, “Share Name”, “”, “Local Path”, 0
    wmic exit
    [/CODE]

    BTW. I didn’t find solution for remove share using wmic.exe. Any help with such as code is very appreciated…

    Both method work fine until they are executed under administrator account on Windows XP computer. When I trying execute those scripts under regular user account I received following errors:

    [IMG]http://img14.imageshack.us/img14/9200/vbsscripterror.jpg[/IMG]

    this is for *.vbs

    [IMG]http://img34.imageshack.us/img34/2663/wmicerror.jpg[/IMG]

    and this one for wmic method.

    I searched the web for solution and found partial solution one more time on
    Scripting Guy web site:

    [URL=http://blogs.technet.com/heyscriptingguy/archive/2009/06/29/how-can-i-use-the-runas-command-to-run-a-script-under-alternate-user-credentials.aspx]How Can I Use the RunAs Command to Run a Script Under Alternate User Credentials?[/URL]

    This is not good enough for me because user still need to provide administrator password in opened command promt window… My goal – as less user interaction as it possible.
    I continued search the web for solution and finally found this script that doing job pretty well:

    [CODE]
    On Error Resume Next
    dim WshShell,oArgs,FSO
    ‘ —————————-
    ‘ configuration variables
    ‘ —————————-
    ‘ sUser = username
    ‘ sPass = password
    ‘ sCmd = path to the program
    ‘ —————————-
    sUser=”domainusername”
    sPass=”password”
    sCmd=”[URL=”file://your_server/your_files/your.bat”]\your_serveryour_filesyour.bat[/URL]”

    ‘ —————————-
    ‘ don’t touch below
    ‘ —————————-
    set oArgs=wscript.Arguments
    set WshShell = CreateObject(“WScript.Shell”)
    set WshEnv = WshShell.Environment(“Process”)
    WinPath = WshEnv(“SystemRoot”)&”System32runas.exe”
    set FSO = CreateObject(“Scripting.FileSystemObject”)
    if FSO.FileExists(winpath) then
    ‘wscript.echo winpath & ” ” & “verified”
    else
    wscript.echo “!! ERROR !!” & VBCRLF & “Can’t find or verify ” & winpath &”.” & VBCRLF & “You must be running Windows 2000 for this script to work.”
    set WshShell=Nothing
    set WshEnv=Nothing
    set oArgs=Nothing
    set FSO=Nothing
    wscript.quit
    end if
    rc = WshShell.Run(“runas /user:” & sUser & ” ” & CHR(34) & sCmd & CHR(34), 2, FALSE)
    ‘wscript.Sleep 1 ‘need to give time for window to open.
    WshShell.AppActivate(WinPath) ‘make sure we grab the right window to send password to
    WshShell.SendKeys sPass & “{ENTER}” ‘send the password to the waiting window.
    ‘set WshShell=Nothing
    ‘set oArgs=Nothing
    ‘set WshEnv=Nothing
    ‘set FSO=Nothing
    ‘wscript.quit

    [/CODE]

    Here is the link where original code was posted:

    [URL]http://forums.aspfree.com/windows-scripting-64/batch-file-temporarily-utilize-administrative-privileges-23496.html[/URL]

    As I said above this script doing job well but I still have a little problem
    When script running it open command prompt window for a user (administrator) password:

    [IMG]http://img197.imageshack.us/img197/7313/waitforinput.jpg[/IMG]

    Some times this window close it self automatically and some times it stay opened on system tray. I’m trying figure out why it happened? This script use SendKeys method for send password to opened windows, per my assumption:

    [CODE]
    rc = WshShell.Run(“runas /user:” & sUser & ” ” & CHR(34) & sCmd & CHR(34), 2, FALSE)
    ‘wscript.Sleep 1 ‘need to give time for window to open.
    WshShell.AppActivate(WinPath) ‘make sure we grab the right window to send password to
    WshShell.SendKeys sPass & “{ENTER}” ‘send the password to the waiting window
    [/CODE]

    But why promt window close itself sometime and sometime not… I have no idea. Any help will be appreciated.[CODE]
    Const FILE_SHARE = 0
    Const MAXIMUM_CONNECTIONS = 25

    strComputer = “atl-ws-01”
    Set objWMIService = GetObject _
    (“winmgmts:\” & strComputer & “rootcimv2”)

    Set objNewShare = objWMIService.Get(“Win32_Share”)

    errReturn = objNewShare.Create _
    (“C:Public”, “PublicShare”, FILE_SHARE, _
    MAXIMUM_CONNECTIONS, “Public share for Fabrikam employees.”)

    [/CODE]

    And here example for delete share:

    strComputer = “atl-ws-01”
    Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\” & strComputer & “rootcimv2”)
    Set colShares = objWMIService.ExecQuery _
    (“Select * from Win32_Share Where Name = ‘PublicShare'”)
    For Each objShare in colShares
    objShare.Delete
    Next
    wscript.Sleep 3000
    WScript.Echo “Done”
    WScript.Quit
    [/CODE]

    Also it is possible create share on remote computer using wmic.exe:

    [CODE]
    @echo on
    wmic /node:Computer name /user:”domainusername” /password:user password share call create “”, “Share Description”, “Maxsimum Connections”, “Share Name”, “”, “Local Path”, 0
    wmic exit
    [/CODE]

    BTW. I didn’t find solution for remove share using wmic.exe. Any help with such as code is very appreciated…

    Both method work fine until they are executed under administrator account on Windows XP computer. When I trying execute those scripts under regular user account I received following errors:

    [IMG]http://img14.imageshack.us/img14/9200/vbsscripterror.jpg[/IMG]

    this is for *.vbs

    [IMG]http://img34.imageshack.us/img34/2663/wmicerror.jpg[/IMG]

    and this one for wmic method.

    I searched the web for solution and found partial solution one more time on
    Scripting Guy web site:

    [URL=http://blogs.technet.com/heyscriptingguy/archive/2009/06/29/how-can-i-use-the-runas-command-to-run-a-script-under-alternate-user-credentials.aspx]How Can I Use the RunAs Command to Run a Script Under Alternate User Credentials?[/URL]

    This is not good enough for me because user still need to provide administrator password in opened command promt window… My goal – as less user interaction as it possible.
    I continued search the web for solution and finally found this script that doing job pretty well:

    [CODE]
    On Error Resume Next
    dim WshShell,oArgs,FSO
    ‘ —————————-
    ‘ configuration variables
    ‘ —————————-
    ‘ sUser = username
    ‘ sPass = password
    ‘ sCmd = path to the program
    ‘ —————————-
    sUser=”domainusername”
    sPass=”password”
    sCmd=”[URL=”file://your_server/your_files/your.bat”]\your_serveryour_filesyour.bat[/URL]”

    ‘ —————————-
    ‘ don’t touch below
    ‘ —————————-
    set oArgs=wscript.Arguments
    set WshShell = CreateObject(“WScript.Shell”)
    set WshEnv = WshShell.Environment(“Process”)
    WinPath = WshEnv(“SystemRoot”)&”System32runas.exe”
    set FSO = CreateObject(“Scripting.FileSystemObject”)
    if FSO.FileExists(winpath) then
    ‘wscript.echo winpath & ” ” & “verified”
    else
    wscript.echo “!! ERROR !!” & VBCRLF & “Can’t find or verify ” & winpath &”.” & VBCRLF & “You must be running Windows 2000 for this script to work.”
    set WshShell=Nothing
    set WshEnv=Nothing
    set oArgs=Nothing
    set FSO=Nothing
    wscript.quit
    end if
    rc = WshShell.Run(“runas /user:” & sUser & ” ” & CHR(34) & sCmd & CHR(34), 2, FALSE)
    ‘wscript.Sleep 1 ‘need to give time for window to open.
    WshShell.AppActivate(WinPath) ‘make sure we grab the right window to send password to
    WshShell.SendKeys sPass & “{ENTER}” ‘send the password to the waiting window.
    ‘set WshShell=Nothing
    ‘set oArgs=Nothing
    ‘set WshEnv=Nothing
    ‘set FSO=Nothing
    ‘wscript.quit

    [/CODE]

    Here is the link where original code was posted:

    [URL]http://forums.aspfree.com/windows-scripting-64/batch-file-temporarily-utilize-administrative-privileges-23496.html[/URL]

    As I said above this script doing job well but I still have a little problem
    When script running it open command prompt window for a user (administrator) password:

    [IMG]http://img197.imageshack.us/img197/7313/waitforinput.jpg[/IMG]

    Some times this window close it self automatically and some times it stay opened on system tray. I’m trying figure out why it happened? This script use SendKeys method for send password to opened windows, per my assumption:

    [CODE]
    rc = WshShell.Run(“runas /user:” & sUser & ” ” & CHR(34) & sCmd & CHR(34), 2, FALSE)
    ‘wscript.Sleep 1 ‘need to give time for window to open.
    WshShell.AppActivate(WinPath) ‘make sure we grab the right window to send password to
    WshShell.SendKeys sPass & “{ENTER}” ‘send the password to the waiting window
    [/CODE]

    But why promt window close itself sometime and sometime not… I have no idea. Any help will be appreciated.[CODE]
    strComputer = “atl-ws-01”
    Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\” & strComputer & “rootcimv2”)
    Set colShares = objWMIService.ExecQuery _
    (“Select * from Win32_Share Where Name = ‘PublicShare'”)
    For Each objShare in colShares
    objShare.Delete
    Next
    wscript.Sleep 3000
    WScript.Echo “Done”
    WScript.Quit
    [/CODE]

    Also it is possible create share on remote computer using wmic.exe:

    @echo on
    wmic /node:Computer name /user:”domainusername” /password:user password share call create “”, “Share Description”, “Maxsimum Connections”, “Share Name”, “”, “Local Path”, 0
    wmic exit
    [/CODE]

    BTW. I didn’t find solution for remove share using wmic.exe. Any help with such as code is very appreciated…

    Both method work fine until they are executed under administrator account on Windows XP computer. When I trying execute those scripts under regular user account I received following errors:

    [IMG]http://img14.imageshack.us/img14/9200/vbsscripterror.jpg[/IMG]

    this is for *.vbs

    [IMG]http://img34.imageshack.us/img34/2663/wmicerror.jpg[/IMG]

    and this one for wmic method.

    I searched the web for solution and found partial solution one more time on
    Scripting Guy web site:

    [URL=http://blogs.technet.com/heyscriptingguy/archive/2009/06/29/how-can-i-use-the-runas-command-to-run-a-script-under-alternate-user-credentials.aspx]How Can I Use the RunAs Command to Run a Script Under Alternate User Credentials?[/URL]

    This is not good enough for me because user still need to provide administrator password in opened command promt window… My goal – as less user interaction as it possible.
    I continued search the web for solution and finally found this script that doing job pretty well:

    [CODE]
    On Error Resume Next
    dim WshShell,oArgs,FSO
    ‘ —————————-
    ‘ configuration variables
    ‘ —————————-
    ‘ sUser = username
    ‘ sPass = password
    ‘ sCmd = path to the program
    ‘ —————————-
    sUser=”domainusername”
    sPass=”password”
    sCmd=”[URL=”file://your_server/your_files/your.bat”]\your_serveryour_filesyour.bat[/URL]”

    ‘ —————————-
    ‘ don’t touch below
    ‘ —————————-
    set oArgs=wscript.Arguments
    set WshShell = CreateObject(“WScript.Shell”)
    set WshEnv = WshShell.Environment(“Process”)
    WinPath = WshEnv(“SystemRoot”)&”System32runas.exe”
    set FSO = CreateObject(“Scripting.FileSystemObject”)
    if FSO.FileExists(winpath) then
    ‘wscript.echo winpath & ” ” & “verified”
    else
    wscript.echo “!! ERROR !!” & VBCRLF & “Can’t find or verify ” & winpath &”.” & VBCRLF & “You must be running Windows 2000 for this script to work.”
    set WshShell=Nothing
    set WshEnv=Nothing
    set oArgs=Nothing
    set FSO=Nothing
    wscript.quit
    end if
    rc = WshShell.Run(“runas /user:” & sUser & ” ” & CHR(34) & sCmd & CHR(34), 2, FALSE)
    ‘wscript.Sleep 1 ‘need to give time for window to open.
    WshShell.AppActivate(WinPath) ‘make sure we grab the right window to send password to
    WshShell.SendKeys sPass & “{ENTER}” ‘send the password to the waiting window.
    ‘set WshShell=Nothing
    ‘set oArgs=Nothing
    ‘set WshEnv=Nothing
    ‘set FSO=Nothing
    ‘wscript.quit

    [/CODE]

    Here is the link where original code was posted:

    [URL]http://forums.aspfree.com/windows-scripting-64/batch-file-temporarily-utilize-administrative-privileges-23496.html[/URL]

    As I said above this script doing job well but I still have a little problem
    When script running it open command prompt window for a user (administrator) password:

    [IMG]http://img197.imageshack.us/img197/7313/waitforinput.jpg[/IMG]

    Some times this window close it self automatically and some times it stay opened on system tray. I’m trying figure out why it happened? This script use SendKeys method for send password to opened windows, per my assumption:

    [CODE]
    rc = WshShell.Run(“runas /user:” & sUser & ” ” & CHR(34) & sCmd & CHR(34), 2, FALSE)
    ‘wscript.Sleep 1 ‘need to give time for window to open.
    WshShell.AppActivate(WinPath) ‘make sure we grab the right window to send password to
    WshShell.SendKeys sPass & “{ENTER}” ‘send the password to the waiting window
    [/CODE]

    But why promt window close itself sometime and sometime not… I have no idea. Any help will be appreciated.[CODE]
    @echo on
    wmic /node:Computer name /user:”domainusername” /password:user password share call create “”, “Share Description”, “Maxsimum Connections”, “Share Name”, “”, “Local Path”, 0
    wmic exit
    [/CODE]

    BTW. I didn’t find solution for remove share using wmic.exe. Any help with such as code is very appreciated…

    Both method work fine until they are executed under administrator account on Windows XP computer. When I trying execute those scripts under regular user account I received following errors:

    vbsscripterror.jpg

    this is for *.vbs

    wmicerror.jpg

    and this one for wmic method.

    I searched the web for solution and found partial solution one more time on
    Scripting Guy web site:

    How Can I Use the RunAs Command to Run a Script Under Alternate User Credentials?

    This is not good enough for me because user still need to provide administrator password in opened command promt window… My goal – as less user interaction as it possible.
    I continued search the web for solution and finally found this script that doing job pretty well:

    On Error Resume Next
    dim WshShell,oArgs,FSO
    ‘ —————————-
    ‘ configuration variables
    ‘ —————————-
    ‘ sUser = username
    ‘ sPass = password
    ‘ sCmd = path to the program
    ‘ —————————-
    sUser=”domainusername”
    sPass=”password”
    sCmd=”[URL=”file://your_server/your_files/your.bat”]\your_serveryour_filesyour.bat[/URL]”

    ‘ —————————-
    ‘ don’t touch below
    ‘ —————————-
    set oArgs=wscript.Arguments
    set WshShell = CreateObject(“WScript.Shell”)
    set WshEnv = WshShell.Environment(“Process”)
    WinPath = WshEnv(“SystemRoot”)&”System32runas.exe”
    set FSO = CreateObject(“Scripting.FileSystemObject”)
    if FSO.FileExists(winpath) then
    ‘wscript.echo winpath & ” ” & “verified”
    else
    wscript.echo “!! ERROR !!” & VBCRLF & “Can’t find or verify ” & winpath &”.” & VBCRLF & “You must be running Windows 2000 for this script to work.”
    set WshShell=Nothing
    set WshEnv=Nothing
    set oArgs=Nothing
    set FSO=Nothing
    wscript.quit
    end if
    rc = WshShell.Run(“runas /user:” & sUser & ” ” & CHR(34) & sCmd & CHR(34), 2, FALSE)
    ‘wscript.Sleep 1 ‘need to give time for window to open.
    WshShell.AppActivate(WinPath) ‘make sure we grab the right window to send password to
    WshShell.SendKeys sPass & “{ENTER}” ‘send the password to the waiting window.
    ‘set WshShell=Nothing
    ‘set oArgs=Nothing
    ‘set WshEnv=Nothing
    ‘set FSO=Nothing
    ‘wscript.quit

    [/CODE]

    Here is the link where original code was posted:

    [URL]http://forums.aspfree.com/windows-scripting-64/batch-file-temporarily-utilize-administrative-privileges-23496.html[/URL]

    As I said above this script doing job well but I still have a little problem
    When script running it open command prompt window for a user (administrator) password:

    [IMG]http://img197.imageshack.us/img197/7313/waitforinput.jpg[/IMG]

    Some times this window close it self automatically and some times it stay opened on system tray. I’m trying figure out why it happened? This script use SendKeys method for send password to opened windows, per my assumption:

    [CODE]
    rc = WshShell.Run(“runas /user:” & sUser & ” ” & CHR(34) & sCmd & CHR(34), 2, FALSE)
    ‘wscript.Sleep 1 ‘need to give time for window to open.
    WshShell.AppActivate(WinPath) ‘make sure we grab the right window to send password to
    WshShell.SendKeys sPass & “{ENTER}” ‘send the password to the waiting window
    [/CODE]

    But why promt window close itself sometime and sometime not… I have no idea. Any help will be appreciated.[CODE]
    On Error Resume Next
    dim WshShell,oArgs,FSO


    ‘ configuration variables


    ‘ sUser = username
    ‘ sPass = password
    ‘ sCmd = path to the program


    sUser=”domainusername”
    sPass=”password”
    sCmd=”[URL=”file://your_server/your_files/your.bat”]\your_serveryour_filesyour.bat[/URL]”


    ‘ don’t touch below


    set oArgs=wscript.Arguments
    set WshShell = CreateObject(“WScript.Shell”)
    set WshEnv = WshShell.Environment(“Process”)
    WinPath = WshEnv(“SystemRoot”)&”System32runas.exe”
    set FSO = CreateObject(“Scripting.FileSystemObject”)
    if FSO.FileExists(winpath) then
    ‘wscript.echo winpath & ” ” & “verified”
    else
    wscript.echo “!! ERROR !!” & VBCRLF & “Can’t find or verify ” & winpath &”.” & VBCRLF & “You must be running Windows 2000 for this script to work.”
    set WshShell=Nothing
    set WshEnv=Nothing
    set oArgs=Nothing
    set FSO=Nothing
    wscript.quit
    end if
    rc = WshShell.Run(“runas /user:” & sUser & ” ” & CHR(34) & sCmd & CHR(34), 2, FALSE)
    ‘wscript.Sleep 1 ‘need to give time for window to open.
    WshShell.AppActivate(WinPath) ‘make sure we grab the right window to send password to
    WshShell.SendKeys sPass & “{ENTER}” ‘send the password to the waiting window.
    ‘set WshShell=Nothing
    ‘set oArgs=Nothing
    ‘set WshEnv=Nothing
    ‘set FSO=Nothing
    ‘wscript.quit

    [/CODE]

    Here is the link where original code was posted:

    http://forums.aspfree.com/windows-scripting-64/batch-file-temporarily-utilize-administrative-privileges-23496.html

    As I said above this script doing job well but I still have a little problem
    When script running it open command prompt window for a user (administrator) password:

    waitforinput.jpg

    Some times this window close it self automatically and some times it stay opened on system tray. I’m trying figure out why it happened? This script use SendKeys method for send password to opened windows, per my assumption:

    rc = WshShell.Run(“runas /user:” & sUser & ” ” & CHR(34) & sCmd & CHR(34), 2, FALSE)
    ‘wscript.Sleep 1 ‘need to give time for window to open.
    WshShell.AppActivate(WinPath) ‘make sure we grab the right window to send password to
    WshShell.SendKeys sPass & “{ENTER}” ‘send the password to the waiting window
    [/CODE]

    But why promt window close itself sometime and sometime not… I have no idea. Any help will be appreciated.[CODE]
    rc = WshShell.Run(“runas /user:” & sUser & ” ” & CHR(34) & sCmd & CHR(34), 2, FALSE)
    ‘wscript.Sleep 1 ‘need to give time for window to open.
    WshShell.AppActivate(WinPath) ‘make sure we grab the right window to send password to
    WshShell.SendKeys sPass & “{ENTER}” ‘send the password to the waiting window
    [/CODE]

    But why promt window close itself sometime and sometime not… I have no idea. Any help will be appreciated.

    Avatar
    Anonymous
    #367679

    Re: Create and delete share on remote computer.

    INPUT_FILE_NAME = “C:Computers.txt”
    Const FILE_SHARE = 0
    Const MAXIMUM_CONNECTIONS = 35
    Const FOR_READING = 1
    Set objFSO = CreateObject(“Scripting.FileSystemObject”)
    Set objFile = objFSO.OpenTextFile(INPUT_FILE_NAME, FOR_READING)
    strComputers = objFile.ReadAll
    objFile.Close
    arrComputers = Split(strComputers, vbCrLf)
    For Each strComputer In arrComputers
    Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\” & strComputers & “rootcimv2”)
    Set objNewShare = objWMIService.Get(“Win32_Share”)
    errReturn = objNewShare.Create _
    (“C:tas”, “TAS”, FILE_SHARE, _
    MAXIMUM_CONNECTIONS, “Public share for the Finance group.”)
    Wscript.Echo errReturn
    next

    Avatar
    igor7
    Member
    #295058

    Re: Create and delete share on remote computer.

    ramesh_hv,
    Thank you for post, but us I said above this script should be executed with admin privileges, otherwise it not work. The RunAS vbs script from my post resolve this problem, but it always leave RunAS.exe administrator password command prompt window open…
    I asked and I answer to myself, may be it will be useful for somebody else.
    To make sure RunAS.exe administrator command prompt window will close during main script execution need to add this code to script:

    Set oShell = CreateObject(“WScript.Shell”)
    Set oWmg = GetObject(“winmgmts:”)
    strWndprs = “select * from Win32_Process where name=’runas.exe'”
    Set objQResult = oWmg.Execquery(strWndprs)
    For Each objProcess In objQResult
    intReturn = objProcess.Terminate(1)
    Next
    [/CODE][CODE]
    Set oShell = CreateObject(“WScript.Shell”)
    Set oWmg = GetObject(“winmgmts:”)
    strWndprs = “select * from Win32_Process where name=’runas.exe'”
    Set objQResult = oWmg.Execquery(strWndprs)
    For Each objProcess In objQResult
    intReturn = objProcess.Terminate(1)
    Next
    [/CODE]

    Avatar
    biggles77
    Spectator
    #210344

    Re: Create and delete share on remote computer.

    Igor7, thanks for posting back with your solution. Your Community Spirit is appreciated. :beer: :beer:

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.