wmic useraccount SID空格/空行

时间:2016-10-18 02:39:36

标签: windows batch-file cmd wmic

我有two commands来获取SID useraccount

wmic useraccount where name='%username%' get sid | findstr /b /C:"S-1" > file.txt

for /F "tokens=2 delims=," %f in ('whoami /user /FO CSV /NH') do echo %~f > file.txt

两个案例(file.txt)都包含空格/空行。我尝试删除

findstr /r /v "^$"

但这是不可能的。如何删除所有空格/空行以仅获取SID号

3 个答案:

答案 0 :(得分:2)

<div class="element" id="mycat">
            <table width="100%" border="0" style="">
                <tbody>
                    <tr>
                        <td width="30%" align="right">
                            <div style="float:right"><label id="lbl_eCat0">Category 0</label></div>
                        </td>
                        <td width="70%" align="left">
                            <div id="div_eCat0" style="float:left">
                                <input type="checkbox" name="eCat0" value="1" class="formField"> 
                            </div>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
        <div class="element" id="mycat">
            <table width="100%" border="0" style="">
                <tbody>
                    <tr>
                        <td width="30%" align="right">
                            <div style="float:right"><label id="lbl_eCat1">Category 1</label></div>
                        </td>
                        <td width="70%" align="left">
                            <div id="div_eCat1" style="float:left">
                                <input type="checkbox" name="eCat1" value="1" class="formField"> 
                            </div>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
        <div class="element" id="mycat">
            <table width="100%" border="0" style="">
                <tbody>
                    <tr>
                        <td width="30%" align="right">
                            <div style="float:right"><label id="lbl_eCat2">Category 2</label></div>
                        </td>
                        <td width="70%" align="left">
                            <div id="div_eCat2" style="float:left">
                                <input type="checkbox" name="eCat2" value="1" class="formField"> 
                            </div>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
        <div class="element" id="mycat">
            <table width="100%" border="0" style="">
                <tbody>
                    <tr>
                        <td width="30%" align="right">
                            <div style="float:right"><label id="lbl_eCat3">Category 3</label></div>
                        </td>
                        <td width="70%" align="left">
                            <div id="div_eCat3" style="float:left">
                                <input type="checkbox" name="eCat3" value="1" class="formField"> 
                            </div>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>

这会跳过标题,包含一个附加字段,因此@echo off setlocal enableextensions disabledelayedexpansion for /f "skip=2 tokens=2 delims=," %%a in (' wmic path win32_useraccount where name^="%username%" get sid^,status /format:csv ') do ( >"file.txt" <nul set /p"=%%a" ) 是记录中的第二个字段(sid格式包含csv字段)所以我们可以避免结束在node命令的输出中回车(每行以wmic结尾)并使用CR+CR+LF命令将输出发送到文件,输入流从{{1}读取设备。这样,文件的输出将不包含set /p

中包含的结尾nul

CR+LF

中的相同代码
echo

答案 1 :(得分:1)

这个批处理文件可以解决这个问题:

@echo off
for /f "delims= " %%a in ('"wmic path win32_useraccount where name='%UserName%' get sid"') do (
   if not "%%a"=="SID" (          
      set myvar=%%a
      goto :loop_end
   )   
)

:loop_end
echo SID=%myvar%
echo %myvar% > SID_%username%.txt
pause>nul
Start "" SID_%username%.txt

对于whoami /user /FO CSV /NH的第二种方法,您可以看一下:

<强> Get SID for current logged in domain user

答案 2 :(得分:0)

我是怎么做的,WMIC:

for /f "skip=1" %a in ('"wmic useraccount where name='%username%' get sid"') do @for %b in (%a) do @(>file.txt echo(%b)

和WhoAmI

for /f tokens^=3^ delims^=^" %a in ('whoami /user /FO CSV /NH') do @(>file.txt echo(%a)

在命令行中匹配问题(忽略批处理文件标记)