Windows Form can't access PowerShell variable - windows

I'm trying to display a PowerShell variable in a Windows form. I have a button and a label on the form. I can display the string return by TestFunction on the label but not on the Form pop up window.
This is my PowerShell script:
function TestFunction()
{
return "PASSED"
}
Function Button_Click()
{
$testResults = TestFunction
$ResultLabel1.Text = $testResults ## this works - I can see "PASSED"
$TroubleButton1.Add_Click(
{
# This does not work
[System.Windows.Forms.MessageBox]::Show($testResults)
}
)
}
## Call function
Button_Click
Why does the form think $testResults is NULL?

You assign $testResults variable in Button_Click scope, thus you will lose that variable, when you leave that scope. You should save the variable in a scope, which will exists when you press $TroubleButton1 button. Or you can pick value from $ResultLabel1 label instead of variable:
[System.Windows.Forms.MessageBox]::Show($ResultLabel1.Text)

Related

Trying to update the value of global variable in Shell Script

myfunc3()
{
echo $1
}
myfunc2()
{
input=$(myfunc3 10)
echo "someting that will be used later"
}
myfunc1() {
var=$(myfunc2) #this does not update the value
# myfunc2 This updates the value
}
input="$(myfunc3 1)"
echo "The new value is: $input"
myfunc1
echo "The new value is: $input"
I have 3 functions that are interrelated. I am trying to update the value of the input variable. I am able to do it when I simply call the function myfunc2 but when I am trying to get the returned value like var=$(myfunc2) as I want to use it for further uses, then the value of the input variable is not updating.
I am very new to shell scripting and I'm not able to understand the reasoning behind it.
Is there any other way to return the value or calling the function?
Thanks in advance

Get a value and than use that value to compare

I am facing a problem with a PowerShell variable.
My scenario is,
Inside a function, I declare a variable $a, than in a switch, I get a value and store this to variable $a.
Now in another switch in that function, I want to compare $a. But there $a returns null.
Sample code is given below:
function fun
{
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true, Position = 0)]
[ValidateNotNullOrEmpty()]
$param
)
$Get_OldData = " " #declare variable
switch ($param){
'param_001' {
$Get_OldData = "test data returned"
}
Default {
$Get_OldData = "test data returned"
}
}
switch ($param){
'param_001' {
$New_Data = "New Data"
#problem is here, can not compare $Get-OldData returns null here
#though data is assigned
if ( $New_Data -eq $Get_OldData){
#logic goes here
}
}
Default {
$New_Data = "New Data"
}
}
}
What is the solution of this problem?
You have multiple issues with your code.
The main issue probably is that you are using $param within your switch which has not been set. Same applies to $Fetch. Another Issue is that your $New-Data variable contains a hypen which you either should replace with an underscore or surround with curly brackets like ${New-Data}.
Also, // does not introduce a comment, you have to use a hash #.

Get return variable or variable from one function to another function in controller

I am wondering if it is allowed to access/get/use the variable from one function to another function within one controller?
Thank you.
Try Something like this.
class Index extends CI_Controller {
protected $var1;
public function index() {
$this->setVar1();
echo $this->var1; // print $var1
}
public function setVar1() {
$this->var1 = 1;
}
}
$superduper = 'superduper' ;
available only locally within the method (function)
or passed like
$this->reviews->insertToReviews($superduper) ;
versus
$this->superduper = 'superduper' ;
as soon as $this->variablename is declared in a controller its instantly available to:
any method in the controller
any method in any model that is called by the controller
any view file called by the controller
so without passing the variable - in a view you can use
echo 'my lunch is ' . $this->superduper ;
but often its better to explicitly pass values to the view especially if they are unique to the method - it makes it easier to see what is going on. so in that case in controller:
$data['superduper'] = $this->superduper ;
and in view
echo 'my lunch is ' . $superduper ;
Now when anyone looks at the method in the controller - we can see that superduper is being passed to $data. the point is that even though you can avoid passing variable names to methods or the view by declaring $this->somename - if you pass them locally it can make it easier to see what is going on.
The flip side is something like:
$this->error_message = "Error retrieving database records";
is awesome. you can have error messages in any method and they will be automatically available no matter what else happens. so then in your view file have something like
if($this->error_message != '') { echo $this->error_message ;}
this is especially helpful while you are building the site.

Output not displayed when assigning the return value of a function to a variable

If I have a very simple script that contains a function that just does the following:
function saySomething() {
Write-Output "Saying someting..."
}
If I call this function from the console the output is displayed. If I, however, do the following:
$something = saySomething
Then no output is displayed. Obviously my working example is much more complicated than this, but in short, how do I display the output within a function if that function is used to set a variable?
Try the following:
saySomething | Tee-Object -Variable something
This should work exactly as you want.

Trying to get environment variables from a Widget widget.system()

I'm attempting to write some Dashcode to but can't seem to get the environment variables when I run the /env command. The environment doesn't appear to be sourced because it always returns "Undefined". Below is my code and I'm open for any suggestions (I need more than just LANG, LANG is just the example).
var textFieldToChange = document.getElementById("LangField");
var newFieldText = widget.system("/usr/bin/env | grep LANG").outputString;
textFieldToChange.value = newFieldText;
Is there an easy way to source my environment and cache it in Dashcode or do I need to attempt to write something that will cache the entire environment somehow?
Thanks for any ideas!
Have you allowed Command Line Access? Go to Widget Attributes (in the left hand menu) , then extensions and check Allow Command Line Access else the widget is prevented from talking to the system. Not sure if this is what is causing the problem though.
I know this thread is quite aged, but anyway, the question is still up to date :-)
Just having started with Dashcode and widgets myself, I did a quick hack on this:
function doGetEnv(event)
{
if (window.widget)
{
var out = widget.system("/bin/bash -c set", null).outputString;
document.getElementById("content").innerText = out;
}
}
For my experimental widget, I did use a scroll area and a button. The doGetEnv(event) is fired upon onclick, set via inspector. The Id "content" is the standard naming of the content within the scroll area.
The out var containes a string with '\n' charaters, to transform it into an array use split().
function doGetEnv(event)
{
if (window.widget)
{
var out = widget.system("/bin/bash -c set", null).outputString;
out = out.split("\n");
document.getElementById("content").innerText = out[0];
}
}
The first entry is "BASH..." in my case.
If you search for a particular item, use STRING's match method (see also http://www.w3schools.com/jsref/jsref_match.asp) along with the following pages on regular expressions:
http://www.w3schools.com/jsref/jsref_obj_regexp.asp
http://www.w3schools.com/js/js_obj_regexp.asp
To cache the environment, you can use:
var envCache = "";
function cacheENV()
{
envCache = widget.system("/bin/bash -c set", null).outputString;
envCache = envCache.split("\n");
}
This will leave an array in envCache. Alternative:
function cacheENV()
{
var envCache = widget.system("/bin/bash -c set", null).outputString;
envCache = envCache.split("\n");
return envCache;
}

Resources