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

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

Viewing 1 post (of 1 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
    }
    }
    }

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.