Unfortunately, I don’t have the script now since I’m home. I’ll post it tomorrow.
My question is, I’m logging int a website using invoke web-request. I’m able to successfully login to the website. My problem is that, when I go to a url, it thinks I’m not logged in. I’m using the same session variable to login and to go to the specific page.
So, you’re use IVR in a script and then manually going to the web site via your browser?
That’s two completely different web sessions, one from the script and one from a browser. Since all site are https (well, almost all) these days. All SSL session are unique.
Outside of your script, you need to explain what you are trying to do.
There are articles that cover why you not want to. Not saying that that one can’t or there is never a reason to, but…
As per Don Jones, ms powershell mvp and curator of this site...
I’m not personally a big fan of polluting the global scope just for one function’s benefit. Probably a preferred route would be to put your function in a script module, and then in that module export the function along with a module-level variable. That variable will be global while the module is loaded, but since it’s an explicit part of the module then the shell will be able to clean it up when removing the module.
The keen observer will immediately notice that $globalvariable never produces any output. That is because a variable in PowerShell is not global unless you specifically says so by using $global:globalvariable. Always. At every use. So what is the scope of $globalvariable in our example? It’s ‘local’, local to the global scope to be precise. Unless specified otherwise nothing is ever global.
…
Note that you cannot just assign a value to $globalvariable, that would just create a local variable of that name shadowing the parameter. You are passing a reference object, so you have to use the value property.
…
‘sapien.com/blog/2013/03/06/first-rule-of-powershell-scoping-rules’
It’s a variable scoping issue. Your login variable only exists within the loginToRAPID() function. It is not returned, and it is does not have a global or script scope, so when the loginToRAPID() function ends, the $login variable that was set within the function goes away. When you subsequently call the uploadXML() function and try to use the $login variable, it is a null value because it has not been set within the working scope of the uploadXML() function.
Here is a more complete example using two functions. The variable gets set in one function and is returned to the script. The script then passes the returned value into the second function as a parameter.
Function e {
$evar = "Variable Set in Function E"
"`$evar inside of Function E: $evar" | Out-Host
$evar
}
Function f {
Param (
$evar
)
"`$evar inside of Function F (after being passed in as a parameter from main script): $evar" | Out-Host
}
$result = e
"`$evar in main script: $evar" | Out-Host
"`$result in main script: $result" | Out-Host
f -evar $result
Results:
$evar inside of Function E: Variable Set in Function E
$evar in main script:
$result in main script: Variable Set in Function E
$evar inside of Function F (after being passed in as a parameter from main script): Variable Set in Function E