0

I have the following code below I have to get down to under 260 characters. I had another post like this before and a powershell wizard came through and introduced me to wildcard matching, and although I spent hours studying what he did I can not figure out how to replicate the dark magic. Would someone be so kind to inform me. I have scoured the internet for reference and have failed on that front as well.

(netsh wlan show profiles) | sls "\:(.+)$" | %{$n=$_.Matches.Groups[1].Value.Trim(); $_} | %{(netsh wlan show profile name="$n" key=clear)}  | sls "Key Content\W+\:(.+)$" | %{$p=$_.Matches.Groups[1].Value.Trim(); $_} | %{[PSCustomObject]@{ ssid=$n;pass=$p }}

This is the example the wizard showed me. My code above this needs to use the same technique to shorten it even further.

this: Add-Type -AssemblyName System.Windows.Forms

to:

Add-Type -AssemblyName *m.W*s.F*s

8
  • The first example shows RegEx, not wildcard expression. Commented Aug 16, 2022 at 2:56
  • ok well no matter how hard I try to replicate what he did with other commands it never works Commented Aug 16, 2022 at 2:59
  • Probably because most cmdlets don't accept either RegEx, or Wildcard Patterns. So, your question is just trying to shorten System.Windows.Forms using RegEx/Wildcard Patterns? Commented Aug 16, 2022 at 3:05
  • no that is an exmaple of how it was shown to me working, I need to apply that to the code above it Commented Aug 16, 2022 at 3:06
  • 2
    This is 216 characters switch -r(netsh wlan show profiles){'\:(.+)$'{$n = $Matches[1].Trim();switch -r(netsh wlan show profile name="$n" key=clear){'Key Content\W+\:(.+)$'{$p = $Matches[1].Trim();[PSCustomObject]@{ssid = $n; pass = $p}}}}} Commented Aug 16, 2022 at 4:35

1 Answer 1

2

Technically what you're doing is code golf. It's ugly and not something that is usually recommended. If I recall correctly, for some reason you're trying to fit scripts like this into the Run box. This is the smallest I could get it with a decently described object at the end.

switch -r(netsh wl sh pr){':\s(.+)'{$s=$matches.1;switch -r(netsh wl sh pr n=$s k=clear){'tent.+:\s(.+)'{[PSCustomObject]@{SSID=$s;Pass=$matches.1}}}}}
Sign up to request clarification or add additional context in comments.

5 Comments

Beat mine... Nice
Yes that is correct. Run box accepts 259 characters. To be clear your help is extremely appreciated but I also want to learn how to incorporate this functionality myself though it is understandably ugly. Was it trial and error for you or is there actually documentation somewhere?
@IamJakoby, it's about the art of refactoring. The more you know about the programming/scripting language, the better, because then you can look at the code and see that it can be rewritten a different way. For example, with the sls and match combination you had I realized that that could be done more concisely with a switch statement. Doug here realized that he could skip the $p assignment completely and just output it in the psobject. Also shortening the netsh parameters was genius. I was thinking there might even be a way to define a quick function since some of the code repeats
Thank you Daniel, I am putting as much effort as I can into becoming more proficient every day and trying to learn something new. I like asking in here not because I'm lazy but the community as a whole has a level of ingenuity that's often not even in the docs and I greatly appreciate being able to learn from you guys. You just now gave me a lot to explore and I'm appreciative.
Keep it up. Some advice. Go through this guy's answers and responses and you will learn A LOT

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.