PowerShell Python command-line arguments

Tagged: ,

Viewing 2 reply threads
  • Author
    Posts
    • #1559377
      Xander Cage
      Participant
      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
      Participant
      Member Points: 4,448
      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'
      03/12/2020
      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.

    • #1567197
      David Johnson
      Participant
      Member Points: 56
      Rank: 1

      I’m pursuing a python course and trying to run a Python script in PowerShell.

      Why are you doing this? Python is Python, PowerShell is PowerShell run python scripts in python and PowerShell scripts in powershell

      from datetime import datetime
      now = datetime.now() # current date and time
      strdate = now.strftime("%d/%m/%Y")
      print("date and time:",strdate)
      avatar
Viewing 2 reply threads
  • You must be logged in to reply to this topic.
© 4sysops 2006 - 2022

CONTACT US

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

Sending

Log in with your credentials

or    

Forgot your details?

Create Account