I have simple powershell skript for downloading XML from url.
I need dynamically added unix timestamp to url, with actual date - in unix timestamp format. then i need running it with windows task scheduler.
code for powershell skript now:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri "https://xxxxx&from_date=1668069585" -OutFile "C:\IMPORT\xxx_$(get-date -Format yyyy_MM_dd_hh_mm_ss).xml"
how can i add unix timestamp after &from_date?
thanks
CodePudding user response:
In PowerShell v5.1 and above, you can use the [datetimeoffset]
type's .ToUnixTimeSeconds()
method:
$uri = 'https://xxxxx&from_date=' [datetimeoffset]::Now.ToUnixTimeSeconds()
Note:
- PowerShell itself offers
Get-Date
-UFormat %s
, which returns the Unix timestamp as a string, but this only works properly in PowerShell (Core) 7 (in Windows PowerShell, the timestamp is mistakenly expressed in the number of seconds since midnight 1 Jan 1970 local time; also, it unexpectedly has a fractional component).