I wanto to track PowerShell commands which are executed by users in the intranet. How can I do this?
I need the user's information and their executed commands. Is it possible?
Edit 1: I guess I can use;
Set-PSDebug -Trace 1
How can I build a script which I then can deploy over whole intranet. I also use an orchestrator.
Edit 2: I tried;
$created = Get-WinEvent -FilterHashtable @{ ProviderName="Microsoft-Windows-PowerShell"; Id = 4104 } #| Where-Object { $_.<...> }
$sortedScripts = $created | sort { $_.Properties[0].Value }
$mergedScript = -join ($sortedScripts | % { $_.Properties[2].Value })
And got this output;
DEBUG: 1+ >>>> $created = Get-WinEvent -FilterHashtable @{ ProviderName="Microsoft-Windows-PowerShell"; Id = 4104 }# | Where-Object { $_.<...> }
DEBUG: 2+ >>>> $sortedScripts = $created | sort { $_.Properties[0].Value }
DEBUG: 2+ $sortedScripts = $created | sort >>>> { $_.Properties[0].Value }
DEBUG: 2+ $sortedScripts = $created | sort { >>>> $_.Properties[0].Value }
DEBUG: 2+ $sortedScripts = $created | sort { $_.Properties[0].Value >>>> }
DEBUG: 2+ $sortedScripts = $created | sort >>>> { $_.Properties[0].Value }
DEBUG: 2+ $sortedScripts = $created | sort { >>>> $_.Properties[0].Value }
DEBUG: 2+ $sortedScripts = $created | sort { $_.Properties[0].Value >>>> }
DEBUG: 2+ $sortedScripts = $created | sort >>>> { $_.Properties[0].Value }
DEBUG: 2+ $sortedScripts = $created | sort { >>>> $_.Properties[0].Value }
DEBUG: 2+ $sortedScripts = $created | sort { $_.Properties[0].Value >>>> }
DEBUG: 2+ $sortedScripts = $created | sort >>>> { $_.Properties[0].Value }
DEBUG: 2+ $sortedScripts = $created | sort { >>>> $_.Properties[0].Value }...
I did comment pipeline codes because I couldn't get "$_.<...>" parameter.
Edit 3: This works perfectly; Firstly I wrote Microsoft's ScriptBlock Enable Code Block
function Enable-PSScriptBlockLogging {
[CmdletBinding()]
param ()
$BasePath = "HKLM:\Software\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging"
if (-not (Test-Path $BasePath)) {
Write-Verbose "ScriptBlockLogging registry key doesn't exist. Creating now."
$null = New-Item $BasePath –Force
Write-Verbose "Setting registry key value to 1 of type DWORD."
$null = New-ItemProperty $BasePath -Name EnableScriptBlockLogging -Value "1" -PropertyType DWORD
} else {
if ((Get-ItemProperty -Path $BasePath).EnableScriptBlockLogging.getType().Name -eq 'Int32') {
Write-Verbose "Key exists, updating value to 1."
Set-ItemProperty $BasePath -Name EnableScriptBlockLogging -Value "1"
} else {
Write-Verbose "Key exists of wrong data type, removing existing entry."
Remove-ItemProperty $BasePath -Name EnableScriptBlockLogging
Write-Verbose "Setting new registry key value to 1 of type DWORD."
$null = New-ItemProperty $BasePath -Name EnableScriptBlockLogging -Value "1" -PropertyType DWORD
}
}
}
Then, when I open Event Viewer GOTCHA! Logs see perfectly. Now I need manage these logs with PowerShell.