为什么我的整个批处理脚本作为SETLOCAL命令运行?

时间:2013-05-23 20:32:47

标签: windows batch-file vbscript wmic

我不明白我做错了什么,输出总是整个脚本,出现Invalid parameter to setlocal错误!这可能只是一个愚蠢的错误,但它让我发疯。

SETLOCAL ENABLEDELAYEDEXPANSION
REM Obtain username of logged in user
SET loggedinuser=%USERNAME%

REM Create temporary vbscript to obtain user OU
echo Const ADS_SCOPE_SUBTREE = 2 >temp.vbs
echo. >>temp.vbs
echo Set objConnection = CreateObject("ADODB.Connection") >>temp.vbs
echo Set objCommand =   CreateObject("ADODB.Command") >>temp.vbs
echo objConnection.Provider = "ADsDSOObject" >>temp.vbs
echo objConnection.Open "Active Directory Provider" >>temp.vbs
echo Set objCommand.ActiveConnection = objConnection >>temp.vbs
echo. >>temp.vbs
echo objCommand.Properties("Page Size") = 1000 >>temp.vbs
echo objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE >>temp.vbs
echo. >>temp.vbs
echo objCommand.CommandText = _ >>temp.vbs
echo    ^"SELECT distinguishedName FROM ^'LDAP://dc=test,dc=com^' ^" ^& _ >>temp.vbs
echo        "WHERE objectCategory='user' " ^& _ >>temp.vbs
echo            "AND sAMAccountName='!loggedinuser!'" >>temp.vbs
echo Set objRecordSet = objCommand.Execute >>temp.vbs
echo. >>temp.vbs
echo objRecordSet.MoveFirst >>temp.vbs
echo Do Until objRecordSet.EOF >>temp.vbs
echo     strDN = objRecordSet.Fields("distinguishedName").Value >>temp.vbs
echo     arrPath = Split(strDN, ",") >>temp.vbs
echo    intLength = Len(arrPath(1)) >>temp.vbs
echo    intNameLength = intLength - 3 >>temp.vbs
echo    Wscript.Echo Right(arrPath(1), intNameLength) >>temp.vbs
echo    objRecordSet.MoveNext >>temp.vbs
echo Loop >>temp.vbs

REM Save backup of old printer list, just in case
echo Creating a backup list of current printers, please wait...
wmic printer list brief /format:csv > \\networkshare\userfiles\!loggedinuser!\oldprinterlist.txt
echo Backup list completed.

REM Set the OU variable by running the vbscript
echo Discovering your department...
FOR /F "delims=" %%a in ('cscript.exe /nologo temp.vbs') do @set OU=%%a
echo Adding printers for %OU%

REM Perform new printer install based on OU
if %OU%==MIS (
set printer1=Printer_1_Yo
set printer2=Printer_2_Yo
set printer3=Printer_3_Yo
echo Adding %printer1%, please wait...
wmic printer call addprinterconnection \\newprintserver\%printer1%
echo %printer1% added. Adding %printer2%, please wait...
wmic printer call addprinterconnection \\newprintserver\%printer2%
echo %printer2% added. Adding %printer3%, please wait...
wmic printer call addprinterconnection \\newprintserver\%printer3%
echo %printer3% added. All printers are ready to use!
)

REM Delete printers that were on job
echo Deleting old printers from testserver, please wait...
wmic printer where servername=\\\\testserver delete
echo Deletion complete.
echo. If you would like to add more printers, please visit the Printers page on the intranet.
echo. Press any key to close this window.
pause>temp.txt
del temp.txt
del temp.vbs

经过进一步测试后,似乎wmic printer where行无法正常工作,我很快就会解决这个问题(欢迎提出建议)......但这是整个剧本分崩离析的原因吗?我知道vbscript部分有点奇怪,但我认为这也不是问题。如果我错了,请纠正我!

2 个答案:

答案 0 :(得分:1)

如果您的脚本不以@ECHO OFF命令开头,那么您将在屏幕上看到运行时的完整脚本内容。

我想好好利用这篇文章,所以我修改了你的脚本,以便使temp.vbs文件的创建更加清晰,尽管这一点与你的问题没有直接关系。但是,当我在创建temp.vbs文件后插入GOTO :EOF命令测试下面的批处理文件时,它正确运行时没有" setlocal"错误!

编辑:我意识到原始脚本在创建temp.vbs程序时使用loggedinuser变量对其值进行硬编码,这是因为每个文件都是创建和删除的时间。我原来的翻译没有考虑到这个细节。

我修改了下面的批处理文件,以便在参数中将loggedinuser的值从Batch传递到VBS部分。这样,.vbs程序只需用更合适的名称创建一次。

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
REM Obtain username of logged in user
SET loggedinuser=%USERNAME%

REM Create temporary vbscript to obtain user OU, if not exists
if not exist getUserOU.vbs (
   for /F "delims=:" %%a in ('findstr /N "^:VBS_Section" "%~F0"') do set n=%%a
   more +!n! < "%~F0" > getUserOU.vbs
)

REM Save backup of old printer list, just in case
echo Creating a backup list of current printers, please wait...
wmic printer list brief /format:csv > 

\\networkshare\userfiles\!loggedinuser!\oldprinterlist.txt
echo Backup list completed.

REM Set the OU variable by running the vbscript
echo Discovering your department...
FOR /F "delims=" %%a in ('cscript.exe //nologo getUserOU.vbs "%loggedinuser%"') do @set OU=%%a
echo Adding printers for %OU%

REM Perform new printer install based on OU
if %OU%==MIS (
set printer1=Printer_1_Yo
set printer2=Printer_2_Yo
set printer3=Printer_3_Yo
echo Adding %printer1%, please wait...
wmic printer call addprinterconnection \\newprintserver\%printer1%
echo %printer1% added. Adding %printer2%, please wait...
wmic printer call addprinterconnection \\newprintserver\%printer2%
echo %printer2% added. Adding %printer3%, please wait...
wmic printer call addprinterconnection \\newprintserver\%printer3%
echo %printer3% added. All printers are ready to use!
)

REM Delete printers that were on job
echo Deleting old printers from testserver, please wait...
wmic printer where servername=\\\\testserver delete
echo Deletion complete.
echo. If you would like to add more printers, please visit the Printers page on the intranet.
echo. Press any key to close this window.
pause>temp.txt
del temp.txt

goto :EOF

:VBS_Section

Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

objCommand.CommandText = _
   "SELECT distinguishedName FROM 'LDAP://dc=test,dc=com' " & _
       "WHERE objectCategory='user' " & _
           "AND sAMAccountName='" & WScript.Arguments(0) & "'"
Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst
Do Until objRecordSet.EOF
    strDN = objRecordSet.Fields("distinguishedName").Value
    arrPath = Split(strDN, ",")
   intLength = Len(arrPath(1))
   intNameLength = intLength - 3
   Wscript.Echo Right(arrPath(1), intNameLength)
   objRecordSet.MoveNext
Loop

答案 1 :(得分:0)

对于wmic打印机命令,请使用:

wmic printer where 'servername="\\\\testserver"'

如果不起作用,请交换单引号和双引号。我不是在电脑上,所以我要从记忆中走出来。此外,您不必经历所有创建vbscript以获得ou。 Wmic可以查询ldap。

 WMIC /NAMESPACE:\\root\directory\ldap PATH ds_user  GET ds_distinguishedname

这是我的脚本版本。

@echo off
setlocal enabledelayedexpansion

set q=wmic /NAMESPACE:\\root\directory\ldap PATH ds_user Where "ds_samaccountname^='!username!'" get ds_distinguishedname

for /f "skip=1 tokens=3 delims==" %%a in ('%q%') do (
   for /f "tokens=1 delims=," %%b in ("%%a") do set ou=%%b
)

:: Save backup of old printer list, just in case
set share=\\networkshare\userfiles
if not exist "%share%\!username!" md "%share%\!username!"
set printlist="%share%\!username!\oldprinterlist.txt"
echo Creating a backup list of current printers, please wait...
wmic printer list brief /format:csv > %printlist%
echo Backup list completed.

::Perform new printer install based on OU
IF %ou%==MIS (
  call :addprinter Printer_1_Yo Testserver
  call :addprinter Printer_2_Yo Testserver
  call :addprinter Printer_3_Yo Testserver
)

::Delete printers that were on job
echo.
echo Deleting old printers from testserver, please wait...
wmic printer where "servername='\\\\testserver'" delete
echo Deletion complete.
echo.
echo. If you would like to add more printers, please visit the Printers page on the intranet.
echo. Press any key to close this window.
pause>nul  
goto :eof

:addprinter prn server
echo.
echo Adding %1, please wait...
wmic printer call addprinterconnection \\%2\%1