Module shellutils :: Class PowerShell
[hide private]

Class PowerShell

source code

Shell --+    
        |    
 WinShell --+
            |
           PowerShell

Instance Methods [hide private]
 
__init__(self, client, protocolName) source code
 
__getRemoteConsoleEncoding(self) source code
 
__getPowerShellEncoding(self) source code
 
__getEncodingNameByCodePage(self, codePage) source code
 
is64BitMachine(self)
Checks is the discovered machine OS 64 bit.
source code
 
getShellStatusVar(self)
Returns the shell command exit status code
source code
 
getWindowsErrorCode(self)
Get the return code for the latest command execution
source code
 
getCommandSeparator(self)
Get the shell command separator character.
source code
 
__makePowerShellCompatible(self, cmdline)
If passed command is a windows batch command it should be called in batch command interpreter
source code
 
__makePowerShellCompatibleWmicQuery(self, cmdline)
Strip WMIC redirection part
source code
 
__isConsoleCommand(self, cmdline)
Indicates whether passed command line contains command of batch command interpreter
source code
 
__pipeToOutString(self, cmd, lineWidth=80) source code
 
execCmd(self, cmdLine, timeout=0, waitForTimeout=0, useSudo=1, checkErrCode=1, useCache=0, lineWidth=80, pipeToOutString=1)
Execute command in powershell * If current command is cmdlet:
source code
 
execLocalScript(self, path, timeout=0, waitForTimeout=0, useSudo=1, checkErrCode=1, useCache=0)
Execute script on the probe
source code
 
execMultilinedCmd(self, cmdLine, timeout=0, waitForTimeout=0, useSudo=1, checkErrCode=1, useCache=0)
Method intended to execute multiline scripts.
source code
 
_execute(self, cmd)
Template method for derived shells for exact command execution
source code
 
__fixEncoding(self, fromEncoding, toEncoding, targetStr)
Method is intended to fix string encoding.
source code

Inherited from WinShell: closeClient, copyFileFromRemoteShare, copyFileIfNeeded, createDirectoryViaShellCommand, createSystem32Link, deleteDirectoryViaShellCommand, deleteFile, deleteRemoteFileFromShare, fsObjectExists, getCodePage, getShellCmdSeperator, getSystem32DirectoryName, isWinOs, putFile, rebuildPath, removeSystem32Link, safecat, setCodePage

Inherited from Shell: canCopyFile, createOutputHandler, determineOsLanguage, execAlternateCmds, execAlternateCmdsList, execCmdAsBytes, executeCommandAndDecode, executeCommandAndDecodeByMatcher, getCharsetName, getClientType, getCredentialId, getDefaultCommandTimeout, getLastCmdReturnCode, getOsLanguage, getOsType, getOsVersion, getPort, getXML, isWindowsWithCygwin, resolveHost, resolveIp, setSessionLocale, useCharset

Class Variables [hide private]
  PROTOCOL_TYPE = PowerShellClient.POWERSHELL_PROTOCOL_NAME
  __DEFAULT_COMMAND_SEPARATOR = ';'
  __INVOKE_COMMAND_SCRIPT_BLOCK = 'Invoke-Command -ScriptBlock {...
  __INVOKE_COMMAND_LOCAL_SCRIPT = 'Invoke-Command -FilePath %s -...

Inherited from WinShell: DEFAULT_ENGLISH_CODEPAGE, DEFAULT_WIN_SHARE

Inherited from Shell: NO_CMD_RETURN_CODE_ERR_NUMBER

Method Details [hide private]

__init__(self, client, protocolName)
(Constructor)

source code 
Overrides: Shell.__init__

is64BitMachine(self)

source code 

Checks is the discovered machine OS 64 bit. 32 or 64 bit family detected by checking existence of %SystemRoot%\SysWOW64 folder

Overrides: WinShell.is64BitMachine

getShellStatusVar(self)

source code 

Returns the shell command exit status code

Overrides: Shell.getShellStatusVar
(inherited documentation)

getWindowsErrorCode(self)

source code 

Get the return code for the latest command execution

Overrides: WinShell.getWindowsErrorCode
(inherited documentation)

getCommandSeparator(self)

source code 

Get the shell command separator character.

Overrides: Shell.getCommandSeparator

execCmd(self, cmdLine, timeout=0, waitForTimeout=0, useSudo=1, checkErrCode=1, useCache=0, lineWidth=80, pipeToOutString=1)

source code 

Execute command in powershell * If current command is cmdlet:

  1. Method passes output object to 'Out-String' cmdlet to get its string representation.

* If current command is non-cmdlet command(console)

  1. Method prefixes current command with 'cmd.exe /c %cmd%' and no passing to 'Out-String' cmdlet performed. List of console commands is defined at globalSettings.xml file and defaults to ('ver', 'type', 'wmic', 'chcp') if 'consoleCommands' attribute is note set.
Parameters:
  • lineWidth - Attribute of the 'Out-String' cmdlet. From the msdn: Specifies the number of characters in each line of output. Any additional characters are truncated, not wrapped
Returns:
output of the executed shell command
Raises:
  • Exception - Command execution does not produced output nor return code
Overrides: Shell.execCmd

See Also: Shell.execCmd for the param meaning and usage

execMultilinedCmd(self, cmdLine, timeout=0, waitForTimeout=0, useSudo=1, checkErrCode=1, useCache=0)

source code 

Method intended to execute multiline scripts. It's output is not passed to 'Out-String' cmdlet, so all toString conversion should be done manually. Powershell remote console encoding used to decode output Use ';' to separate different commands.

See Also: Shell.execCmd for the param meaning

_execute(self, cmd)

source code 

Template method for derived shells for exact command execution

Raises:
  • Exception - Command produced nor output nor return status
Overrides: Shell._execute

__fixEncoding(self, fromEncoding, toEncoding, targetStr)

source code 

Method is intended to fix string encoding. The root problem comes from execution of console commands through the powershell. There are two variables defining encodings: $OutputEncoding and [Console]::OutputEncoding. The problem is that Exception is raised when one tries to set remote [Console]::OutputEncoding, so when chcp output differes from [Console]::OutputEncoding.CodePage value, we need to fix corrupted string. Fixing is done if passed encodings are different only


Class Variable Details [hide private]

__INVOKE_COMMAND_SCRIPT_BLOCK

Value:
'Invoke-Command -ScriptBlock {%s ;$?} -Session $'+ PowerShellAgent.REM\
OTE_SESSION_IDENTIFIER

__INVOKE_COMMAND_LOCAL_SCRIPT

Value:
'Invoke-Command -FilePath %s -Session $'+ PowerShellAgent.REMOTE_SESSI\
ON_IDENTIFIER+ '; $?'