Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Get Common Data

Posted on April 18, 2011April 25, 2013

While judging entries in this year's Scripting Games I realized there were some common properties that were repeatedly used. This got me thinking about a simple way to retrieve that information with a single command Then you could access the data values from within your script. I've put together a relatively simply function called Get-Common that you might find helpful.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

There's nothing fancy about this function.

Function Get-Common {

[cmdletbinding()]

Param()

Set-StrictMode -Version Latest

Write-Verbose "Starting $($myinvocation.mycommand)"

#test if user is an administrator
Write-Verbose "Testing for admin rights"
$currentUser = [Security.Principal.WindowsIdentity]::GetCurrent()
$IsAdmin=(New-Object Security.Principal.WindowsPrincipal $currentUser).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)

Write-Verbose "Get Username"
#get username from the environmental variable
$username=$env:username

Write-Verbose "Get Computername"
#get computername from the environmental variable
$computername=$env:computername

Write-Verbose "Get Domain name"
#get domain name from the environmental variable
$domain=$env:userdomain

Write-Verbose "Get DNS Domain name"
#get domain DNS name from the environmental variable
#this may not be found on older computers
if ($env:userDNSDomain)
{
$dnsDomain=$env:userdnsdomain
}
else
{
$dnsDomain=$Null
}
Write-Verbose "Get Operating System"
#get operating system from WMI
$os=Get-WmiObject -Class Win32_OperatingSystem

Write-Verbose "Get last boot up time"
#use the WMI ConvertToDateTime method
$boot=$os.ConvertToDateTime($os.LastBootUpTime)

Write-Verbose "Write object"
#write custom object to the pipeline
New-Object -TypeName PSObject -Property @{
Username=$username
Computername=$computername
IsAdmin=$IsAdmin
Domain=$domain
DNSDomain=$DNSDomain
OperatingSystem=$os.caption
LastBoot=$boot
PSHost=$host.name
PSVersion=$host.version
}

Write-Verbose "Ending $($myinvocation.mycommand)"

} #end function

I gathers a number of what I think of as common values such as username, computername, PowerShell host and whether the user is running as an administrator. The function writes a custom object to the pipeline, using New-Object.

PS S:\> get-common

Computername : SERENITY
Username : Jeff
LastBoot : 4/15/2011 2:34:19 PM
IsAdmin : True
DNSDomain :
OperatingSystem : Microsoft Windows 7 Ultimate
PSHost : ConsoleHost
Domain : SERENITY
PSVersion : 2.0

Assuming you have dot sourced the function, you could use it in your script like this:

$data=Get-Common

if ($data.IsAdmin)
{
$wsmanPort=(dir WSMan:\localhost\Listener -rec | where {$_.name -eq "Port"}).value

$uptime=(Get-Date) - $data.lastBoot
if ($data.OperatingSystem -match "Windows 7")
{
$Win7=$True
}
else
{
$Win7=$False
}
New-Object -TypeName PSObject -Property @{
Computername=$data.computername
Uptime=$uptime
IsWin7=$Win7
WSManPort=$wsmanPort
}

}
else
{
Write-Warning "You must be an administrator"
}

Maybe this will help simplify your scripts or maybe it won't. Of course you can easily extend it to include other core or common data you use frequently in your scripts. If there's something you add, I'm curious to learn what it is.

Download Get-Common.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

1 thought on “Get Common Data”

  1. James Tryand says:
    April 18, 2011 at 10:09 am

    Nice,
    Perhaps this stuff would make good candidates to go into the V3 shells. ( tho not necessarly the runspaces cos i imagine it would be overkill there. )

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d