r/PowerShell • • Jul 29 '18

Announcing PS Sysadmin Toolkit

Fellow Powershellers!

A few days ago I came up with the idea for module that would be a Toolkit of useful functions for system administrators. I posted a request for ideas over in /r/sysadmin, and the response was fantastic.

I'm happy to report that development is well underway. If you'd like to review the module, you may do so here: https://github.com/steviecoaster/PSSysadminToolkit

If you have an idea for a function that could be added to the module, please file an issue, and if you have code already written, do share if you are able, and I'll get it added in. Rather post it in the issue and I'll handle the rest, or submit a PR for me to merge.

The Master branch is relatively empty at the moment, I've got everything in the Dev branch until things are built enough to warrant a Release, at which time, I'll push to Master.

Any and all feedback is most welcome, and an extra set of eyes/hands from you fine folks is more than welcome.

118 Upvotes

27 comments sorted by

View all comments

Show parent comments

5

u/[deleted] Jul 29 '18

I will admit that one is.....broken. I had it pretty well working, then changed a bunch of stuff and absentmindedly commited it to the repo. Haha.

1

u/Lee_Dailey [grin] Jul 29 '18

howdy steviecoaster,

naughty, naughty! [grin]

if i was going to include connection info, i would likely do it something like this ...

function Get-SystemUpTime_LD
    {
    <#

    Comment Based Help Here

    #>

    [CmdletBinding()]
    Param
        (
        [Parameter (
            Position = 0
            )]
            [string[]]
            $ComputerName = $env:COMPUTERNAME
        )

    begin {}

    process
        {
        foreach ($CN_Item in $ComputerName)
            {
            $Now = Get-Date
            $LastBootUpTime = $UpTime = '__ Not Available __'
            $WSMan_OK = $False

            $Connect_OK = Test-Connection -ComputerName $CN_Item -Count 1 -Quiet
            if ($Connect_OK)
                {
                $WSMan_OK = [bool](Test-WSMan -ComputerName $CN_Item -ErrorAction SilentlyContinue)
                }
            if ($Connect_OK -and $WSMan_OK)
                {
                $GCIMI_Params = @{
                    ClassName = 'CIM_OperatingSystem'
                    ComputerName = $CN_Item
                    }
                $LastBootUpTime = (Get-CimInstance @GCIMI_Params).LastBootUpTime
                }
            if ($LastBootUpTime -is [datetime])
                {
                $UpTime = $Now - $LastBootUpTime
                }


            [PSCustomObject]@{
                ComputerName = $CN_Item
                LastBootUpTime = $LastBootUpTime
                UpTime = $UpTime
                NetConnection_OK = $Connect_OK
                WSMan_OK = $WSMan_OK
                }
            }
        }

    end {}

    } # end >> Get-SystemUpTime_LD

with this call ...

Get-SystemUpTime_LD -ComputerName BetterNotBeThere, LocalHost

... i get this output ...

ComputerName     : BetterNotBeThere
LastBootUpTime   : __ Not Available __
UpTime           : __ Not Available __
NetConnection_OK : False
WSMan_OK         : False

ComputerName     : LocalHost
LastBootUpTime   : 2018-07-13 11:35:31 PM
UpTime           : 14.23:35:07.9854000
NetConnection_OK : True
WSMan_OK         : True

i would almost certainly add the local computer name to go along with the input computer name. that way you would know why it returned one name LocalHost when it really otta have returned the $env:COMPUTERNAME value. [grin]

if the connection info wasn't wanted, then i would likely wrap it around an Invoke-Command call to the CIM stuff and feed that the single/array $ComputerName and let the system run it all in parallel.

take care,
lee

2

u/[deleted] Jul 29 '18

Yeah, I just got done reworking the script. It pretty well does what you are doing here, just different strokes for different folks :)

1

u/Lee_Dailey [grin] Jul 29 '18

howdy steviecoaster,

the different ways folks get to similar goals is one reason i so very much enjoy reading code here. [grin]

take care,
lee