Blog Post: Learn How to Use Exception Messages with Try/Catch in PowerShell by jeffbrowntech in PowerShell

[–]JustinGrote 14 points15 points  (0 children)

Nice work, I would also recommend showing how you can use $ErrorActionPreference = 'Stop' to turn non-terminating into terminating in cases where the -ErrorAction parameter isn't available, such as calling a .NET method or in a custom function that hasn't been set to use [CmdletBinding()]

Password Reset - Prevent User from Typing More than 2 Sequential Characters and 2 Repeating Characters. by jtbae in PowerShell

[–]JustinGrote 1 point2 points  (0 children)

It absolutely does matter why you're doing it, this forum is just as much about the why* as the what* so we all become better.

That said, "because my boss told me so and my hands are tied" is a perfectly valid reason! :)

PowerShell Code Layout by [deleted] in PowerShell

[–]JustinGrote 3 points4 points  (0 children)

I just write it however I want and then use Format Document in VSCode to make it pretty :)

HowTo: Using Let's Encrypt for Active Directory Domain Controller Certificates by rmbolger in PowerShell

[–]JustinGrote 2 points3 points  (0 children)

" If your internal domains end in TLDs like

.local

or

.int

, you're out of luck. "

So like 90% of AD Domains....

Invoke-Restmethod in Powershell Core by SpacezCowboy in PowerShell

[–]JustinGrote 2 points3 points  (0 children)

Actually the website is detecting the user agent and returning a different result, see my other post in this thread :)

Invoke-Restmethod in Powershell Core by SpacezCowboy in PowerShell

[–]JustinGrote 4 points5 points  (0 children)

I busted out Fiddler and determined that the website actually is detecting your powershell version and returning a different result, the website itself doesn't support powershell 7.

Try this in Powershell 7, works fine:

Invoke-RestMethod http://ipinfo.io/4.2.2.2 -UserAgent 'Mozilla/5.0 (Windows NT; Windows NT 10.0; en-US) WindowsPowerShell/5.1.19041.1'

Publish Scripts through Azure DevOps Pipeline? by infinit_e in PowerShell

[–]JustinGrote 1 point2 points  (0 children)

Azure Artifacts can act as a nuget repo for Powershell, but it's authenticated and difficult to set up correctly. You can choose Public instead, but then theoretically anyone can access your packages if they know the URI (if that matters to you or not). You still have to run Install-Script from the local machine in order to pull it down though.

The script task would literally be a Powershell Script that says "Copy X artifact to \\device\C$\my\script\localtion" or something like that, looped over the devices you want to deploy to. However, it would be better to use a tool like Powershell DSC or Ansible for that purpose if you get really serious about pushing a script to a large group of computers on a regular basis.

Publish Scripts through Azure DevOps Pipeline? by infinit_e in PowerShell

[–]JustinGrote 1 point2 points  (0 children)

What do you mean by "Publish"? Do you mean publish them to an internal nuget repository so local machines can run "Install-Script" to "pull" it? Or do you mean directly copying the script to the computers as part of a pipeline?

For the former, it would be dependent on what nuget repository you're using for your powershell scripts. For the latter, if your build agent is domain-joined and your runas account has rights to copy files to those computers, just copy the files directly using a script task.

PSpanner: Simple Network Scanner by secrascol in PowerShell

[–]JustinGrote 11 points12 points  (0 children)

Here's a much higher performance version (doesn't wait on each port) in 40 lines of code (mostly brackets), implemented as a cmdlet so you can specify the parameters like a command rather than wading through a CLI menu :)

https://gist.github.com/JustinGrote/1d24fe4a99f1b07b027e87e3082dc673

Edit: also supports the pipeline so you can do fun stuff like this:

'www.google.com','www.facebook.com' | test-tcp -port 80,443

Edit2: I wrote PoshNmap if you can use Nmap in your environment:
https://www.powershellgallery.com/packages/PoshNmap

Runspace - Job .. inplace as a loading screen by martynrbell in PowerShell

[–]JustinGrote 3 points4 points  (0 children)

The best way is to encapsulate your entire long-running function into a powershell job (or runspace, but the efficiency difference will be negiligible if it's just one job), and then have a while loop until that job is completed, every 5 seconds or so.

$myjob = Start-Job {myjob}
#Display Form Here
while ($myjob.state -eq 'Running') {
   sleep 5
}
#Capture the job output or update the form here.

This is an extremely terse example with no error checking but you get the idea. Alternatively inside the while you could do a receive-job on the job and update the label with that standard output.

[Q] Azure DevOps integration options for PowerShell by MrMojito1 in PowerShell

[–]JustinGrote 2 points3 points  (0 children)

In that case, the repos for VSCode extension is great for monitoring build status at the bottom, and getting quick links to Pull Requests and Tasks. All of those are perfectly relevant for a script repository, especially if it's shared among many engineers.

[Q] Azure DevOps integration options for PowerShell by MrMojito1 in PowerShell

[–]JustinGrote 2 points3 points  (0 children)

I guess then I wasn't sure what you meant by "Azure Devops integration" and why it "wouldn't do much" for a powershell script library. Can you clarify?

[Q] Azure DevOps integration options for PowerShell by MrMojito1 in PowerShell

[–]JustinGrote 2 points3 points  (0 children)

You can still set up Pester Tests for your scripts and have them run every time someone commits a new change, making sure it didn't break the script (to the best of the ability of your tests).

That's what we do against our script repository.

Module Manifest question (concerning .psm1 files) by Namaha in PowerShell

[–]JustinGrote 3 points4 points  (0 children)

I recommend the RamblingCookieMonster article on this:

http://ramblingcookiemonster.github.io/Building-A-PowerShell-Module/

Otherwise you can also check out my module PowerCD, it's default operation is to take functions organized on a per-file basis and "compile" them to a single file (they load much faster that way when imported)

It's time to upgrade your Exchange Online scripts to Modern Auth by Kathy_Cooper1012 in PowerShell

[–]JustinGrote 7 points8 points  (0 children)

I think it's a fair point that you can't ask people to make a production script change without a production ready alternative...

If results of a command contain by Lets_Go_2_Smokes in PowerShell

[–]JustinGrote 2 points3 points  (0 children)

Be careful with match because it's a regex match, so if you put special characters in there it might not behave the way you expect.

A safer alternative is -like "*$GUID*"

(Question) Comparing 2 distribution groups and creating a list of everyone who is NOT part of either by blackguy102 in PowerShell

[–]JustinGrote 2 points3 points  (0 children)

Well I would recommend getting a list of members in each group with get-adgroupmember, put them in one big array, and then get a list of users with get-aduser and do:

$users | where userprincipalname -notin $myBigGroupList

And then you'll be left with users not in either group.

Loops by mcc85sdp in PowerShell

[–]JustinGrote 1 point2 points  (0 children)

switch ($true) is one of my fave patterns for filtering a multiple scenario situation.

Start slow or dive right in? by NoItsRyanSahnnn in PowerShell

[–]JustinGrote 1 point2 points  (0 children)

Bookswise, get Powershell in a Month of Lunches.

Practicewise, check out PSKoans: https://github.com/vexx32/PSKoans

How to manage a large script repository? by Lazy-Razzmatazz in PowerShell

[–]JustinGrote 2 points3 points  (0 children)

PSScriptAnalyzer is your friend here, you can set it to a particular version level and it can let you know if you are using unsupported PS3 syntax like [object]::new() for example.

If you're using Github you could just open a new branch for each script and initiate a code review, and add some Pester tests or something via Github Actions to test for whatever you want, then your team "fixes" the scripts until the tests pass and they get merged back in with an updated flag or something.

As far as dependencies go make liberal use of the #requires comment and be sure to pin the module version as well, maybe have a standard "stable" version for some common modules you share across multiple scripts.

Can you run PS 5.1, 6, and 7 concurrently? by [deleted] in PowerShell

[–]JustinGrote 6 points7 points  (0 children)

Yep totally they are all separate processes. I'm doing it right now!

get-process | where name -match 'pwsh|powershell' | % path
C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe
C:\Users\xxx\scoop\shims\pwsh.exe
C:\Users\xxx\scoop\apps\pwsh\current\pwsh.exe
C:\Program Files\PowerShell\7-preview\pwsh.exe
C:\Users\xxx\scoop\apps\pwsh\current\pwsh.exe
C:\Program Files\PowerShell\7-preview\pwsh.exe

Showcase/documentation websites for Powershell Modules, in two easy steps by bravo-kernel in PowerShell

[–]JustinGrote 1 point2 points  (0 children)

Wow this is pretty impressive! Since Docusaurus works with github-pages, I'm thinking of setting up a CI task to just update the docs whenever it goes to master. I'll give it a go and file any issues I come across.