none
Type conversion RRS feed

  • Question

  • hey,

    I'm try to convert a system.directoryservices.resultpropertyvaluecollection to system.in64 but I'm failing badly

    Basically, I've got a powershell script that uses adsi searcher to pull out the maxpwdage from a domain. Once I've got this value I'm trying to do some arithmetic on in.

    Can anyone assist?

     

    Wednesday, October 5, 2011 12:16 PM

Answers

  • I've cracked it. if I use the following command it works without problem, quite strange as it's not an object of array type

    [int64]$maxpwdage = $dc.properties.item("maxPwdAge")[0]

     

    He is the finished function should anyone need it

    function Get-maxPwdAge{
    
      $root = [ADSI]"LDAP://<<PUT_FQDN_FOR DOMAIN_HERE>>"
    
      $filter = "(&(objectcategory=domainDNS)(distinguishedName=<<PUT_DN_OF_DOMAIN_NAME_HERE>>))"
    
      $ds = New-Object system.DirectoryServices.DirectorySearcher($root,$filter)
    
      $dc = $ds.findone()
    
      [int64]$maxpwdage = [System.Math]::Abs( $dc.properties.item("maxPwdAge")[0])
    
      $maxpwdage/864000000000
    
    
    }
    

    • Marked as answer by ZeniTimes Wednesday, October 5, 2011 1:29 PM
    Wednesday, October 5, 2011 1:27 PM

All replies

  • [int64]$maxpwdage = {code to get system.directoryservices.resultpropertyvaluecollection object} | select -expandproperty maxpwdage
    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    • Edited by Bigteddy Wednesday, October 5, 2011 12:42 PM
    Wednesday, October 5, 2011 12:40 PM
  • didn't work got the following error

     

    Cannot convert the "@{maxpwdage=}" value of type "Selected.System.Int64" to type "System.Int64".
    At P:\scripts\ad\users\get-maxpwdage\get-maxpwdage.ps1:33 char:18
    + [Int64]$maxpwdage <<<<  = $dc.properties.item("maxPwdAge") | select maxpwdage
        + CategoryInfo          : MetadataError: (:) [], ArgumentTransformationMetadataException
        + FullyQualifiedErrorId : RuntimeException

    Wednesday, October 5, 2011 1:10 PM
  • Can you post part of your code so we can see what's happening?


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Wednesday, October 5, 2011 1:15 PM
  • I've cracked it. if I use the following command it works without problem, quite strange as it's not an object of array type

    [int64]$maxpwdage = $dc.properties.item("maxPwdAge")[0]

     

    He is the finished function should anyone need it

    function Get-maxPwdAge{
    
      $root = [ADSI]"LDAP://<<PUT_FQDN_FOR DOMAIN_HERE>>"
    
      $filter = "(&(objectcategory=domainDNS)(distinguishedName=<<PUT_DN_OF_DOMAIN_NAME_HERE>>))"
    
      $ds = New-Object system.DirectoryServices.DirectorySearcher($root,$filter)
    
      $dc = $ds.findone()
    
      [int64]$maxpwdage = [System.Math]::Abs( $dc.properties.item("maxPwdAge")[0])
    
      $maxpwdage/864000000000
    
    
    }
    

    • Marked as answer by ZeniTimes Wednesday, October 5, 2011 1:29 PM
    Wednesday, October 5, 2011 1:27 PM
  • here is the code from richard's script http://www.rlmueller.net/PowerShell/PSPwdExpires.txt

    # Retrieve Domain maximum password age policy, in days.
    $D = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
    $Domain = [ADSI]"LDAP://$D"
    $MPA = $Domain.maxPwdAge.Value
    # Convert to Int64 ticks (100-nanosecond intervals).
    $lngMaxPwdAge = $Domain.ConvertLargeIntegerToInt64($MPA)
    # Convert to days.
    $MaxPwdAge = -$lngMaxPwdAge/(600000000 * 1440)
    
    Write-Host $MaxPwdAge
    

    you can now use the $maxpwdage

     


    -join("74686979616775313440686F746D61696C2E636F6D"-split"(?<=\G.{2})",21|%{[char][int]"0x$_"})
    http://www.myExchangeWorld.com
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Wednesday, October 5, 2011 1:27 PM
    Moderator
  • Using Quest:

    $maxPwdAge=(Get-QADObject 'dc=sec,dc=local').maximumPasswordAge

    This will get you a timespan object set to the number of days.

    $maxPwdAge.Days

     


    jv
    Wednesday, October 5, 2011 1:54 PM