For /F with wmic unwanted output

前端 未结 4 1077
不思量自难忘°
不思量自难忘° 2020-12-04 03:40

First post here, so apologies if I don\'t do this quite right.

I\'m trying to output the OS version on a remote Windows PC, but I keep getting unwanted data. Execut

4条回答
  •  渐次进展
    2020-12-04 04:10

    I'd suggest

    set "remotever="
    FOR /F "skip=1 tokens=*" %%A in ('wmic /node:%hostname% OS get caption^|more') DO if not defined remotever set "remotever=%%A"
    echo %remotever:~0,-1%
    

    more is the classic batch method to conver wmic's unicode output to ANSI.

    But there's a catch. Apart from Unicode, WMIC "ends" its lines with CRCRLF not CRLF and the penultimate CR is then included in the value assigned to the variable (hence echo the substring not including the last character - and you'll find some trailing spaces...).

    (as a slightly easier demo, try echo q%%Aj in your original - you'll find the q is overwritten by the j)

    Hence, to capture the first line out, set a variable to nothing, then in the for loop set it to the value you want to capture, but gate the set with if not defined.

提交回复
热议问题