Jump to content

Why are my objects gone after I For ... loop through them?


Recommended Posts

I have what I hope is a simple noob programming question.

In the code snippet below, I make an WMI query and store the results in $objItems. Then I run through $objItems with a For loop.

If I try to re-use $objItems after I do this, the date stored in it isn't there. I have to re-run the WMI query to have valid data.

Is that normal? Or should I query, store the data I need to variables, and then be able to reuse them?

Example workflow: Query WMI, store to $objItems. Loop through and find there are no disks matching my variables that have partitions. After I have more functions to create partitions, but I have to re-query WMI to reuse the same $objItems array because all the objects are now null.

$objItems = $objWMI.ExecQuery("SELECT * FROM CIM_DiskDrive", "WQL", 0x10 + 0X20); query WMI for disk information
If IsObj($objItems) Then ; we check for existing partitions and clean them if needed, then reboot
    For $objItem In $objItems
        If $objItem.SCSIBus = $sSysDiskBus And $objItem.SCSIPort = $sSysDiskPrt And $objItem.InterfaceType = "SCSI" And $objItem.Partitions <> 0 Then
            _cleanDisk($objItem.Index); will reboot when it's done
        EndIf
    Next
EndIf

Always carry a towel.

Link to comment
Share on other sites

Please show us the complete script. Without the code I just can ask my crystal ball and guess.

Important is the part where you define variable $objItems.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Hi, sorry for the delay!

I have since eliminated the problem with some rewriting that doesn't try to reuse the query but, I think JohnOne was on the right track. I was making the query outside a function then trying to reuse it inside a function for the second use.

Always carry a towel.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...