0

So, I'm running the command netsh wlan show profiles and I want to filter out all of the output except for the strings that include the SSID names into separate variables. The output looks similar to this:

    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

and so on.

How would I go about getting each string by itself without the All User Profile : behind it, and then setting it into a variable, with each string having its own seperate variable? I would like to keep it in CMD and Powershell. I know there's the CMD for command, but the best I could come up with is

for /f "delims=" %%a in ('
     netsh wlan show profiles
     ^| findstr "    All User Profile     : "
') do set "code=%%a"

which would only set one, and it also wouldn't filter out the All User Profile : part of the command, it would take the whole line which is not what I want. I found this page, but I don't think that will work.

EDIT: So I've made some progress, but I don't like it because it's sloppy and uses temporary files.

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"

Also, doing this if I were to set the contents of the text file just made into a variable, it would only be the first line of the file, and would only make one variable, which are both problems.

EDIT 2: Made everything wayyy more specific.

EDIT 3: Ok, I'm so close now, sorry for not being specific enough earlier. This is what I have right now:

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"

But the problem is that it sets each string as code, so they just overwrite each other. I'm thinking something like this but I don't know the syntax well enough.

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"
)

Here's the continuation of this

I did get it, here's the final code:

$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
}
jangles
  • 167
  • 1
  • 2
  • 18
  • 1
    Why do you think you need each value in an individual variable? What are you trying to do with these variables? – Ansgar Wiechers Jul 20 '17 at 22:10
  • @AnsgarWiechers - I am going to use these variables in another command as a parameter. – jangles Jul 21 '17 at 00:00
  • 1
    An array or hashtable would be a better choice than individually named variables, so you can then [splat](https://msdn.microsoft.com/en-us/powershell/reference/3.0/microsoft.powershell.core/about/about_splatting) the array/hashtable on the other command. Take Bill's suggestion and proceed from there. Batch is not a good tool for this. – Ansgar Wiechers Jul 21 '17 at 07:02
  • @AnsgarWiechers - You're probably right, I'm looking into arrays right now. – jangles Jul 21 '17 at 18:07
  • I suggest you to read [this answer](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990) – Aacini Jul 22 '17 at 01:47
  • Trying to emulate arrays in batch is tedious and error-prone and should be avoided on general principle. – Ansgar Wiechers Jul 23 '17 at 22:31

2 Answers2

2

PowerShell:

command | Select-String '^Output: (.*)' | ForEach-Object {
  $_.Matches[0].Groups[1].Value
}
Bill_Stewart
  • 18,984
  • 4
  • 42
  • 53
  • I tried this and it gave me no output. I should have mentioned the command is a CMD command. So I tried `cmd.exe /c "command"` but with still no output. – jangles Jul 20 '17 at 22:30
  • 2
    @jangles Generally speaking, there is no need to do that. PowerShell is a shell, and can execute commands just like cmd.exe would. – TheMadTechnician Jul 20 '17 at 22:44
  • @TheMadTechnician - Right, but the command I'm using is a Windows command, not a Powershell command. That's why I specified using cmd.exe to run the command. – jangles Jul 20 '17 at 23:59
  • Netstat.exe isn't a PowerShell command either, but you can run `Netstat -a -o` in PowerShell just fine. – TheMadTechnician Jul 21 '17 at 00:28
  • @TheMadTechnician - Oh, I think I get what you're saying. Even if I put just the command in though, it doesn't output? – jangles Jul 21 '17 at 00:40
  • I tested with a console executable that writes to standard output and it works fine for me. What command/program are you running? – Bill_Stewart Jul 21 '17 at 01:48
  • @Bill_Stewart - Never mind, after your comment I gave it a second look and it works now. It does a great job at filtering out, but I still need to have each line be a separate variable in the batch file. – jangles Jul 21 '17 at 02:43
  • Why do you need a batch file when you can use PowerShell? – Bill_Stewart Jul 21 '17 at 12:41
  • @Bill_Stewart - I know batch way better than I do PowerShell. I wouldn't know how to continue with what I'm doing, which is setting variables by each line then using the variables to run code. – jangles Jul 21 '17 at 18:01
  • In that case, we would definitely recommend learning PowerShell more in-depth. Your learning investment will be well rewarded. – Bill_Stewart Jul 21 '17 at 18:02
  • @Bill_Stewart - Okay thank you! I'm looking into arrays right now in PowerShell as AnsgarWiechers recommended. So far it looks like exactly what I need. – jangles Jul 21 '17 at 18:10
  • @Bill_Stewart - Should I just make a new question? Because now with ForEach-Object I'm trying to add them to an array, but it's selecting each letter as an object, not each string. – jangles Jul 21 '17 at 18:28
  • Yes, you probably should. – Bill_Stewart Jul 21 '17 at 18:36
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

I used a file containing your data and typed it to simulate your command.

The findstr will assign a serial number to the detected line because of the /n option, so the output will be eg. 2:>>Output: String1. Tokenise that using : and space; %%a becomes 2, %%b >>Output and %%c String1

set code simply lists all of the variables whose names start code

Magoo
  • 68,705
  • 7
  • 55
  • 76
  • It doesn't completely separate it for me, it gives me the code number, then the output-while cut down-is not completely filtered. I updated my post to be a little bit more specific, sorry I should have done that to begin with. – jangles Jul 20 '17 at 22:29
  • Since your output contains `User Profile` not `Output` that changes the tokenising since the delimiters include space and there is an extra token on the line before the colon. Therefore you'd need to change the `tokens` option to `tokens=1,3*`. I presume you've changed the `findstr` target string for your actual data. – Magoo Jul 21 '17 at 00:53
  • `for /f "tokens=1,3*delims=: " %%a in (' netsh wlan show profiles ^| findstr /n " All User Profile : " ') do set "code%%a=%%c"` This is what I have right now, and it outputs this: `code10=Profile : String1 code11=Profile : String2 code12=Profile : String3 code13=Profile : String4 code2=interface Wi-Fi:` If I remove `/n` from `findstr` I would think that would remove the codes on the beginning of each line but it messes it up further. It's hard to see in the comments because it's not formatted correctly. – jangles Jul 21 '17 at 01:13
  • I am relying on your description of the output of your command. I don't have a wireless interface on my system. You've stated that the lines began `>>Output:` and at present your problem-description shows "User Profile : String1" You now use ` All User Profile : `. If *that* is the case, then you've added yet another delimited string into the mix and you'd need `tokens=1,4*` – Magoo Jul 21 '17 at 02:10
  • I'm sorry, `>>Output:` was just a placeholder. I was trying to make it simple but I made it so much more complex than it needed to be. – jangles Jul 21 '17 at 02:34