gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 10:37:36 24 4
gpt4 key购买 nike

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

最佳答案

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

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

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

我修改了下面的 Batch 文件,将 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

关于windows - 为什么我的整个批处理脚本作为 SETLOCAL 命令运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16723382/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com