obtendo o valor do profilepath na variável em lote

1

Eu preciso escrever um arquivo batch para fazer alguns movimentos de conteúdo do diretório.

Como posso obter em uma variável de lote o valor do caminho de perfil retornado pelo comando net user xxx / domain?

    
por niko_las 30.05.2018 / 19:49

1 resposta

0

Você pode usar um loop para / f e colocar o comando net user canalizado para um findstr usando tokens e delims de acordo com o script em lote para analisar a saída e obter o valor do campo User profile , que está definido para a conta no AD que você executa nesse comando.

Script

@ECHO ON
for /f "tokens=3 delims= " %%a in ('net user <username> /domain ^| findstr /i "profile"') do set profilepath=%%a
echo %profilepath%

Script Notes

  • This script assumes you will replace <username> below with the explicit value of the needed username when run to get the Profile path from the net use xxx /domain command.

  • The caret symbol in front of the pipe symbol ( i.e. ^|) within the brackets of the for /f loop pipes the output of the net user command into the findstr command so it's there to escape the pipe symbol so at command execution time within the loop it knows that's redirecting one command's output as another commands input otherwise it gets confused so simply escape it.

  • The percent sign has special meaning in batch scripts so when using a for loop within a batch script you need to double the percent sign i.e. %%a of this placeholder to escape it ensuring it's interpreted as a single % and can pass the variable accordingly within the loop.

Mais recursos

  • FOR / F
  • FindStr

  • For /?

    delims=xxx      - specifies a delimiter set.  This replaces the
                      default delimiter set of space and tab.
    tokens=x,y,m-n  - specifies which tokens from each line are to
                      be passed to the for body for each iteration.
                      This will cause additional variable names to
                      be allocated.  The m-n form is a range,
                      specifying the mth through the nth tokens.  If
                      the last character in the tokens= string is an
                      asterisk, then an additional variable is
                      allocated and receives the remaining text on
    
  • Caracteres de escape, delimitadores e citações

por 30.05.2018 / 20:16