Announcement

Collapse
No announcement yet.

Create and delete share on remote computer.

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Create and delete share on remote computer.

    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:

    Code:
     
    Const FILE_SHARE = 0
    Const MAXIMUM_CONNECTIONS = 25
     
    strComputer = "atl-ws-01"
    Set objWMIService = GetObject _
        ("winmgmts:\\" & strComputer & "\root\cimv2")
     
    Set objNewShare = objWMIService.Get("Win32_Share")
     
    errReturn = objNewShare.Create _
        ("C:\Public", "PublicShare", FILE_SHARE, _
            MAXIMUM_CONNECTIONS, "Public share for Fabrikam employees.")
    And here example for delete share:

    Code:
     
    strComputer = "atl-ws-01"
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    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
    Also it is possible create share on remote computer using wmic.exe:

    Code:
     
    @echo on
    wmic /node:Computer name /user:"domain\username" /password:user password share call create "", "Share Description", "Maxsimum Connections", "Share Name", "", "Local Path", 0
    wmic exit
    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:



    this is for *.vbs




    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:

    Code:
     
    On Error Resume Next
    dim WshShell,oArgs,FSO 
    ' ----------------------------
    ' configuration variables
    ' ----------------------------
    ' sUser = username 
    ' sPass = password
    ' sCmd = path to the program
    ' ----------------------------
    sUser="domain\username"
    sPass="password" 
    sCmd="\\your_server\your_files\your.bat"
     
    ' ----------------------------
    ' don't touch below
    ' ----------------------------
    set oArgs=wscript.Arguments
    set WshShell = CreateObject("WScript.Shell")
    set WshEnv = WshShell.Environment("Process")
    WinPath = WshEnv("SystemRoot")&"\System32\runas.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
    Here is the link where original code was posted:

    http://forums.aspfree.com/windows-sc...ges-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:



    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
    But why promt window close itself sometime and sometime not... I have no idea. Any help will be appreciated.
    Last edited by igor7; 27th October 2009, 05:33.

  • #2
    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 & "\root\cimv2")
    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
    Last edited by ramesh_hv; 27th October 2009, 10:33.

    Comment


    • #3
      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:
      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

      Comment


      • #4
        Re: Create and delete share on remote computer.

        Igor7, thanks for posting back with your solution. Your Community Spirit is appreciated.
        1 1 was a racehorse.
        2 2 was 1 2.
        1 1 1 1 race 1 day,
        2 2 1 1 2

        Comment

        Working...
        X