Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot overwrite variable #28

Closed
Aymen80 opened this Issue Nov 15, 2016 · 8 comments

Comments

Projects
None yet
4 participants
@Aymen80
Copy link

Aymen80 commented Nov 15, 2016

hello,
when i run the script i have directly these error message at the beginning ,i dont know how to correct this

New-Variable : Cannot overwrite variable EnvironmentName because the variable has been optimized. Try using the
New-Variable or Set-Variable cmdlet (without any aliases), or dot-source the command that you are using to set the
variable.
At C:\temp\scripts\XA-and-XD-HealthCheck.ps1:74 char:26

  •     If ($CreateVariable) { New-Variable -Name $_.Name -Value $VarValue -Scope $_.S ...
    
  •                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : WriteError: (EnvironmentName:String) [New-Variable], SessionStateUnauthorizedAccessExcep
      tion
    • FullyQualifiedErrorId : VariableNotWritableRare,Microsoft.PowerShell.Commands.NewVariableCommand

New-Variable : Cannot overwrite variable ShowXenAppTable because the variable has been optimized. Try using the
New-Variable or Set-Variable cmdlet (without any aliases), or dot-source the command that you are using to set the
variable.
At C:\temp\scripts\XA-and-XD-HealthCheck.ps1:74 char:26

  •     If ($CreateVariable) { New-Variable -Name $_.Name -Value $VarValue -Scope $_.S ...
    
  •                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : WriteError: (ShowXenAppTable:String) [New-Variable], SessionStateUnauthorizedAccessExcep
      tion
    • FullyQualifiedErrorId : VariableNotWritableRare,Microsoft.PowerShell.Commands.NewVariableCommand

New-Variable : Cannot overwrite variable PvsWriteMaxSize because the variable has been optimized. Try using the
New-Variable or Set-Variable cmdlet (without any aliases), or dot-source the command that you are using to set the
variable.
At C:\temp\scripts\XA-and-XD-HealthCheck.ps1:74 char:26

  •     If ($CreateVariable) { New-Variable -Name $_.Name -Value $VarValue -Scope $_.S ...
    
  •                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : WriteError: (PvsWriteMaxSize:String) [New-Variable], SessionStateUnauthorizedAccessExcep
      tion
    • FullyQualifiedErrorId : VariableNotWritableRare,Microsoft.PowerShell.Commands.NewVariableCommand

The variable '$PvsWriteMaxSize' cannot be retrieved because it has not been set.
At C:\temp\scripts\XA-and-XD-HealthCheck.ps1:81 char:20

  • $PvsWriteMaxSize = $PvsWriteMaxSize * 1Gb
  •                ~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (PvsWriteMaxSize:String) [], RuntimeException
    • FullyQualifiedErrorId : VariableIsUndefined

Begin with Citrix XenDestop / XenApp HealthCheck

@sommerers

This comment has been minimized.

Copy link

sommerers commented Dec 27, 2016

I'm seeing similar results, when I try to run the PowerShell script through PowerShell and not through PowerShell ISE. When I run it through PowerShell ISE it works fine, so I'm wondering if it is requiring some type of interaction with the host that isn't allowed.

@sommerers

This comment has been minimized.

Copy link

sommerers commented Dec 27, 2016

Aymen80 I believe I've been able to get this to work by modifying the PowerShell Script. I'm not saying this is the preferred method, but it will allow you to use the script if you are having the issue above.

While testing the script in Powershell.exe and PowerShell ISE I noticed that there was an issue with the script on the first part under # Import Variables from XML: it seemed like it was not able to completely pull the information required to define the Script Path and Script Name that are used lower in the script.

To resolve this issue I commented out the following code and then added a manual location and set the Variables for ScriptPath and ScriptName

Before Modifying
#==============================================================================================

Import Variables from XML:

If (![string]::IsNullOrEmpty($hostinvocation)) {
[string]$Global:ScriptPath = [System.IO.Path]::GetDirectoryName([System.Windows.Forms.Application]::ExecutablePath)
[string]$Global:ScriptFile = [System.IO.Path]::GetFileName([System.Windows.Forms.Application]::ExecutablePath)
[string]$global:ScriptName = [System.IO.Path]::GetFileNameWithoutExtension([System.Windows.Forms.Application]::ExecutablePath)
} ElseIf ($Host.Version.Major -lt 3) {
[string]$Global:ScriptPath = Split-Path -parent $MyInvocation.MyCommand.Definition
[string]$Global:ScriptFile = Split-Path -Leaf $script:MyInvocation.MyCommand.Path
[string]$global:ScriptName = $ScriptFile.Split('.')[0].Trim()
} Else {
[string]$Global:ScriptPath = $PSScriptRoot
[string]$Global:ScriptFile = Split-Path -Leaf $PSCommandPath
[string]$global:ScriptName = $ScriptFile.Split('.')[0].Trim()
}

Set-StrictMode -Version Latest

After Modifying

#==============================================================================================

Import Variables from XML:

<#
If (![string]::IsNullOrEmpty($hostinvocation)) {
[string]$Global:ScriptPath = [System.IO.Path]::GetDirectoryName([System.Windows.Forms.Application]::ExecutablePath)
[string]$Global:ScriptFile = [System.IO.Path]::GetFileName([System.Windows.Forms.Application]::ExecutablePath)
[string]$global:ScriptName = [System.IO.Path]::GetFileNameWithoutExtension([System.Windows.Forms.Application]::ExecutablePath)
} ElseIf ($Host.Version.Major -lt 3) {
[string]$Global:ScriptPath = Split-Path -parent $MyInvocation.MyCommand.Definition
[string]$Global:ScriptFile = Split-Path -Leaf $script:MyInvocation.MyCommand.Path
[string]$global:ScriptName = $ScriptFile.Split('.')[0].Trim()
} Else {
[string]$Global:ScriptPath = $PSScriptRoot
[string]$Global:ScriptFile = Split-Path -Leaf $PSCommandPath
[string]$global:ScriptName = $ScriptFile.Split('.')[0].Trim()
}

#>

Set-location 'C:\Citrix\PowerShell\HealthCheck'
$Global:ScriptPath = 'C:\Citrix\PowerShell\HealthCheck'
$Global:ScriptName = 'XA-and-XD-HealthCheck'

Set-StrictMode -Version Latest

The machine I was testing this from was a Delivery Controller running XenDesktop 7.11 on Windows 2012 R2 with PowerShell v4

@sommerers

This comment has been minimized.

Copy link

sommerers commented Dec 31, 2016

Sacha, what is the variable $hostInvocation supposed to represent?

@sacha81

This comment has been minimized.

Copy link
Owner

sacha81 commented Jan 1, 2017

This part is done by Stefan but I asked him, here: https://www.sapien.com/blog/2009/09/02/powershell-hosting-and-myinvocation/

btw your solution with

Set-location 'C:\Citrix\PowerShell\HealthCheck'
$Global:ScriptPath = 'C:\Citrix\PowerShell\HealthCheck'
$Global:ScriptName = 'XA-and-XD-HealthCheck'

is not system independent for this reason I will not add this in the main branch

anyway, thanks for your hint for script users which has a non working script

@sommerers

This comment has been minimized.

Copy link

sommerers commented Jan 2, 2017

FYI, the link you referenced above for that code is not native to PowerShell and requires the Sapiens PowerShell , which makes that software/package a requirement to utilize that.

@Speedliner

This comment has been minimized.

Copy link

Speedliner commented Jan 4, 2017

I'm seeing the same issue. I tried to insert the code (and changed the paths, of course) from @sommerers for testing purpose, but that didn't change anything.

@sacha81 Any ideas how to address this issue in the script?

@sacha81

This comment has been minimized.

Copy link
Owner

sacha81 commented Jan 9, 2017

Input on the Blog from SRI:

Hi Sacha/Ryan, I fixed this issue by running the scheduled task with argument “non interactive”
I used below command in argument field after which it started working like we run from ISE.
-NonInteractive -file C:\task\XA-and-XD-HealthCheck.ps1

please check that! On my system user run the script has permission "logon interfactive", I assume thats the reason thats working on my system.

@Speedliner

This comment has been minimized.

Copy link

Speedliner commented Jan 19, 2017

I fixed this issue by running the scheduled task with argument “non interactive”
I used below command in argument field after which it started working like we run from ISE.
-NonInteractive -file C:\task\XA-and-XD-HealthCheck.ps1

@sacha81 That solved the problem for me, too!

@sacha81 sacha81 added the bug label Feb 28, 2017

@sacha81 sacha81 closed this Mar 28, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.