Jump to content
Sign in to follow this  
ModemJunki

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

Recommended Posts

ModemJunki

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

Share this post


Link to post
Share on other sites
water

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 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
ModemJunki

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.

Share this post


Link to post
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
Sign in to follow this  

×