如何解决使用Powershell操纵带有真实实时系统数据的URL标头
我正在尝试使用Powershell为正在访问Web网址的命令程序操纵url值。
基本上情况就是这样
在Powershell中运行:
程序参数https://www.example.com/dash/926846/l.php?link=bTP77O5LyLorxCtjnkdE0g§ime=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) 如果我将网址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}§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)
DateTimeOffset
类以某个.net版本(4.5th .net,5th powershell,AFAIR)开头,具有To(From)UnixTimeSeconds
和To(From)UnixTimeMilliseconds
方法。
DateTime
(还有Get-Date,它返回DateTime)没有这种方法!使用DateTimeOffset
。
对于您的原始情况:
在双引号字符串中,应将变量(在我看来,甚至很简单)都放在$()
括号中:
"Hello,$($env:USERNAME),it's $(Get-Date -Format 'D') today!"
# Hello,User1,it's Aug 22 2020 today!
可以不使用$()
语法添加单个变量,但建议始终添加$()
。
在单引号字符串内,$()
或$var
不起作用:
'Hello,it is $(Get-Date -Format "D") today!'
# Hello,it is $(Get-Date -Format "D") today!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。