locked
Powershell datatime variable to hold null value to pass to function RRS feed

  • Question

  • Hello Experts,

    I am working with a custom API which is delivered as DLL and I have referenced the DLL in my Powershell session and accessing the methods. The API provides multiple overloaded methods to call the function with different counts of parameters.

    One of the overloaded methods provides a way to update an "object" by calling the function with parameters as shown below, Parameter number 14 expects a valid datetime attribute.

    From the front\GUI I am able to populate the object by specific "No Value" to the "date_obsolete" parameter and when I check the database, I see that the database filed is populated with "NULL" Value as show below,


    queston: how do I create a valid variable of type "DATETIME" with $NULL value? I have tried to do that and PowerShell does not allow me to have a attribute of DATETIME with Value $NULL.  I tried to search and found some discussion around "[Nullable[DateTime]]$null "  but I am unable to use it to call my method.  it errors out . Kindly suggest how do i pass a NULL Value to the parameter of the type DATATYPE,

    $Result2=$myproxy.chg_item($Result,$psitemNumber,$psitemver,$psitemdesc,$psitemStatus,$psconfigitem,$psunitId,$psitemclass,$psserial,$psphantom,$psmainEquip,$pssoftware,$psitemtype,$psclassid,[Nullable[DateTime]]$null,$psextdesc)







    Wednesday, February 26, 2020 1:38 PM

Answers

  • A null or empty datetime object looks like this:

    [datetime]0

    A null datetime cannot evaluate to a generic $null. It will throw an exception.  A Net DateTime object must contain a value and the generic empty is a value of 0 which equates to 0 ticks of the clock which started on 1/1/0001.

    If a structure is created with a property that is declared as a DateTime then it will automatically contain a 0 since Net guarantees that all objects will be allocated and initialized t the ground state.  An int will be inited as 0 and a string as "" and so on.

    I recommend contacting the creator of you DLL and asking them how they expect that you can do what you ask.  

    You can try using the missing type:

    [type]::Missing


    Since the call you are using does not use the nullable datetime object you cannot use that type as an argument.  The "Missing" method will depend on how the code was built and designed to work.  Only the vendor can answer that.


    \_(ツ)_/


    • Edited by jrv Wednesday, February 26, 2020 2:14 PM
    • Marked as answer by c_programer Thursday, February 27, 2020 4:21 AM
    Wednesday, February 26, 2020 2:12 PM

All replies

  • A null or empty datetime object looks like this:

    [datetime]0

    A null datetime cannot evaluate to a generic $null. It will throw an exception.  A Net DateTime object must contain a value and the generic empty is a value of 0 which equates to 0 ticks of the clock which started on 1/1/0001.

    If a structure is created with a property that is declared as a DateTime then it will automatically contain a 0 since Net guarantees that all objects will be allocated and initialized t the ground state.  An int will be inited as 0 and a string as "" and so on.

    I recommend contacting the creator of you DLL and asking them how they expect that you can do what you ask.  

    You can try using the missing type:

    [type]::Missing


    Since the call you are using does not use the nullable datetime object you cannot use that type as an argument.  The "Missing" method will depend on how the code was built and designed to work.  Only the vendor can answer that.


    \_(ツ)_/


    • Edited by jrv Wednesday, February 26, 2020 2:14 PM
    • Marked as answer by c_programer Thursday, February 27, 2020 4:21 AM
    Wednesday, February 26, 2020 2:12 PM
  • Thanks a lot for your quick response. The value  [datetime]0 worked for me.

    Thursday, February 27, 2020 4:22 AM