批量WMIC重定向输出并将其包装为变量 [英] Batch WMIC redirecting output and wrapping into variable

查看:113
本文介绍了批量WMIC重定向输出并将其包装为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用WMIC删除旧版打印服务器上的某些旧版打印机

I'm trying to use WMIC to remove some old printers on our older print servers

如果我在命令行中使用以下语法,则可以使用

if I use the below syntax from the command line it works

WMIC打印机在"SERVERNAME之类的'%% OLDSERVERNAME %%'"中删除

WMIC PRINTER WHERE "SERVERNAME LIKE '%%OLDSERVERNAME%%'" DELETE

有人问我是否可以更换打印机或对其进行更新,以使服务台免于受到如何添加打印机"的呼叫

I was asked to see if I could make it replace the printers or update them to spare the helpdesk from getting "how do I add my printer" calls

我尝试使用SET设置SERVERNAME的属性,它似乎可以处理,但是没有任何变化,执行GET仍显示旧值.

I tried some variation with SET to set the properties for SERVERNAME, it appear to process, but nothing changes and doing a GET shows the old value still.

所以我想我可以执行FOR/F循环,将打印机的名称(共享名)包装在一个变量中,然后在删除有问题的打印机之后,使用另一个命令重新创建打印机.

so I figured I could do a FOR /F loop, wrap the name of the printer (sharename) in a variable and then use another command to recreate the printers, after the offending printers have been deleted.

这是我的剧本

@ECHO

ECHO Gathering Installed Printer Information...

FOR /F "tokens=1 skip=1" %%A IN ('WMIC PRINTER WHERE "Servername like "%%printaz1%%"" get sharename') DO SET PRINTERNAME=%%A

ECHO %PRINTERNAME%

所以您会认为可以将值全部包装在一个不错的变量中以进行处理,但是没有,我打开了echo,似乎变量在被捕获后立即被设置回null

so you would think you'd get the value all wrapped up in a nice variable to do with as you wish but no, I turned echo on and it appears that the variable gets set back to null as soon as it's captured

我的输出看起来像这样...

my output looks like this...

FOR /F "tokens=1 skip=1" %%A IN ('WMIC PRINTER WHERE "Servername like "%%printaz1%%"" get sharename') DO SET PRINTERNAME=%%A

SET PRINTERNAME=nameofprinter

SET PRINTERNAME=

ECHO

ECHO is on.

想知道为什么它似乎会丢弃字符串.

would love some thoughts on why it seems to be dumping the string.

Powershell和VBScript也不是不可能,我都熟悉,这似乎有可能,而且我真的不想从头开始.

Powershell and VBScript aren't out of the option either, I'm familar with both, just this seemed possible, and I really don't want to start over.

推荐答案

WMIC返回两行,其中一行具有打印机名称,另一行为空.
解决方案是测试空行.

WMIC is returning two lines, one with the printername, and one empty.
The solution to this is to test for empty lines.

ECHO Gathering Installed Printer Information...
FOR /F "tokens=1 skip=1" %%A IN ('WMIC PRINTER WHERE "Servername like "%%printaz1%%"" get sharename') DO if not "%%A"=="" SET PRINTERNAME=%%A
ECHO %PRINTERNAME%

这篇关于批量WMIC重定向输出并将其包装为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆