locked
PoSh Script Error RRS feed

  • Question

  • I am running the following PoSh script I receive from Microsoft in each of my Configuration Manager environments. 

    Param( [Parameter(Mandatory=$True)] [string]$value )  
      $guid = [System.Guid]::NewGuid()  
      if( [System.Guid]::TryParse($value,[ref] $guid) -eq $true ) {  
      #many of the values we hash are Guids  
      $bytesToHash = $guid.ToByteArray()  
    } else {  
      #otherwise hash as string (unicode)  
      $ue = New-Object System.Text.UnicodeEncoding  
      $bytesToHash = $ue.GetBytes($value)   
    }  
      # Load Hash Provider (https://en.wikipedia.org/wiki/SHA-2)   
    $hashAlgorithm = [System.Security.Cryptography.SHA256Cng]::Create()    
    # Hash the input   
    $hashedBytes = $hashAlgorithm.ComputeHash($bytesToHash)              
    # Base64 encode the result for transport   
    $result = [Convert]::ToBase64String($hashedBytes)    
    return $result   

    The script works fine in 1 environment.

    In my 2nd environment it errors out with "You cannot call a method on a null-valued expression. 

    $hashedBytes = $hashAlgorithm.ComputeHash($bytesToHash)

    Thursday, November 15, 2018 5:04 PM

All replies

  • What version of .Net Framework is installed on the machine that fails (the "2nd environment")?

    System.Security.Cryptography.SHA256Cng has been available since version 3.5. However, it isn't available in POS Ready environments.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)


    Thursday, November 15, 2018 8:27 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    LEE


    Just do it.

    Tuesday, November 27, 2018 2:42 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Tuesday, December 4, 2018 3:29 AM