@ECHO OFF
setlocal EnableDelayedExpansion
call :testitout
goto:eof
set "SERVER[1]=server1.oll.is.server1.com"
set "SERVER[2]=server2.cyl.is.anydom.com"
:testitout
FOR /L %%i IN (1,1,2) DO (
ping !SERVER[%%i]! -n 1 -4 | find /i "TTL=">nul
if errorlevel 1 (
echo ping %!SERVER[%%i]!% failure
echo ping %%SERVER[%%i]%% failure
echo ping %!SERVER[%%i]!% failure
echo ping !SERVER[%%i]! failure
) else (
for /f "tokens=*" %%f in ('Cscript %WINDIR%\\System32\\Printing_Admin_Scripts\\en-US\\Prnmngr.vbs -l -s %%SERVER[%%i]%%') do (
set str1=%%f
if /i not "!str1:Server name=!"=="!str1!" (
set str1=!str1:Server name =!
if /i not "!str1:Server name=!"=="!str1!" (
echo | set /p=" "
) else (
echo | set /p="!str1! "
)
)
if /i not "!str1:Printer name=!"=="!str1!" (
set str1=!str1:Printer name =!
if /i not "!str1:Printer name=!"=="!str1!" (
echo | set /p=" "
) else (
echo | set /p="!str1! "
)
)
if /i not "!str1:Share name=!"=="!str1!" (
set str1=!str1:Share name =!
if /i not "!str1:Share name=!"=="!str1!" (
echo | set /p=" "
) else (
echo | set /p="!str1! "
)
)
if /i not "!str1:Driver name=!"=="!str1!" (
set str1=!str1:Driver name =!
if /i not "!str1:Driver name=!"=="!str1!" (
echo | set /p=" "
) else (
echo | set /p="!str1! "
)
)
if /i not "!str1:Port name=!"=="!str1!" (
set str1=!str1:Port name =!
if /i not "!str1:Port name=!"=="!str1!" (
echo | set /p=" "
) else (
echo !str1!
)
)
)
)
)
输出:
ping failure
ping %SERVER[1]% failure
ping failure
ping failure
ping failure
ping %SERVER[2]% failure
ping failure
ping failure
我尝试使用echo(%SERVER [%% i]%,!SERVER [%% i]!等)语法,但为什么不能得到数组变量的值? 它应显示 ping servername failure 请帮忙
编辑:
它与!SERVER[%%i]!
一起正常工作,直到我添加:testitout
函数,以便我可以在call :testitout > output.txt
之类的文件中捕获脚本内的输出,但在我添加了函数:testitout
之后它停止了工作。
答案 0 :(得分:1)
set
后面有call
条款。应该是相反的方式:
set "SERVER[1]=server1.oll.is.server1.com"
set "SERVER[2]=server2.cyl.is.anydom.com"
call :testitout
goto eof
由于set
仅在call
之后运行,因此您的参数没有获得任何值。