简体   繁体   English

Powershell正在重新编写Scripts中的变量

[英]Powershell is reanimating variables in Scripts

Good evening 晚上好

I have this Problem with this Version 我对这个版本有这个问题

PS C:\temp> $PSVersionTable.PSVersion.Major
4

This is a really strange problem... despite of initialized variables, PowerShell script is somehow able to reuse variable values from previous invocations. 这是一个非常奇怪的问题......尽管初始化变量,PowerShell脚本能够以某种方式重用以前调用的变量值。

The script is simple; 脚本很简单; to show the problem, I work with a list of virtual machines: 为了显示问题,我使用虚拟机列表:

  1. Read all Virtual Machines into an Array 将所有虚拟机读入阵列
  2. Select the 1st Element in the Array 选择阵列中的第一个元素
  3. Add a new Property to the Object from step 2 从步骤2向对象添加新属性
  4. The Problem: if I run the script a second time, the new Property is already there - despite all Variables are initialized. 问题:如果我第二次运行脚本,新属性已经存在 - 尽管所有变量都已初始化。 If I start the Script in a new session, the new Property is missing on the 1st run, afterwards it is already there. 如果我在新会话中启动脚本,则第一次运行时会丢失新属性,之后它已经存在。

Here is the simple Code: 这是简单的代码:

Set-StrictMode -Version 2.0
# Read all Virtual Machines into an Array
$AllVMs = @()
$AllVMs = Get-VM

# Get the 1st Virtual Machine
$VM = $null
$VM = $AllVMs[0]

# Prepare my Property
$MyList = @()
$MyList += "Test"

# If the Property already exists, just add my List
if ($VM.PSobject.Properties.Name -match "MyList") {
  $VM.MyList += $MyList
} else {
  # My Property does not exist: create it
  $VM | Add-Member –MemberType NoteProperty –Name MyList –Value ($MyList)
}

# Give Back my VM Object
$VM

To test the script, I just count the number of MyList-Elements: 要测试脚本,我只计算MyList-Elements的数量:

PS C:\temp> $result = c:\temp\testvar.ps1
PS C:\temp> $result.MyList.Count
1
PS C:\temp> $result = c:\temp\testvar.ps1
PS C:\temp> $result.MyList.Count
2
…

Does somone can help me with this Problem? somone可以帮我解决这个问题吗?

Thanks a lot for any help!! 非常感谢任何帮助!!

Kind regards, Tom 亲切的问候,汤姆

I have asked this question to 'the scripting guy' Forum , too. 我也向“脚本人” 论坛提出了这个问题。

I've got two great answers: 我有两个很好的答案:

  1. From jrv: 来自jrv:
    You are not exiting from PowerShell. 您没有退出PowerShell。 The VM object is dynamic to the session. VM对象对会话是动态的。 It persists until you close PowerShell. 它会一直存在,直到您关闭PowerShell。 Some objects are like this. 有些对象是这样的。 The code base drags them I to PowerShell and they remain cached. 代码库将它们拖到PowerShell,它们仍然被缓存。 I suspect this is what is happening here. 我怀疑这就是这里发生的事情。

  2. From Evgenij Smirnov: 来自Evgenij Smirnov:
    Hi, 嗨,
    this appears to be specific to the VM object. 这似乎是VM对象特有的。 If I substitute Get-VM by Get-Process or Get-ChildItem c:\\ I do not experience this behaviour. 如果我用Get-Process或Get-ChildItem替换Get-VM c:\\我没有遇到这种行为。 If I select a new VM every time I run the script, it does not retain the property. 如果我每次运行脚本时选择一个新VM,它都不会保留该属性。 On the other hand, if I do (Get-VM)[0].MyList after running the script four times, I get four entries. 另一方面,如果我在运行脚本四次后执行(Get-VM)[0] .MyList,我会得到四个条目。
    So this persistence is obviously built into the Hyper-V module, the custom property getting added to the instance of the VM object itself. 因此,这种持久性显然构建在Hyper-V模块中,自定义属性被添加到VM对象本身的实例中。 So you could initialize MyTest to empty on the whole VM collection like this: 因此,您可以初始化MyTest以清空整个VM集合,如下所示:

$AllVMs | foreach {
  if ($_.PSobject.Properties.Name -match "MyList") {
    $_.MyList = @()
  }     
} 

Kind regards, Tom 亲切的问候,汤姆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM