所以,我正在运行命令netsh wlan show profiles
,我希望将包含SSID名称的字符串除外的所有输出过滤成单独的变量。输出看起来类似于:
All User Profile : String1
All User Profile : String2
All User Profile : String3
All User Profile : String4
All User Profile : String5
All User Profile : String6
All User Profile : String7
All User Profile : String8
等等。
如何在没有All User Profile :
的情况下自己获取每个字符串,然后将其设置为变量,每个字符串都有自己的单独变量?我想将它保存在CMD和Powershell中。我知道有CMD for
命令,但我能想到的最好的是
for /f "delims=" %%a in ('
netsh wlan show profiles
^| findstr " All User Profile : "
') do set "code=%%a"
只会设置一个,并且它也不会过滤掉命令的All User Profile :
部分,它会占用整行而不是我想要的。
我找到了this页面,但我认为这不起作用。
编辑:所以我取得了一些进展,但我不喜欢它,因为它很草率并使用临时文件。
netsh wlan show profiles | findstr /v "Wi-Fi:" | findstr /v "profiles" | findstr /v "^-" | findstr /v "None" > test.txt
powershell -Command "(gc test.txt) -replace ' All User Profile : ', '' | sc test2.txt"
另外,如果我将文本文件的内容设置为变量,那么这样做只会是文件的第一行,并且只会产生一个变量,这两个都是问题。
编辑2 :使一切都更加具体。
编辑3 :好的,我现在离这么近了,抱歉早些时候不够具体。 这就是我现在所拥有的:
for /f "delims=" %%a in ('
powershell -command "netsh wlan show profiles | Select-String '^ All User Profile : (.*)' | ForEach-Object {$_.Matches[0].Groups[1].Value}"
') do set "code=%%a"
但问题是它将每个字符串设置为代码,因此它们只是相互覆盖。 我在想这样的事情,但我不太清楚语法。
for /f "delims=" %%a in ('
powershell -command "netsh wlan show profiles | Select-String '^ All User Profile : (.*)' | ForEach-Object {$_.Matches[0].Groups[1].Value}"
') do (
set "code1=%%a"
set "code2=%%a"
set "code3=%%a"
set "code4=%%a"
set "code5=%%a"
set "code6=%%a"
set "code7=%%a"
set "code8=%%a"
)
我确实得到了,这是最终的代码:
$array = netsh wlan show profiles |
ForEach-Object {
if ($_ -match "\s*All User Profile\s*:\s*(.*)") { $($matches[1]) }
}
foreach ($wn in $array) {
netsh WLAN show profile name=$wn
}
答案 0 :(得分:2)
的PowerShell:
command | Select-String '^Output: (.*)' | ForEach-Object {
$_.Matches[0].Groups[1].Value
}
答案 1 :(得分:1)
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q45225939.txt"
for /f "tokens=1,2*delims=: " %%a in ('
TYPE "%filename1%"
^| findstr /n "Output: "
') do set "code%%a=%%c"
SET code
GOTO :EOF
我使用了一个包含您数据的文件,并使用type
来模拟您的command
。
由于findstr
选项,/n
将为检测到的行分配序列号,因此输出将为例如。 2:>>Output: String1
。使用:
和空格的标记; %%a
变为2
,%%b
>>Output
和%%c
String1
set code
只列出名称以code