-1

The goal : get all logged in / logged out users from the system. Those users who logged in / logged out by using remote desktop connection.

My script :

Param(
    [array]$ServersToQuery = (hostname),
    [datetime]$StartTime = "January 1, 1970"
)

    foreach ($Server in $ServersToQuery) {

        $LogFilter = @{
            LogName = 'Microsoft-Windows-TerminalServices-LocalSessionManager/Operational'
            ID = 21, 23, 24, 25
            StartTime = $StartTime
            }

        $AllEntries = Get-WinEvent -FilterHashtable $LogFilter -ComputerName $Server

        $AllEntries | Foreach { 
            $entry = [xml]$_.ToXml()
            [array]$Output += New-Object PSObject -Property @{
                TimeCreated = $_.TimeCreated
                User = $entry.Event.UserData.EventXML.User
                IPAddress = $entry.Event.UserData.EventXML.Address
                EventID = $entry.Event.System.EventID
                ServerName = $Server
                }        
            } 

    }

    $FilteredOutput += $Output | Select TimeCreated, User, ServerName, IPAddress, @{Name='Action';Expression={
                if ($_.EventID -eq '21'){"logon"}
                if ($_.EventID -eq '22'){"Shell start"}
                if ($_.EventID -eq '23'){"logoff"}
                if ($_.EventID -eq '24'){"disconnected"}
                if ($_.EventID -eq '25'){"reconnection"}
                }
            }

    $Date = (Get-Date -Format s) -replace ":", "."
    $FilePath = "$env:USERPROFILE\Desktop\$Date`_RDP_Report.csv"
    $FilteredOutput | Sort TimeCreated | Export-Csv $FilePath -NoTypeInformation

Write-host "Writing File: $FilePath" -ForegroundColor Cyan
Write-host "Done!" -ForegroundColor Cyan


#End

I really do not understand ps1 scripts. I've found this script but i want to use it for my purposes.

When i try to execute it with c# :

Scenario 1 :

string scriptText = "C:\\MyPath\\script.ps1";
            try
            {
                Runspace runspace = RunspaceFactory.CreateRunspace();

                // open it

                runspace.Open();

                // create a pipeline and feed it the script text

                Pipeline pipeline = runspace.CreatePipeline();
                pipeline.Commands.AddScript(scriptText);
  Collection<PSObject> results = pipeline.Invoke();

It throws an error :

ps1 is not digitally signed.

Second scenario :

using (PowerShell PowerShellInstance = PowerShell.Create())
            {
                PowerShellInstance.AddScript(str_Path);
                Collection<PSObject> PSOutput = PowerShellInstance.Invoke();

                if (PowerShellInstance.Streams.Error.Count > 0)
                {
                    // error records were written to the error stream.
                    // do something with the items found.
                }
              }

Streams are always empty. Actually it always count 0 rows.

Any idea/suggestion how to get this done ?

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
SAFF
  • 1
  • 6

1 Answers1

0

I used Write-Output instead of Write-Host into the end of the script without generating csv file. Credits to this.

SAFF
  • 1
  • 6