PowerShell Python command-line arguments

Tagged: ,

Viewing 1 reply thread
  • Author
    • #1559377
      Xander Cage
      Member Points: 184
      Rank: 2

      I’m pursuing a python course and trying to run a Python script in PowerShell. While using the sys module I stumble upon the problem of getting the return value of a PowerShell function. The function in question is Date -f ‘dd\/MM\/yyyy’ that returns 14/03/2019 and I’d like that value to be used as an argument in the Python script. I’ve been able so far to get the literal arguments from the command line, e.g. text.

    • #1559402
      David Figueroa
      Member Points: 4,202
      Rank: 3

      I’m guessing that date is some sort of shortcut for Get-Date.  (I hadn’t seen it before, but it only took a second to see what it’s doing).

      The key here is that date is producing a DateTime object.  Now — when you tack on the -f (format string operator), you are converting that DateTime to a String.

      PS C:\> date
      Thursday, December 3, 2020 9:56:30 PM
      PS C:\> (date).GetType()
      IsPublic IsSerial      Name           BaseType
      -------- --------      ----           --------
          True     True  DateTime   System.ValueType
      PS C:\> date -f 'dd/MM/yyyy'
      PS C:\> (date -f 'dd/MM/yyyy').GetType()
      IsPublic IsSerial    Name       BaseType
      -------- --------    ----       --------
          True     True  String  System.Object

      So, if you want to input it into your python function, get rid of the -f operator, it’s completely unnecessary at this point.

      Good luck!

      David F.

Viewing 1 reply thread
  • You must be logged in to reply to this topic.
© 4sysops 2006 - 2022


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account