0

I am trying to store the value of current process in the else statement of a powershell script. I used "Global:varname" to store. This process calls another elevated powershell process where I need this variable value to close the caller process. When I try to print the global variable outside else statement it prints the initialized value rather then PID.

My script looks something like this:

# Get the ID and security principal of the current user account

$global:some = 'some global'   //declaring the global variable
$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)

# Get the security principal for the Administrator role
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator

# Check to see if we are currently running "as Administrator"
if ($myWindowsPrincipal.IsInRole($adminRole))
{

Write-Host $PID

# We are running "as Administrator" - so change the title and background color to indicate this

$Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"

$Host.UI.RawUI.BackgroundColor = "DarkBlue"
clear-host
}
else
{



# We are not running "as Administrator" - so relaunch as administrator

# Create a new process object that starts PowerShell
$newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";

# Specify the current script path and name as a parameter
$newProcess.Arguments = $myInvocation.MyCommand.Definition;

# Indicate that the process should be elevated
$newProcess.Verb = "runas";

# Start the new process
[System.Diagnostics.Process]::Start($newProcess);

 Set-Variable -Name $some -Value $PID -Scope Global   //setting the value of PID to the global    variable

# Exit from the current, unelevated, process
Write-Host $global:some 

exit
}
 Write-Host $global:some   //this comes out to be initialized value rather then PID


  # Run your code that needs to be elevated here
 #Write-Host -NoNewLine "Press any key to continue..."
#$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
java_doctor_101
  • 3,287
  • 4
  • 46
  • 78
  • "Global" variables are global for a single process. If you need them in a new process, you should pass them as arguments to the new process. – Mike Zboray Sep 23 '14 at 16:33
  • Ummm...I am not sure if new process is started when I print this variable. I try to print it just outside else and it still prints the initialized the value and not the one which I changed inside if. – java_doctor_101 Sep 23 '14 at 20:05
  • I'm not quite sure what part you are referring to. There is an exit statement in the else clause so it never gets outside the else. But one other thing I can see that is incorrect is that it should be `Set-Variable -Name some -Value $PID -Scope Global`. The variable name for the purposes of Set-Variable does not include `$` otherwise you are setting a variable with the name that is equal to the value of $some (i.e. 'some global'). – Mike Zboray Sep 23 '14 at 20:24
  • possible duplicate of [PowerShell - get process ID of called application](http://stackoverflow.com/questions/4762982/powershell-get-process-id-of-called-application) – Paul Sweatte Jan 29 '15 at 23:01

0 Answers0