Announcement

Collapse
No announcement yet.

processing quser output and change '.' to '0' in output file

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • processing quser output and change '.' to '0' in output file

    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
        }
        }
        }
    Last edited by Rems; 5th March 2014, 20:38. Reason: changed [quote]-tag to [code]-tags

  • #2
    Re: processing quser output and change '.' to '0' in output file

    Ive tried adding the following

    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

    Comment


    • #3
      Re: processing quser output and change '.' to '0' in output file

      You can use the -Replace operator

      Code:
      $ComputerName = @("TST-SVR-001", "TST-SVR-003")
      
      
      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] -Replace '\.',0
                          $HashProps.LogonTime = $CurrentLine[4..6] -join ' '
                  } else {
                          $HashProps.SessionName = $CurrentLine[1]
                          $HashProps.Id = $CurrentLine[2]
                          $HashProps.State = $CurrentLine[3]
                          $HashProps.IdleTime = $CurrentLine[4] -Replace '\.',0
                          $HashProps.LogonTime = $CurrentLine[5..7] -join ' '
                  }
      
                  New-Object -TypeName PSCustomObject -Property $HashProps |
                  Select-Object -Property UserName,ComputerName,SessionName,Id,State,IdleTime,LogonTime
              }
          }
      }
      /Rems

      This posting is provided "AS IS" with no warranties, and confers no rights.

      __________________

      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts

      Comment

      Working...
      X