# Help with a loop and saving a desired result

**URL:** <https://forums.powershell.org/t/help-with-a-loop-and-saving-a-desired-result/16462>\
**Category:** PowerShell Help\
**Created:** [May 9, 2021, 11:07pm UTC](https://forums.powershell.org/t/help-with-a-loop-and-saving-a-desired-result/16462 "2021-05-09T23:07:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![EdO\_Bessemer](https://avatars.discourse-cdn.com/v4/letter/e/d9b06d/32.png) [@EdO\_Bessemer](https://forums.powershell.org/u/EdO_Bessemer)\
**Post date:** [May 9, 2021, 11:07pm UTC](https://forums.powershell.org/t/help-with-a-loop-and-saving-a-desired-result/16462/1 "2021-05-09T23:07:08Z")

</div>

Hi, I have a script that loops through all the VM’s on a specific host and checks the ‘isolation.tools.copy.disable’. I am trying to get the code so that if any one VM shows that the setting is $False (users can copy to the console) it saves a value of “Non-Compliant” into a variable. My problem what ever the last server in the loops results are in what ends up in the variable.

Here is my code:

```auto
#Connects directly to the VMHost

Connect-VIServer $vmhost -Credential $Credential

#Gets all the VMs on a VMHost
$HostVMs = $vmhost | Get-VM *
$VMSettings =$null

$VMSettings += ForEach ($HostVM in $HostVMs)
                {

                #Get the VM Copy setting

                $ConsoleCopyCompliance = "Compliant"
                 
                $VMConsoleCopySettings = $null
                $VMConsoleCopySettings = {$HostVM | Get-AdvancedSetting -Name "isolation.tools.copy.disable" | Select Entity, Name, Value}
                                            If($VMConsoleCopySetting.Value -eq $False) {$ConsoleCopyCompliance = "Non-Compliant"}
                                                

                $ConsoleCopyVMs = $VMConsoleCopySettings | where {$_.value –eq “false”}

                #$ConsoleCopyVMs | Select Entity,Value | ft
                $ConsoleCopyCompliance 

```

All help is appreciated. Thanks , Ed

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [May 9, 2021, 11:43pm UTC](https://forums.powershell.org/t/help-with-a-loop-and-saving-a-desired-result/16462/2 "2021-05-09T23:43:30Z")

</div>

Remove the + before the foreach or here is another approach to try:

```auto
#Gets all the VMs on a VMHost
$HostVMs = $vmhost | Get-VM *
   
$VMSettings = $HostVMs | 
                  Get-AdvancedSetting -Name "isolation.tools.copy.disable" | 
                      Select Entity, 
                             Name,
                             Value,
                             @{Name='Compliant';Expression={$_.Value}}
                              
$ConsoleCopyVMs = $VMSettings | where {$_.value –eq “false”}

#$ConsoleCopyVMs | Select Entity,Value | ft
$ConsoleCopyCompliance 

```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:26pm UTC](https://forums.powershell.org/t/help-with-a-loop-and-saving-a-desired-result/16462/3 "2024-05-16T20:26:25Z")

</div>


