locked
Powershell where-object in VBScript?? RRS feed

  • Question

  • Hey,

    I am a very enthusiastic powershell user - but now I have to solve a problem in VBScript....where I am a real noob :)
    Reason that I have to use VBScript is that this script will work in a WindowsPE which has not the powershell extensions included :(

    In powershell I would search an object array like the following:

    $ObjArray | Where-object {$_.Name -eq 'foo'}

    But how can this be done in VBScript?

    Thanks

    Monday, June 15, 2015 7:04 PM

Answers

  • If you must use VBScript, you will need to rewrite the code using the For Each statement in VBScript.

    VBScript does not have object filtering in the same way PowerShell does.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by MK-Maddin Monday, June 15, 2015 7:30 PM
    Monday, June 15, 2015 7:07 PM
  • For i = 0 to 10
         Set obj = new  Organisation
         obj.Location_AD = "someplace" & i
         Set a(i) = obj
    Next
    
    


    \_(ツ)_/

    • Marked as answer by MK-Maddin Tuesday, June 16, 2015 7:54 PM
    Monday, June 15, 2015 9:46 PM

All replies

  • If you must use VBScript, you will need to rewrite the code using the For Each statement in VBScript.

    VBScript does not have object filtering in the same way PowerShell does.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by MK-Maddin Monday, June 15, 2015 7:30 PM
    Monday, June 15, 2015 7:07 PM
  • hm ... not nice but makes sense

    Thank you :)

    Monday, June 15, 2015 7:30 PM
  • Hey,

    the function I wrote looks like the following:

    Function ObjByLocation(Location,Array)
    	Dim obj,  i
    	i = 0
    	
    	For Each obj in Array
    		IF obj.Location_short = Location Then
    			Set ObjByLocaion(i) = obj
    			i = i+1
    		End IF
    	Next
    	
    End Function

    unfortunately I receive a failure "Object required : 'obj'" at the 'IF' line..

    Monday, June 15, 2015 9:12 PM
  • There are no objects in VBScript.  Everything is a Variant.  There are COM objects but that is not what you have and an array does not have object notation.

    What is in your array?


    \_(ツ)_/

    Monday, June 15, 2015 9:17 PM
  • Sorry,

    this of course is not the whole script.

    I am defining a custom class first and give that to my function..

     'Define script classes
    Class Organisation
    	Public Country
    	Public Location
    	Public Company
    	Public Computername
    	Public Country_short
    	Public Location_short
    	Public Company_short
    	Public Country_AD
    	Public Location_AD
    	Public Company_AD
    	Public LDAP_in_Group
     End Class

    Monday, June 15, 2015 9:26 PM
  • Thanks - I found my failure.

    It was because the array contained 'null' values

    Monday, June 15, 2015 9:32 PM
  • How are you creating the array?


    \_(ツ)_/

    Monday, June 15, 2015 9:36 PM
  • For i = 0 to 10
         Set obj = new  Organisation
         obj.Location_AD = "someplace" & i
         Set a(i) = obj
    Next
    
    


    \_(ツ)_/

    • Marked as answer by MK-Maddin Tuesday, June 16, 2015 7:54 PM
    Monday, June 15, 2015 9:46 PM