我正试图使用powershell来操作正在访问web url的命令程序的url值。
基本上情况是这样的
在Powershell中运行:
程序参数https://www.example.com/dash/926846/l.php?link=bTP77O5LyLorxCtjnkdE0g&扇区时间=1598048132
Sectime采用Unix时间格式。我想做的是:
使用powershell命令(Get-Date-Date((Get-Date(.DateTime(-UFormat%s(实时生成系统时间
将结果作为sectime的值附加到Url的末尾。
我试图使用调用运算符连接这些参数
Arg1=program
Arg2=parameter
Arg3=Url
& $Arg1 Arg2 $Arg3
现在的问题是我可以做
这就是问题所在&Arg1Arg2$Arg3,这很有效,但我不能修改URL以使用实时命令(Get-Date-Date((Get-Date(.DateTime(-UFormat%s(如果我把URL arg分成两个,我不能不把它们像一样连接起来
"https://www.example.com/dash/926846/l.php?link=bTP77O5LyLorxCtjnkdE0g§ime=(获取日期-日期((获取日期(.DateTime(-U格式%s(">
由于它是一个字符串和一个命令
一个如何做到这一点的想法?
$uriTemplate = 'https://www.example.com/dash/{0}/l.php?link={1}§ime={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)
以一些.net版本(4.5th.net,5thpowershell,AFAIR(开始的DateTimeOffset
类具有To(From)UnixTimeSeconds
和To(From)UnixTimeMilliseconds
方法。
DateTime
(和GetDate,返回DateTime(没有这样的方法!使用DateTimeOffset
。
对于您的原始案例:在双引号字符串中,变量(在我看来甚至很简单(应该添加到$()
括号中:
"Hello, $($env:USERNAME), it's $(Get-Date -Format 'D') today!"
# Hello, User1, it's Aug 22 2020 today!
可以在没有$()
语法的情况下添加单个变量,但我建议始终添加$()
。
在单引号字符串中,$()
或$var
不起作用:
'Hello, $($env:USERNAME), it is $(Get-Date -Format "D") today!'
# Hello, $($env:USERNAME), it is $(Get-Date -Format "D") today!