2

我不明白我做错了什么,输出总是整个脚本,有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 部分有点奇怪,但我认为这也不是问题。如果我错了,请纠正我!

4

2 回答 2

1

If your script does NOT start with @ECHO OFF command, then you will see in the screen the fully script contents when it runs.

I want to make good use of this post, so I modified your script in order to make the creation of the temp.vbs file much cleaner, although this point is not directly related to your problem. However, when I tested the Batch file below with a GOTO :EOF command inserted after the creation of the temp.vbs file, it correctly runs with no "setlocal" error!

EDIT: I realized that original script uses loggedinuser variable to hardcode its value in the creation of the temp.vbs program, that is the reason because the file is created and deleted each time. My original translation does not account for this detail.

I modified the Batch file below to pass the value of loggedinuser from Batch to VBS section in the parameter. This way, the .vbs program could be created just once with a more appropriate name.

@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
于 2013-05-24T00:31:16.170 回答
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
于 2013-05-23T21:31:42.203 回答