Skip to content

Instantly share code, notes, and snippets.

@1RedOne
Last active August 29, 2015 14:24
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save 1RedOne/e2a89f1a2ec5413d2c37 to your computer and use it in GitHub Desktop.
PowerShell Puzzles
<#
Write a one-liner that produces the following output (note that property values will be different from computer to computer; that’s fine).
PSComputerName ServicePackMajorVersion Version· BIOSSerial
-------------- ----------------------- -------· ----------
win81· · · · · · · · · · · · · · · · 0 6.3.9600 VMware-56 4d 09 1 71 dd a9 d0 e6 46 9f
By definition, a one-liner is a single, long command or pipeline that you type, hitting Enter only at the very end. If it wraps to more than one physical line as you’re typing, that’s OK. But, in order to really test your skill with the parser, try to make your one-liner as short as technically possible while still running correctly.
#>
<#This is the shortest thing I could come up with. I liked the idea of querying for two different classes,
for the difficulty involved I liked the curve ball of making sure it supported remote PCs too. The way I
handled that was to prompt using read-host, for you to provie your own and remote PCs. By default, PowerShell
didn't like it when I typed localhost,localhost, probably because it interpreted this as a host
with a super long name, containing a comma. I didn't want the end user to have to type "Localhost","remotehost"
with commas as an array,so instead I placed a .Split() at the end of the input,
to allow you to provide your PC names in either format.
To pass the PC name down to the second WMI query, I used -PipelineVariable to store the results of the first query,
so I could get the .CSname property, which contains the name of the PC queried. This way, you could get answers from multiple PCs.
Hope you liked it!
#>
gwmi win32_operatingSystem -Comp (read-host "pc to query?") -PV PC|
select CSName,ServicePackMajorVersion,Version,`
@{Name=‘BIOSSerial‘;Expression={(gwmi win32_bios -Comp $pc.CSname).SerialNumber}}
>pc to query?: localhost,localhost
>CSName ServicePackMajorVersion Version BIOSSerial
>------ ----------------------- ------- ----------
>DELLBOOK 0 10.0.10130 JGT3Q12
>DELLBOOK 0 10.0.10130 JGT3Q12
>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment