PowerShell Fun

As some of you may know, I’ve been dabbling in Powershell for some time now, and have started trying to make it more useful on the Mac. There’s a lot that you can do on Windows that should work on the Mac and as of yet does not. One of the sillier things is “Get-ComputerInfo” which on Windows shows you all kinds of neat things about your computer, and doesn’t exist on the macOS version of PowerShell. I thought this was a shame so I set about trying to replicate this functionality, so that you get at least some of it. (There’s a lot of windows things that don’t make sense on the mac, so I didn’t worry if I didn’t get those.)

The name of the script is “Get-MacInfo” and it’s available from my Github site. It’s a pretty simple script, that should work under current versions of PowerShell. I’ve built and tested it under the 7.1 preview versions, but there’s nothing in there that shouldn’t work under 7.0. Prior to 7.0, no idea.

The script itself pulls in data from a number of sources, including uname, sw_ver, system_profiler, osascript, sysctl, and some built-in PowerShell functions. It dumps them all into a hashtable so they can be displayed/retrieved easier. If you run the script without any parameters, you get the full table dump, as seen here:

Name                           Value
----                           -----
macOSBuildLabEx                17.7.0:
macOSCurrentVersion            10.13.6
macOSCurrentBuildNumber        17G12034
macOSProductName               Mac OS X
macOSDarwinVersion             17.7.0
EFIVersion                     87.0.0.0.0
SMCVersion                     1.70f6
HardwareSerialNumber           ***************
HardwareUUID                   ********-****-****-****-************
HardwareModelName              MacBook Pro
HardwareModelID                MacBookPro8,3
CPUArchitecture                x86_64
CPUName                        Intel Core i7
CPUSpeed                       2.2 GHz
CPUCount                       1
CPUCoreCount                   4
CPUL2CacheSize                 256 KB
CPUBrandString                 Intel(R) Core(TM) i7-2720QM CPU @ 2.20GHz
L3CacheSize                    6 MB
RAMAmount                      16 GB
AppMemoryUsedGB                9.9869
VMPageFile                     /private/var/vm/swapfile
VMSwapInUseGB                  1.5210
BootDevice                     /dev/disk1s1
FileVaultStatus                Off
EFICurrentLanguage             English (United States)
DSTStatus                      True
TimeZone                       America/New_York
UTCOffset                      -05:00:00
DNSHostName                    ********.local
LocalHostName                  *********
NetworkServiceList             Ethernet, iPhone USB, Wi-Fi, iPad USB, FireWire, Bluetooth PAN, Thunderbolt Bridge
CurrentUserName                ******
CurrentUserUID                 ******
CurrentDateTime                5/20/2020 8:42:19 PM
LastBootDateTime               May 7 17:26
Uptime                         13.03:16:50

If you just want one or more of the parameters, then you’d supply those as a comma-delimited list, i.e.:

./Get-MacInfo.ps1 RAMAmount,NetworkServiceList,macOSCurrentBuildNumber                
Name                          Value                                                                                               
----                          -----                                                                                               
RAMAmount                     16 GB                                                                                               
NetworkServiceList            Ethernet, iPhone USB, Wi-Fi, iPad USB, FireWire, Bluetooth PAN, Thunderbolt Bridge                  
macOSCurrentBuildNumber       17G12034                     

The script itself takes about a second or two to run, as it collects all the info first, then displays what you want. Yes, that’s wasteful to some, but a) takes less than two seconds to run on ten-year-old gear and b) I’m lazy. You’re welcome to improve on it.

Actually, that last part is serious. I didn’t get every possible parameter, i can’t think of them all. The script itself is extensively commented, so even a novice should be able to figure out what’s going on and add to it. I’m also not that clever with output formatting as you can see from the code.

Anyway, now that I have the parameter input done, my next step is to turn it into a proper PowerShell module so you can incorporate it into your system. Hopefully, that will be sooner than later, and I’ll try to put a post up about it when I can.

In terms of using PowerShell, as a language, it’s really very nice. I like it better than most, it’s really easy to get up and running quickly and there is a LOT of documentation and support from both Microsoft and third-parties. (HEY APPLE! YOU SHOULD THINK ABOUT STEALING HOW MICROSOFT DOCUMENTS THEIR AUTOMATION STUFF. IT’S REALLY USEFUL. HINT!!!!)

Visual Studio Code (VSC) is a really nice dev environment for the Mac, and you really only have to install like two extensions to get it working well with PowerShell and GitHub, specifically “PowerShell” and “GitHub Pull Requests and Issues”. Both are easy to use and in conjunction with VSC give you most of the tools you need to get things done.

My biggest complaint about PowerShell is the complete lack of UI primitives, like simple dialog boxes and lists. Building those without external modules is just ridiculously tedious and kind of inexcusable. Come on Microsoft, AppleScript has had “Display Dialog” and “Choose from List” for how many decades? This is just silly that a modern language doesn’t allow you to create simple UI elements without a gob of code setup.

My second biggest is that its OS integration on macOS is pants. I mean, look at the reason for this post. What would be neat, and probably doable, instead of trying to replicate Apple’s existing scripting implementations in PowerShell would be for the PowerShell team to just build an event broker that could take Commands from PowerShell, spit out Apple Events to the OS/other applications and return the results to the script. I’m not saying that’s easy but over time, it’s probably less work to build a daemon that does that instead of trying to replicate many decades of work in terms of OSA languages.

Either way, Powershell on macOS is quite usable, and y’all should give it a try. The instructions for doing so are here, and you can do it via either Homebrew, or just downloading the installer from MS.