Board logo

标题: VBS远程主机共享设置 [打印本页]

作者: youxi01    时间: 2007-11-5 20:12     标题: VBS远程主机共享设置

  1. '///////code by youxi01@bbs.bathome.net
  2. '//////早期作品;
  3. '/////依赖rpc
  4. on error resume next
  5. if (lcase(right(wscript.fullname,11))="wscript.exe") then
  6.    set objShell=wscript.createObject("wscript.shell")
  7. objShell.Run("cmd.exe /k  title 远程主机共享"&"& cscript //nologo " &wscript.scriptfullname)
  8. wscript.quit
  9. end if
  10. dim objconnect
  11. connect()
  12. sub connect()
  13. wscript.echo "            #欢迎使用#                "
  14. wscript.echo "**************************************"
  15. wscript.echo "#请输入远程主机名或IP地址#"
  16. strcomputer=wscript.stdin.readline
  17. set objShell=wscript.createObject("wscript.shell")
  18. set objexec=objShell.exec("ping -n 2 -w 1000 "&strcomputer)
  19. if instr(objexec.stdout.readall,"Reply from") then
  20. wscript.echo
  21. wscript.echo "#请输入用户名:"
  22. username=wscript.stdin.readline
  23. wscript.echo
  24. wscript.echo "#请输入密码:"
  25. pass=wscript.stdin.readline
  26. wscript.echo "#正在列举共享,请等待...."
  27. set obj=createobject("wbemscripting.swbemlocator")
  28. set objconnect=obj.connectserver(strcomputer,"root\cimv2",username,pass)
  29. for each ps in objconnect.execquery("select * from win32_logicaldisk where description='本地固定磁盘'")
  30. cnt=objconnect.execquery("select * from win32_share where name='"&left(ps.name,1)&"$'").count
  31. if cnt>0 then
  32. share=share+"固定磁盘"+vbtab+ps.volumename+" "+ps.name&vbcrlf
  33. else
  34. noshare=noshare+vbcrlf+"固定磁盘"+vbtab+ps.volumename+" "+ps.name
  35. end if
  36. next
  37. if noshare="" then
  38. wscript.echo "**************************************"
  39. wscript.echo "#所有的磁盘已经共享:"&vbcrlf&share
  40. else
  41. wscript.echo "**************************************"
  42. wscript.echo "#已经共享的磁盘:"&vbcrlf&vbcrlf&share
  43. wscript.echo "**************************************"
  44. wscript.echo "#未共享的磁盘:"&vbcrlf&noshare
  45. wscript.echo "**************************************"
  46. end if
  47. choice()
  48. else
  49. wscript.echo "远程主机: "&chr(34)&strcomputer&chr(34)&" ping不通."
  50. choice()
  51. end if
  52. end sub
  53. sub setshare()
  54. wscript.echo "**************************************"
  55. wscript.echo "请输入需要共享的磁盘"
  56. input=wscript.stdin.readline
  57. input=left(input,1)
  58. input1=input&":"
  59. set share=objconnect.get("win32_share")
  60. errreturn=share.create(input1&"\",input&"$",0,10,"默认共享")
  61. if errreturn=0 then
  62. wscript.echo input&": 共享成功"
  63. choice()
  64. elseif errreturn=22 then
  65. wscript.echo input &": 共享失败,该共享已经存在"
  66. choice()
  67. elseif errreturn=24 then
  68. wscript.echo input &": 共享失败,该盘符不存在"
  69. choice()
  70. else
  71. wscript.echo input &": 共享失败,存在未知错误"
  72. choice()
  73. end if
  74. end sub
  75. sub choice()
  76. wscript.echo "**************************************"
  77. wscript.echo "请输入指令:"
  78. wscript.echo "A、添加共享"
  79. wscript.echo "D、删除共享"
  80. wscript.echo "L、列举共享"
  81. wscript.echo "C、重新连接"
  82. wscript.echo "Q、退出"
  83. input=wscript.stdin.readline
  84. if ucase(input)="Q" then
  85. wscript.quit
  86. elseif ucase(input)="D" then
  87. delshare()
  88. elseif ucase(input)="A" then
  89. setshare()
  90. elseif ucase(input)="L" then
  91. listshare()
  92. elseif ucase(input)="C" then
  93. connect()
  94. else
  95. wscript.echo "未知的服务"
  96. choice()
  97. end if
  98. end sub
  99. sub delshare()
  100. wscript.echo "请输入要删除的盘符名:"
  101. input=wscript.stdin.readline
  102. input=left(input,1)
  103. input1=input&"$"
  104. for each ps in objconnect.execquery("select * from win32_share where name='"&input1&"'")
  105. errreturn=ps.delete
  106. next
  107. if errreturn=0 then
  108. wscript.echo input1&" 成功删除"
  109. choice()
  110. else
  111. wscript.echo input&" 删除失败,发生未知错误 "
  112. choice()
  113. end if
  114. end sub
  115. sub listshare()
  116. for each drive in objconnect.execquery("select * from win32_share where description='默认共享'")
  117. share1=share1+"固定磁盘:"+vbtab+left(drive.name,1)&":"+vbcrlf
  118. next
  119. if share1="" then
  120. wscript.echo vbcrlf&"没有共享"
  121. else
  122. wscript.echo "**************************************"
  123. wscript.echo vbcrlf&"已经存在的共享有:"&vbcrlf&share1
  124. end if
  125. choice()
  126. end sub
复制代码





欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2