使用 powershell 通过实时系统数据操作 URL header

Using powershell to manipulate URL header with real live system data

我正在尝试使用 powershell 为正在访问网络的命令程序操作 url 值 Url。

基本上是这样的

运行 在 Powershell 中:

程序参数https://www.example.com/dash/926846/l.php?link=bTP77O5LyLorxCtjnkdE0g&sectime=1598048132

Sectime 是Unix 时间格式。我想做的是:

使用powershell命令(Get-Date -Date((Get-Date).DateTime) -UFormat %s)实时生成系统时间,

将结果附加到 Url 的末尾作为 sectime 的值。

我尝试使用调用运算符连接参数:

Arg1=program
Arg2=parameter
Arg3=Url
& $Arg1 Arg2 $Arg3

现在的问题是我能做到

这就是问题所在。 & $Arg1 Arg2 $Arg3 并且有效,但我无法修改 URL 以采用实时命令 (Get-Date-Date ((Get-Date).DateTime) -UFormat %s) 如果我将 URL arg 分成两个,我不能像这样连接它们

"https://www.example.com/dash/926846/l.php?link=bTP77O5LyLorxCtjnkdE0g§ime=(Get-Date -Date ((Get-Date).DateTime ) -UFormat %s)."

因为是字符串和命令

知道怎么做了吗?

$uriTemplate = 'https://www.example.com/dash/{0}/l.php?link={1}&sectime={2}'

$id = '926846'
$link = 'bTP77O5LyLorxCtjnkdE0g'
$timeunixseconds =  [System.DateTimeOffset]::UtcNow.ToUnixTimeSeconds().ToString()

$uriSting = [String]::Format($uriTemplate, # Template with placeholders {0}, {1}, {2}...
    $id, # first argument goes {0}
    $link, # second goes {1}
    $timeunixseconds ) # third goes {2}, etc etc

$isUriOk = [System.Uri]::IsWellFormedUriString($uriSting, [System.UriKind]::Absolute)

DateTimeOffset class 从某些 .net 版本(4.5th .net、5th powershell、AFAIR)开始有 To(From)UnixTimeSecondsTo(From)UnixTimeMilliseconds 方法。

DateTime (and Get-Date, which returns DateTime) 没有这样的方法!使用 DateTimeOffset.


对于您的原始案例: 在 double-quoted 字符串中,变量(在我看来甚至是简单的)应该添加在 $() 括号中:

"Hello, $($env:USERNAME), it's $(Get-Date -Format 'D') today!"
# Hello, User1, it's Aug 22 2020 today! 

可以在没有 $() 语法的情况下添加单个变量,但我建议始终添加 $()

在 single-quoted 字符串中,$()$var 不起作用:

'Hello, $($env:USERNAME), it is $(Get-Date -Format "D") today!'
# Hello, $($env:USERNAME), it is $(Get-Date -Format "D") today!