Inspecting The Bot Run Environment

When running a bot on an On-Prem Proxy sometimes its results may differ from those when running the same Powershell interactively in ISE on the same machine - especially with respect to installed/available modules. Often this comes down to differences in the Powershell session environment for the identity under which the bot is running.

The below script can be run in a bot with a “Windows System” type ConnectId set as the RunAs. The same script can be run directly in Windows Powershell ISE. The output of the two can then be compared for differences to assist in debugging any issues.

Most commonly the issue is due to either:

  • the $Env:PsModulePath of the bot "identity" not including the necessary directory(ies); or,

  • the needed module not having been installed with -Scope AllUsers

$VerbosePreference="Continue" Write-Verbose '---- CURRENT USER INFORMATION ----' [Security.Principal.WindowsIdentity]::GetCurrent() Write-Verbose '---- ENVIRONMENT INFORMATION ----' @{path=$env:Path;pathExt=$env:PathExt;PsModulePath=$env:PSModulePath;execPolicy=Get-ExecutionPolicy} Write-Verbose '---- GET-MODULE INFORMATION ----' Get-Module -listavailable|Select-Object -property name,@{n='Version';e={$_.version.ToString()}},path Write-Verbose '---- GET-INSTALLED MODULE INFORMATION ----' Get-InstalledModule|Select-Object -property name,@{n='Version';e={$_.version.ToString()}},installedlocation Write-Verbose '---- DIR C:\Program Files\WindowsPowerShell\Modules\ ----' dir 'C:\Program Files\WindowsPowerShell\Modules\'|Select-Object -property BaseName,FullName,Attributes Write-Verbose '---- Get-Disk ----' Get-Disk|Select-Object -property SerialNumber Write-Verbose '---- .Net Version ----' Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -Recurse | Get-ItemProperty -Name version -EA 0 | Where { $_.PSChildName -Match '^(?!S)\p{L}'} | Select PSChildName, version Write-Verbose '---- Proxy PVersion ----' $ctlrFilePath='C:\Program Files\UCClearly\UcClearly.ProxyController\UcClearly.ProxyController.exe' (Get-Command $ctlrFilePath).FileVersionInfo Write-Verbose '---- Memory In Use % ----' $CompObject = Get-WmiObject -Class WIN32_OperatingSystem @{MemoryUsePercent=((($CompObject.TotalVisibleMemorySize - $CompObject.FreePhysicalMemory)*100)/ $CompObject.TotalVisibleMemorySize)} Write-Verbose '---- Readibots Processes Memory Use (MB) ----' Get-WmiObject WIN32_PROCESS | Where-Object {$_.ProcessName -like 'UcClearly*'} | Select-Object -first 5 processname, @{Name="Mem Usage(MB)";Expression={[math]::round($_.ws / 1mb)}} write-Verbose '---- END OF CHECKS ----'