【问题标题】:Why is my entire batch script running as a SETLOCAL command?为什么我的整个批处理脚本都作为 SETLOCAL 命令运行?
【发布时间】:2013-05-23 20:32:46
【问题描述】:

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

【问题讨论】:

  • 我唯一想到的就是检查文件是否以 Windows 样式格式化,而不是 Linux。
  • wmic printer get ServerName 的输出是什么?
  • @npocmaka:新旧打印服务器按某种顺序排列。 \\oldserver \\oldserver \\oldserver \\newserver 全部用换行符分隔。
  • 那么为什么在 where 查询中使用 4 个斜杠?我认为你不需要转义序列。
  • 啊啊啊。抱歉,您还需要将服务器设置为单引号:where "servername like 'someServer'"

标签: windows batch-file vbscript wmic


【解决方案1】:

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

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

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

我修改了下面的批处理文件,将loggedinuser 的值从批处理传递到参数中的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

【讨论】:

    【解决方案2】:

    对于您的 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
    

    【讨论】:

    • 那个 wmic 打印机命令确实有效。但是,对于通过 SETLOCAL 运行的所有内容,我仍然遇到同样的问题。此外,那个 wmic 命令只是给了我完整的 AD 用户列表和他们的 OU - 哎呀!我确信有一种方法可以调低它,但我喜欢 vbscript 的一件事是输出如何只是没有添加 OU= 的 OU。
    • 使用 wmic 获取特定用户的 ou 是微不足道的,而且只需几行代码。明天我上班的时候给你看。
    猜你喜欢
    • 2010-09-23
    • 2015-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-02
    • 2023-03-03
    相关资源
    最近更新 更多