processing quser output and change ‘.’ to ‘0’ in output file

Home Forums Scripting PowerShell processing quser output and change ‘.’ to ‘0’ in output file

This topic contains 2 replies, has 3 voices, and was last updated by Avatar Scoundrel 5 years, 7 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • Avatar
    nyehus
    Member
    #163301

    I have a modified version of Get-LoggedOnUser.ps1, which takes out put from quser for a server and outputs it as a csv file.

    I have found one of the fields has a ‘.’, I would like to change this to a ‘0’
    then the output file can be uploaded into an oracle database

    but cant work it out as the last part of the script does the converting into CSV

    the field is ‘Idletime’

    Code:
    < # .Synopsis Queries a computer to check for interactive sessions .DESCRIPTION This script takes the output from the quser program and parses this to PowerShell objects .NOTES Copied and modified from script written by Name: Get-LoggedOnUser Author: Jaap Brasser Version: 1.1 DateUpdated: 2013-06-26 .PARAMETER ComputerName The string or array of string for which a query will be executed .EXAMPLE .Get-LoggedOnUser.ps1 -ComputerName server01,server02 Description: Will display the session information on server01 and server02 .EXAMPLE 'server01','server02' | .Get-LoggedOnUser.ps1 Description: Will display the session information on server01 and server02 #>
    param(
    [CmdletBinding()]
    [Parameter(ValueFromPipeline=$true,
    ValueFromPipelineByPropertyName=$true)]
    [string[]]$ComputerName = ‘localhost’,
    [string]$timestamp
    )

    process {
    foreach ($Computer in $ComputerName) {
    quser /server:$Computer | Select-Object -Skip 1 | ForEach-Object {
    $CurrentLine = $_.Trim() -Replace ‘s+’,’ ‘ -Split ‘s’
    $HashProps = @{
    UserName = $CurrentLine[0]
    ComputerName = $Computer -replace “.shareprod.org”,””
    timestamp = $timestamp
    }

    # If session is disconnected different fields will be selected
    if ($CurrentLine[2] -eq ‘Disc’) {
    $HashProps.SessionName = ‘rdp’
    $HashProps.Id = $CurrentLine[1]
    $HashProps.State = $CurrentLine[2]
    $HashProps.IdleTime = $CurrentLine[3]
    $HashProps.LogonTime = $CurrentLine[4..6] -join ‘ ‘
    } else {
    $HashProps.SessionName = $CurrentLine[1]
    $HashProps.Id = $CurrentLine[2]
    $HashProps.State = $CurrentLine[3]
    $HashProps.IdleTime = $CurrentLine[4]
    $HashProps.LogonTime = $CurrentLine[5..7] -join ‘ ‘
    }

    New-Object -TypeName PSCustomObject -Property $HashProps |
    select-Object -Property timestamp,UserName,SessionName,Id,State,IdleTime,LogonTime,ComputerName |
    ConvertTo-Csv -NoTypeInformation |
    % { $_ -replace ‘”,”‘, ‘,’} | % { $_ -replace “^””,”} | % { $_ -replace “”$”,”} |
    select -Skip 1 |
    Out-File -Append $PWD$Computer.csv
    }
    }
    }

    Avatar
    Scoundrel
    Member
    #389776

    Re: processing quser output and change ‘.’ to ‘0’ in output file

    Ive tried adding the following

    Quote:
    if ($CurrentLine[3] -eq ‘.’) {
    $HashProps.SessionName = $CurrentLine[1]
    $HashProps.Id = $CurrentLine[2]
    $HashProps.State = $CurrentLine[3]
    $HashProps.IdleTime = ‘0’
    $HashProps.LogonTime = $CurrentLine[5..7] -join ‘ ‘
    }

    it didnt work

    Rems
    Rems
    Moderator
    #228414

    Re: processing quser output and change ‘.’ to ‘0’ in output file

    You can use the -Replace operator

    Code:
    [COLOR=”blue”][I]$ComputerName = @(“TST-SVR-001”, “TST-SVR-003″)[/I][/COLOR]

    process {
    foreach ($Computer in $ComputerName) {
    quser /server:$Computer | Select-Object -Skip 1 | ForEach-Object {
    $CurrentLine = $_.Trim() -Replace ‘s+’,’ ‘ -Split ‘s’
    $HashProps = @{
    UserName = $CurrentLine[0]
    ComputerName = $Computer
    }

    # If session is disconnected different fields will be selected
    if ($CurrentLine[2] -eq ‘Disc’) {
    $HashProps.SessionName = $null
    $HashProps.Id = $CurrentLine[1]
    $HashProps.State = $CurrentLine[2]
    $HashProps.IdleTime = $CurrentLine[3] [B][COLOR=”RED”]-Replace ‘.’,0[/COLOR][/B]
    $HashProps.LogonTime = $CurrentLine[4..6] -join ‘ ‘
    } else {
    $HashProps.SessionName = $CurrentLine[1]
    $HashProps.Id = $CurrentLine[2]
    $HashProps.State = $CurrentLine[3]
    $HashProps.IdleTime = $CurrentLine[4] [B][COLOR=”RED”]-Replace ‘.’,0[/COLOR][/B]
    $HashProps.LogonTime = $CurrentLine[5..7] -join ‘ ‘
    }

    New-Object -TypeName PSCustomObject -Property $HashProps |
    Select-Object -Property UserName,ComputerName,SessionName,Id,State,IdleTime,LogonTime
    }
    }
    }

    /Rems

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.