Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

add folder name to filename

$
0
0

I have an amended version of Get-LoggedOnUser.ps1 to get details fo users lconnected to a farm of several terminal servers -

I have the output in a folder name as the yyyymmddhhmm and called servername.txt

the script also outputs the data in csv format, I would like to add the timestamp (foldername) as the first characters in each line in each file - but cannot get it to accept reading in a parameter at the time of running

<#
.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'
    )

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",""
        }

            # 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 UserName,SessionName,Id,State,IdleTime,LogonTime,ComputerName |
    ConvertTo-Csv -NoTypeInformation |
    % { $_ -replace '","', ','} | % { $_ -replace "^`"",''} | % { $_ -replace "`"$",''} |
         select -Skip 1 |
         Out-File -Append $PWD\$Computer.txt
    }
    }
    }

 

I want to pass a parameter $timestamp and

a) add it to the output filename ie $timestamp_$computer.txt

b) add $timestamp, to each line in each fo the files

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles