#Requires -version 2.0 # ----------------------------------------------------------------------------- # Script: Get-Common.ps1 # Version: 1.0 # Author: Jeffery Hicks # http://jdhitsolutions.com/blog # http://twitter.com/JeffHicks # Date: 4/15/2011 # Keywords: WMI, Operating System, Environmental Variables # Comments: # # Get an object with commonly referenced values # # "Those who forget to script are doomed to repeat their work." # # **************************************************************** # * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * # * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * # * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * # * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * # **************************************************************** # ----------------------------------------------------------------------------- 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