olivierg
olivierg

Reputation: 792

PowerShell variable definition from a pscustomobject

i've got this piece of code from a script i found on the web (just showing the part that interests me)

ForEach  ($Computer in  $Computername) {
    $adsi  = [ADSI]"WinNT://$Computername"
    $adsi.Children | where {$_.SchemaClassName -eq  'user'} |  ForEach {
        [pscustomobject]@{
        UserName = $_.Name[0]
        SID = ConvertTo-SID -BinarySID $_.ObjectSID[0]
        PasswordAge = [math]::Round($_.PasswordAge[0]/86400)
        LastLogin = If ($_.LastLogin[0] -is [datetime]){$_.LastLogin[0]}Else{'Never logged  on'}
        UserFlags = Convert-UserFlag  -UserFlag $_.UserFlags[0]
        MinPasswordLength = $_.MinPasswordLength[0]
        MinPasswordAge = [math]::Round($_.MinPasswordAge[0]/86400)
        MaxPasswordAge = [math]::Round($_.MaxPasswordAge[0]/86400)
        BadPasswordAttempts = $_.BadPasswordAttempts[0]
        MaxBadPasswords = $_.MaxBadPasswordsAllowed[0]
        }
    }
}

the code displays things on the console, but i would like to define/use these values as variables instead (as i want to use them in a hash table afterwards to send them in a http/POST request afterwards)

is there a way to get all these attributes as variables such as $LastLogin, $MinPasswordAge etc ?

as i don't want to display them, but send them in a POST like this :

$postParams = @{LastLogin=$LastLogin;MinPasswordAge=$MinPasswordAge}
Invoke-WebRequest -Uri http://example.com/foobar -Method POST -Body $postParams

to be honest i'm a complete newbie in PowerShell (i'm a Perl guru) and i don't know what pscustomobject does in there, i just want to define the variables in that loop, and use them at the end.

i've tried a couple of things with no success (can post them if required) thanks !

Upvotes: 1

Views: 256

Answers (2)

mklement0
mklement0

Reputation: 437248

Your own solution works, but only if you perform all processing inside the ForEach-Object script block (unless there's only ever 1 iteration, which doesn't appear to be the case here).

If you want to process the results later, you can simply collect them in an array by assigning the entire foreach loop to a variable (code shortened):

$allUsers = foreach ($Computer in  $Computername) {
    $adsi  = [ADSI]"WinNT://$Computername"
    $adsi.Children | where {$_.SchemaClassName -eq  'user'} |  ForEach {
        # Output a custom object for each user.
        [pscustomobject]@{
          ComputerName = $Computer  # also record the computer name
          UserName = $_.Name[0]
          SID = ConvertTo-SID -BinarySID $_.ObjectSID[0]
          # ...
        }
    }
}

You can then simply enumerate the collected [pscustomobject]s and access their properties rather than using variables:

foreach ($user in $allUsers) {
   # Use the properties to define a hashtable for later use in a http/POST request.
   $ht = @{
     User = $user.UserName
     # ...
   }
}

Upvotes: 2

olivierg
olivierg

Reputation: 792

nm, i found the solution a minute ago.

just got rid of that pscustomobject hash completely, and assigning the variables directory

    $adsi.Children | where {$_.SchemaClassName -eq  'user'} |  ForEach {

        $UserName = $_.Name[0]
        $SID = ConvertTo-SID -BinarySID $_.ObjectSID[0]
        $PasswordAge = [math]::Round($_.PasswordAge[0]/86400)
        $LastLogin = If ($_.LastLogin[0] -is [datetime]){$_.LastLogin[0]}Else{'Never logged  on'}
        $UserFlags = Convert-UserFlag  -UserFlag $_.UserFlags[0]
        $MinPasswordLength = $_.MinPasswordLength[0]
        $MinPasswordAge = [math]::Round($_.MinPasswordAge[0]/86400)
        $MaxPasswordAge = [math]::Round($_.MaxPasswordAge[0]/86400)
        $BadPasswordAttempts = $_.BadPasswordAttempts[0]
        $MaxBadPasswords = $_.MaxBadPasswordsAllowed[0]

        Write-Host $UserName

    }
}

Upvotes: -1

Related Questions