Vbscript Windows启动脚本

Vbscript Windows启动脚本,vbscript,Vbscript,我有一个脚本需要修改,以便在桌面上创建一个文件夹,如果它不存在于Windows 7 PC上,则与所有人共享-无域。这是脚本,需要帮助在底部 ======================================================= Set objShell = CreateObject("WScript.Shell") objComputer=objShell.ExpandEnvironmentStrings("%ComputerName%") IF Right(objCom

我有一个脚本需要修改,以便在桌面上创建一个文件夹,如果它不存在于Windows 7 PC上,则与所有人共享-无域。这是脚本,需要帮助在底部 =======================================================

Set objShell = CreateObject("WScript.Shell")
objComputer=objShell.ExpandEnvironmentStrings("%ComputerName%")
IF Right(objComputer,3) = "000" Then

Else

strShortcut = objShell.SpecialFolders( "Desktop" )  & "\%username% Share.lnk"
strShortcut = objShell.ExpandEnvironmentStrings(strShortcut)

Set objLink = objShell.CreateShortcut( strShortcut ) 

objComputer=objShell.ExpandEnvironmentStrings("%ComputerName%")
objServer=Left(objComputer,7) & "-000"

objLink.Description = objShell.ExpandEnvironmentStrings("%username% Share")
objLink.TargetPath = objShell.ExpandEnvironmentStrings("\\" & objServer & "\Users\%username%\Desktop\%username% Share")
objLink.Save

End If
=======================================================

Set objShell = CreateObject("WScript.Shell")
objComputer=objShell.ExpandEnvironmentStrings("%ComputerName%")
IF Right(objComputer,3) = "000" Then

Else

strShortcut = objShell.SpecialFolders( "Desktop" )  & "\%username% Share.lnk"
strShortcut = objShell.ExpandEnvironmentStrings(strShortcut)

Set objLink = objShell.CreateShortcut( strShortcut ) 

objComputer=objShell.ExpandEnvironmentStrings("%ComputerName%")
objServer=Left(objComputer,7) & "-000"

objLink.Description = objShell.ExpandEnvironmentStrings("%username% Share")
objLink.TargetPath = objShell.ExpandEnvironmentStrings("\\" & objServer & "\Users\%username%\Desktop\%username% Share")
objLink.Save

End If
如果“C:\Users\%username%\desktop\%username%Share”退出,则不会执行任何操作

如果没有,请创建文件夹并与所有人共享只读


上面的两个if语句是我需要补充的,有人知道如何实现这一点吗

在VBScript中,您将使用
FileSystemObject
实例处理文件夹,使用WMI处理共享:

path = "%USERPROFILE%\Desktop\%USERNAME% Share"

Set sh  = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

path = sh.ExpandEnvironmentStrings(path)

If Not fso.FolderExists(path) Then
  'create the folder
  fso.CreateFolder path

  'create the share
  Set wmi = GetObject("winmgmts://./root/cimv2")

  Set trustee = wmi.Get("Win32_Trustee").SpawnInstance_()
  trustee.Domain = Null
  trustee.Name   = "Everyone"
  trustee.SID    = Array(1,1,0,0,0,0,0,1,0,0,0,0) 'SID S-1-1-0 (binary)

  Set ace = wmi.Get("Win32_Ace").SpawnInstance_()
  ace.AccessMask = 1179817  'read/execute access
  ace.AceFlags   = 3        'object inheritance + container inheritance
  ace.AceType    = 0        'allow access
  ace.Trustee    = trustee

  Set sd = wmi.Get("Win32_SecurityDescriptor").SpawnInstance_()
  sd.DACL = Array(ace)

  Set share = wmi.Get("Win32_Share")
  rc = share.Create(path, fso.GetFileName(path), 0, 10, "", "", sd)
End If
分批进行可能更简单,但:

@echo off

set "fldr=%USERPROFILE%\Desktop\%USERNAME% Share"
for %%n in ("%fldr%") do set "name=%%~nxn"

if not exist "%fldr%" (
  mkdir /p "%fldr%"
  net share "%name%"="%fldr%" /grant:Everyone,READ
)
附录:请注意,
CreateFolder
函数要求您要创建的文件夹的父文件夹已经存在。在您的特定场景中(用户的桌面文件夹),可以合理地假设。但是,如果有人想将此应用于更一般的场景,他们需要使用创建父文件夹和子文件夹