简体   繁体   English

从批处理文件运行.VBS

[英]Running .VBS from Batch File

I have been writing some batch files to some specific job in my role as Desktop Support. 我一直在以桌面支持的身份将一些批处理文件写入某些特定工作。

I have been scripting a Profile Backup script for a couple of months now, changing as the role requires and as new issues crop up. 我已经为“个人档案备份”脚本编写了几个月的脚本,随着角色要求和新问题的出现而变化。

I am trying to run the following VBS script from inside my batch file and point its output to another folder. 我正在尝试从批处理文件内部运行以下VBS脚本,并将其输出指向另一个文件夹。

Set oNet= WScript.CreateObject("WScript.Network")
Set oDrives = oNet.EnumNetworkDrives
oUser = oNet.UserName
Set oFilesys = CreateObject("Scripting.FileSystemObject")
Set oFiletxt = oFilesys.CreateTextFile("Mapped_Network_Drives_"&oUser&".cmd", True)
For i = 0 to oDrives.Count - 1
oFiletxt.WriteLine "Net Use " & oDrives.Item(i) & " " & chr(34)& oDrives.Item(i+1) & chr(34)  & " /persistent:yes"
Next
oFiletxt.Close

This VBS script was made (not by me) to export a user Mapped Network Drives to a CMD file that uses the netuse command to remap them. 制作此VBS脚本(不是我本人)是为了将用户映射的网络驱动器导出到使用netuse命令重新映射它们的CMD文件。

If the VBS script is run itself outside of the batch file then its works fine, but when it is run inside the batch using cscript, etc, then it creates the output file Mapped_Network_Drives_%username%.cmd, but it is empty. 如果VBS脚本本身在批处理文件外部运行,则可以正常工作,但是当使用cscript等在批处理内部运行VBS脚本时,它将创建输出文件Mapped_Network_Drives_%username%.cmd,但为空。

REM pushes script to use batch file Drive Letter as working directory
setlocal & pushd %~d0
ECHO =============================
ECHO Creating Backup Folder
ECHO =============================
MD "%UserName%_Backup"
ECHO =============================
ECHO Backing Up Mapped Network Drives
ECHO =============================
REM pushes script to use Backup Folder as working directory
pushd "%~d0%UserName%_Backup"
REM Runs VB script to backup Mapped Network Drives
cscript //nologo "%~dp0Mapped_Network_Drives.vbs"
pause

Above is a snipped of the code i have been working with. 上面是我一直在使用的代码的摘要。

Try with this 试试这个

vbs part : Corrected iteration over the drive collection vbs部分 :在驱动器集合上更正了迭代

Option Explicit 

Dim userName, drives
    With WScript.CreateObject("WScript.Network")
        userName = .UserName
        Set drives = .EnumNetworkDrives 
    End With 

Dim drive
    With WScript.CreateObject("Scripting.FileSystemObject").CreateTextFile("Mapped_Network_Drives_" & userName & ".cmd", True)
        For drive = 0 To (drives.Count-1) Step 2
            .WriteLine "net use " & drives.Item(drive) & " """ & drives.Item(drive+1) & """ /persistent:yes"
        Next 
        .Close
    End With 

batch part : Ensures all path references are properly handled 批处理部分 :确保正确处理所有路径引用

@echo off
    setlocal enableextensions disabledelayedexpansion

    call :getCurrentBatchFile _f0
    for %%a in ("%_f0%") do (
        set "backupFolder=%%~da\%username%_Backup"
        set "scriptsFolder=%%~dpa"
    )

    md "%backupFolder%" 2>nul
    pushd "%backupFolder%" && (
        cscript //nologo "%scriptsFolder%\Mapped_Network_Drives.vbs"
        popd
    ) || (
        echo Backup folder does not exist
    )

    goto :eof

:getCurrentBatchFile returnVar
    set "%~1=%~f0"
    goto :eof

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM