Functions/Get-PSWho.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
function Get-PSWho { <# .SYNOPSIS Get PowerShell user summary information .DESCRIPTION This command will provide a summary of relevant information for the current user in a PowerShell session. You might use this to troubleshoot an end-user problem running a script or command. The default behavior is to write an object to the pipeline, but you can use the -AsString parameter to force the command to write a string. This makes it easier to use in your scripts with Write-Verbose. .PARAMETER AsString Write the summary object as a string. .EXAMPLE PS C:\> Get-PSWho User : BOVINE320\Jeff Elevated : True Computername : BOVINE320 OperatingSystem : Microsoft Windows 10 Pro [64-bit] OSVersion : 10.0.16299 PSVersion : 5.1.16299.64 Edition : Desktop PSHost : ConsoleHost WSMan : 3.0 ExecutionPolicy : RemoteSigned Culture : en-US .EXAMPLE PS /mnt/c/scripts> get-pswho User : jhicks Elevated : NA Computername : Bovine320 OperatingSystem : Linux 4.4.0-43-Microsoft #1-Microsoft Wed Dec 31 14:42:53 PST 2014 OSVersion : Ubuntu 16.04.3 LTS PSVersion : 6.0.0-rc Edition : Core PSHost : ConsoleHost WSMan : 3.0 ExecutionPolicy : Unrestricted Culture : en-US .EXAMPLE PS C:\Program Files\PowerShell\6.0.0-rc> get-pswho User : BOVINE320\Jeff Elevated : True Computername : BOVINE320 OperatingSystem : Microsoft Windows 10 Pro [64-bit] OSVersion : 10.0.16299 PSVersion : 6.0.0-rc Edition : Core PSHost : ConsoleHost WSMan : 3.0 ExecutionPolicy : RemoteSigned Culture : en-US .EXAMPLE PS C:\> Get-PSWho -asString | Set-Content c:\test\who.txt .NOTES Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/ .INPUTS none .OUTPUTS [pscustomboject] [string] .LINK Get-CimInstance .LINK Get-ExecutionPolicy .LINK $PSVersionTable .LINK $Host #> # todo - add Credential [CmdletBinding()] Param( [switch] $AsString ) begin { Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]" } process { if ($PSVersionTable.PSEdition -eq 'desktop' -OR $PSVersionTable.OS -match 'Windows') { #get some basic information about the operating system $cimos = Get-CimInstance -classname win32_operatingsystem -Property Caption, Version, OSArchitecture $os = "$($cimos.Caption) [$($cimos.OSArchitecture)]" $osver = $cimos.Version #determine the current user so we can test if the user is running in an elevated session $current = [Security.Principal.WindowsIdentity]::GetCurrent() $principal = [Security.Principal.WindowsPrincipal] $current $Elevated = $principal.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) $user = $current.Name $computer = $env:COMPUTERNAME } else { #non-Windows values $os = $PSVersionTable.OS $lsb = lsb_release -d $osver = ($lsb -split ':')[1].Trim() $elevated = 'NA' $user = $env:USER $computer = $env:NAME } #object properties will be displayed in the order they are listed here $who = [pscustomObject]@{ User = $user Elevated = $elevated Computername = $computer OperatingSystem = $os OSVersion = $osver PSVersion = $PSVersionTable.PSVersion.ToString() Edition = $PSVersionTable.PSEdition PSHost = $host.Name WSMan = $PSVersionTable.WSManStackVersion.ToString() ExecutionPolicy = (Get-ExecutionPolicy) Culture = $host.CurrentCulture } if ($AsString) { $who | Out-String } else { $who } } end { Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]" } } |