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.
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.
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. )