Forum Discussion
Jason_44516
Nimbostratus
Sep 11, 2013iControl PoolMember stats returning a calculated negative value.
Hello, I have been working with the iControl for some time now in PowerShell and python. I have recently been working towards gathering statistics values through the iControl api. In testing the cont...
Jason_44516
Nimbostratus
Sep 11, 2013I fixed the formatting as I too see that it was an epic failure on my part for the pasting.
This is the Posh function i was using then with the Invoke-Inline function call.
function Convert-To64Bit()
{
param($high, $low);
return Invoke-Inline "result.Add((Convert.ToUInt64($high)<<32) | (Convert.ToUInt64($low)));"
} end of function Convert-To64Bitfunction Invoke-Inline()
https://devcentral.f5.com/wiki/iControl.PsRateBasedStatistics.ashx
{
param(
[string[]] $code,
[object[]] $arguments,
[string[]] $reference = @()
)
Stores a cache of generated inline objects. If this library is dot-sourced
from a script, these objects go away when the script exits.
if(-not (Test-Path Variable:\inlineCode.Cache))
{
${GLOBAL:inlineCode.Cache} = @{}
}
$using = $null;
$source = $null;
if($code.length -eq 1) {
$source = $code[0]
} elseif($code.Length -eq 2){
$using = $code[0]
$source = $code[1]
} else {
Write-Error "You have to pass some code, or this won't do anything ..."
}
un-nesting magic (why do I need this?)
$params = @()
foreach($arg in $arguments) { $params += $arg }
$arguments = $params
The main function to execute inline C.
Pass the argument to the function as a strongly-typed variable. They will
be available from C code as the Object variable, "arg".
Any values assigned to the "returnValue" object by the C code will be
returned to MSH as a return value.
See if the code has already been compiled and cached
$cachedObject = ${inlineCode.Cache}[$source]
Write-Verbose "Type: $($arguments[0].GetType())"
The code has not been compiled or cached
if($cachedObject -eq $null)
{
$codeToCompile =
@"
using System;
using System.Collections.Generic;
$using
public class InlineRunner
{
public List Invoke(Object[] args)
{
List result = new List();
$source
if( result.Count > 0 ) {
return result;
} else {
return null;
}
}
}
"@
Write-Verbose $codeToCompile
Obtains an ICodeCompiler from a CodeDomProvider class.
$provider = New-Object Microsoft.CSharp.CSharpCodeProvider
Get the location for System.Management.Automation DLL
$dllName = [PsObject].Assembly.Location
Configure the compiler parameters
$compilerParameters = New-Object System.CodeDom.Compiler.CompilerParameters
$assemblies = @("System.dll", $dllName)
$compilerParameters.ReferencedAssemblies.AddRange($assemblies)
$compilerParameters.ReferencedAssemblies.AddRange($reference)
$compilerParameters.IncludeDebugInformation = $true
$compilerParameters.GenerateInMemory = $true
Invokes compilation.
$compilerResults =
$provider.CompileAssemblyFromSource($compilerParameters, $codeToCompile)
Write any errors if generated.
if($compilerResults.Errors.Count -gt 0)
{
$errorLines = ""
foreach($error in $compilerResults.Errors)
{
$errorLines += "`n`t" + $error.Line + ":`t" + $error.ErrorText
}
Write-Error $errorLines
}
There were no errors. Store the resulting object in the object
cache.
else
{
${inlineCode.Cache}[$source] =
$compilerResults.CompiledAssembly.CreateInstance("InlineRunner")
}
$cachedObject = ${inlineCode.Cache}[$source]
}
Write-Verbose "Argument $arguments`n`n$cachedObject"
Finally invoke the C code
if($cachedObject -ne $null)
{
return $cachedObject.Invoke($arguments)
}
} end function Invoke-Inline- mhite_60883Sep 12, 2013
Cirrocumulus
Maybe you could put it in a gist? The F5 web site still munges it for some reason.
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects