Skip to content

Commit

Permalink
Merge pull request #5 from UNT-CAS/4-the-greater-than-sign
Browse files Browse the repository at this point in the history
Added IsCurrentSession
  • Loading branch information
VertigoRay authored Apr 27, 2019
2 parents 856804e + 5d668a4 commit 971d1dc
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 76 deletions.
Binary file added Examples/Culture.es-EC.xml
Binary file not shown.
92 changes: 53 additions & 39 deletions QuserObject/Private/ConvertTo-QuserObject.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
.Parameter QuserOutput
This is the STDOUT as returned by `quser.exe`.
A hashtable of the target server and the STDOUT (Result) as returned by `quser.exe`.
#>
function ConvertTo-QuserObject {
[CmdletBinding()]
Expand All @@ -29,65 +29,79 @@ function ConvertTo-QuserObject {
Write-Debug "[QuserObject ConvertTo-QuserObject] Process Bound Parameters: $($MyInvocation.BoundParameters | ConvertTo-Json)"
Write-Debug "[QuserObject ConvertTo-QuserObject] Process Unbound Parameters: $($MyInvocation.UnboundParameters | ConvertTo-Json)"

if ($script:Culture.Parent.Name -and $script:Culture.Parent -eq 'es') {
Write-Debug "[QuserObject ConvertTo-QuserObject] Culture Adjustements: ${script:Culture}"
Write-Debug "[QuserObject ConvertTo-QuserObject] Culture ($(Get-Culture)) DateTime Format: $((Get-Culture).DateTimeFormat.ShortDatePattern)"
if ((Get-Culture).Parent.Name -eq 'es') {
Write-Debug "[QuserObject ConvertTo-QuserObject] Culture Adjustments: ${script:Culture}"
$QuserOutput.Result[0] = $QuserOutput.Result[0].Replace('.', ' ')
}

Write-Debug "[QuserObject ConvertTo-QuserObject] QuserOutput.Result: $($QuserOutput.Result | Out-String)"

((($QuserOutput.Result) -replace '^>', '') -replace '\s{2,}', ',').Trim() | ForEach-Object {
$quserCsv = (($QuserOutput.Result) -replace '\s{2,}', ',').Trim() | ForEach-Object {
Write-Debug "[QuserObject ConvertTo-QuserObject] Add Comma, if needed: $_"
if ($_.Split(',').Count -eq 5) {
Write-Output ($_ -replace '(^[^,]+)', '$1,')
} else {
Write-Output $_
}
} | ForEach-Object {
Write-Debug "[QuserObject ConvertTo-QuserObject] Process Row: $_"
$rowParts = $_.Split(',')
if (-not $headerDone) {
Write-Debug "[QuserObject ConvertTo-QuserObject] Skipping Header Row"
$headerDone = $true
} else {
# IdleTime
$getQuserIdleTime = @{
QuserIdleTime = $rowParts[4]
AsDateTime = $script:IdleStartTime
}
}

$quserCsvHeader = $quserCSV[0].Split(',').Trim()
Write-Debug "[QuserObject ConvertTo-QuserObject] Header Row: $($userCsvHeader -join ', ')"

$quserObj = $quserCsv | ConvertFrom-Csv

if (($quserObj | Measure-Object).Count -eq 1) {
$quserObj = ,$quserObj
}

foreach ($row in $quserObj) {
Write-Debug "[QuserObject ConvertTo-QuserObject] Process Row: ${row}"

# IdleTime
$getQuserIdleTime = @{
QuserIdleTime = $row.($quserCsvHeader[4])
AsDateTime = $script:IdleStartTime
}

Write-Debug "[QuserObject ConvertTo-QuserObject] Processed Row: $($rowParts -join ',') "
Write-Debug "[QuserObject ConvertTo-QuserObject] Pre Output ($(($_ | Measure-Object).Count)): $($_ | Out-String)"
Write-Debug "[QuserObject ConvertTo-QuserObject] Processing Row: $($row | Out-String) "
Write-Debug "[QuserObject ConvertTo-QuserObject] Pre Output ($(($row | Measure-Object).Count)): $($row | Out-String)"

$output = @{}
$output = @{}

Write-Debug "[QuserObject ConvertTo-QuserObject] Server: $($QuserOutput.Server)"
$output.Add('Server', ($QuserOutput.Server))
Write-Debug "[QuserObject ConvertTo-QuserObject] Server: $($QuserOutput.Server)"
$output.Add('Server', ($QuserOutput.Server))

Write-Debug "[QuserObject ConvertTo-QuserObject] Username: $(([string] $rowParts[0]))"
$output.Add('Username', ([string] $rowParts[0]))
Write-Debug "[QuserObject ConvertTo-QuserObject] Username: $(([string] $row.($quserCsvHeader[0])))"
[string] $username = $row.($quserCsvHeader[0])

Write-Debug "[QuserObject ConvertTo-QuserObject] Sessionname: $(([string] $rowParts[1]))"
$output.Add('Sessionname', ([string] $rowParts[1]))
if ($username.StartsWith('>')) {
$output.Add('Username', $username.Substring(1))
$output.Add('IsCurrentSession', $true)
} else {
$output.Add('Username', $username)
}

Write-Debug "[QuserObject ConvertTo-QuserObject] Sessionname: $(([string] $row.($quserCsvHeader[1])))"
$output.Add('Sessionname', ([string] $row.($quserCsvHeader[1])))

Write-Debug "[QuserObject ConvertTo-QuserObject] Id: $(([int] $rowParts[2]))"
$output.Add('Id', ([int] $rowParts[2]))
Write-Debug "[QuserObject ConvertTo-QuserObject] Id: $(([int] $row.($quserCsvHeader[2])))"
$output.Add('Id', ([int] $row.($quserCsvHeader[2])))

Write-Debug "[QuserObject ConvertTo-QuserObject] State: $(([string] $rowparts[3]))"
$output.Add('State', ([string] $rowparts[3]))
Write-Debug "[QuserObject ConvertTo-QuserObject] State: $(([string] $row.($quserCsvHeader[3])))"
$output.Add('State', ([string] $row.($quserCsvHeader[3])))

$quserIdleTime = Get-QuserIdleTime @getQuserIdleTime
Write-Debug "[QuserObject ConvertTo-QuserObject] IdleTime: ${quserIdleTime}"
$output.Add('IdleTime', $quserIdleTime)
$quserIdleTime = Get-QuserIdleTime @getQuserIdleTime
Write-Debug "[QuserObject ConvertTo-QuserObject] IdleTime: ${quserIdleTime}"
$output.Add('IdleTime', $quserIdleTime)

Write-Debug "[QuserObject ConvertTo-QuserObject] LogonTime: $(([DateTime] $rowparts[5]))"
$output.Add('LogonTime', ([DateTime] $rowparts[5]))
Write-Debug "[QuserObject ConvertTo-QuserObject] LogonTime: $(Get-Date $row.($quserCsvHeader[5]))"
$output.Add('LogonTime', (Get-Date $row.($quserCsvHeader[5])))

$newObject = New-Object PSObject -Property $output
$newObject.PSTypeNames.Insert(0, 'QuserObject')
Write-Debug "[QuserObject ConvertTo-QuserObject] Output ($(($_ | Measure-Object).Count)): $($output | Out-String)"
Write-Output $newObject
}
$newObject = New-Object PSObject -Property $output
$newObject.PSTypeNames.Insert(0, 'QuserObject')
Write-Debug "[QuserObject ConvertTo-QuserObject] Output ($(($row | Measure-Object).Count)): $($output | Out-String)"
Write-Output $newObject
}
}
}
19 changes: 8 additions & 11 deletions QuserObject/Private/Invoke-Quser.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ function Invoke-Quser {
Write-Debug "[QuserObject Invoke-Quser] Process Bound Parameters: $($MyInvocation.BoundParameters | ConvertTo-Json)"
Write-Debug "[QuserObject Invoke-Quser] Process Unbound Parameters: $($MyInvocation.UnboundParameters | ConvertTo-Json)"

$quser = '{0} /SERVER:{1}' -f (Get-Command 'quser').Path, $Server
Write-Debug "[QuserObject Invoke-Quser] QUSER Command: ${quser}"

if ((Get-Culture) -ne 'en-US') {
$currentCulture = Get-Culture
Set-Culture -CultureInfo 'en-US'
if ($Server -eq 'localhost') {
$cmd = '{0}'
} else {
$cmd = '{0} /SERVER:{1}'
}

$quser = $cmd -f (Get-Command 'quser').Path, $Server
Write-Debug "[QuserObject Invoke-Quser] QUSER Command: ${quser}"

try {
$result = (Invoke-Expression $quser) 2>&1
Expand All @@ -40,14 +41,10 @@ function Invoke-Quser {
}
Write-Verbose "[QuserObject Invoke-Quser] QUSER Result (ExitCode: ${LASTEXITCODE}):`n$($result | Out-String)"

if ($currentCulture) {
$currentCulture | Set-Culture
}

if ($LASTEXITCODE -eq 0) {
Write-Output @{
Server = $Server
Result = $result
Result = $result
}
} else {
$message = if ($result.Exception) { $result.Exception.Message } else { $result }
Expand Down
34 changes: 18 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ The parsing has already been done for you:
```powershell
PS > Get-Quser
Server : localhost
Username : vertigoray
Sessionname : console
Id : 1
State : Active
IdleTime : 03:11:00
LogonTime : 7/26/2018 7:29:00 PM
Server : localhost
Username : vertigoray
IsCurrentSession : True
Sessionname : console
Id : 1
State : Active
IdleTime : 03:11:00
LogonTime : 7/26/2018 7:29:00 PM
```

## QuserObject Types
Expand All @@ -56,15 +57,16 @@ This output is a `[PSObject]`, and can be used programmatically.
Here's an example that shows you the types of each returned property:

```powershell
PS > Get-Quser | %{ $_.PSObject.Properties | %{ "{0,15}: [{1,-15}] {2}" -f $_.Name, $_.Value.GetType().FullName, $_.Value } }
Server: [System.String ] localhost
Username: [System.String ] vertigoray
Sessionname: [System.String ] console
Id: [System.Int32 ] 1
State: [System.String ] Active
IdleTime: [System.TimeSpan] 03:11:00
LogonTime: [System.DateTime] 7/26/2018 7:29:00 PM
PS > Get-Quser | %{ $_.PSObject.Properties | %{ "{0,16}: [{1,-15}] {2}" -f $_.Name, $_.Value.GetType().FullName, $_.Value } }
Server: [System.String ] localhost
Username: [System.String ] vertigoray
IsCurrentSession: [System.Boolean ] True
Sessionname: [System.String ] console
Id: [System.Int32 ] 1
State: [System.String ] Active
IdleTime: [System.TimeSpan] 03:11:00
LogonTime: [System.DateTime] 7/26/2018 7:29:00 PM
```

## `Get-LoggedOnUsers` Alias
Expand Down
18 changes: 8 additions & 10 deletions Tests/ConvertTo-QuserObject.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,18 @@ function Get-QuserIdleTime {

Describe $testFile.Name {
foreach ($test in $tests) {
[hashtable] $parameters = $test.Parameters

if (-not $test.Culture) {
$test.Culture = 'en'
$test.Culture = 'en-US'
}

$script:Culture = [System.Globalization.CultureInfo]::GetCultureInfo($test.Culture)
if (-not ($script:CultureText = Import-LocalizedData -UICulture $script:Culture -BaseDirectory $projectDirectory -FileName 'culture.psd1' -ErrorAction SilentlyContinue)) {
$script:Culture = [System.Globalization.CultureInfo]::GetCultureInfo('en')
$script:CultureText = Import-LocalizedData -UICulture $script:Culture -BaseDirectory $projectDirectory -FileName 'culture.psd1'
Write-Host "`tCulture: $(Get-Culture)" -ForegroundColor Cyan
Write-Host "`tTest Culture: $($test.Culture)" -ForegroundColor Cyan

if ((Get-Culture) -ne $test.Culture) {
break
}

[hashtable] $parameters = $test.Parameters

Context "$($test.Name) Parameters" {
BeforeEach {
Mock Get-QuserIdleTime {
Expand All @@ -84,7 +84,6 @@ Describe $testFile.Name {
}

$script:results = $null
Write-Host "`tCulture: ${script:Culture}"

It "ConvertTo-QuserObject Parameter" {
# $DebugPreference = 'Continue'
Expand Down Expand Up @@ -161,7 +160,6 @@ Describe $testFile.Name {
}

$script:results = $null
Write-Host "`tCulture: ${script:Culture}"

It "ConvertTo-QuserObject Pipeline" {
{ $script:results = $parameters.QuserOutput | ConvertTo-QuserObject } | Should Not Throw
Expand Down
4 changes: 4 additions & 0 deletions Tests/cultures.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
"en-US",
"es-EC"
]

0 comments on commit 971d1dc

Please sign in to comment.