如何在Powershell中创建动态变量,如日期/时间等 [英] How to create a dynamic variable in Powershell, sucha as date/time etc
本文介绍了如何在Powershell中创建动态变量,如日期/时间等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$ DateTime = get-date |选择datetime
添加内容$ LogFile -Value$ DateTime.DateTime:XXXXX
如果有任何关于我的问题的问题或澄清,请告诉我。
解决方案
为什么不使用:
$ b $ $ $((Get-Date).DateTime):XXXXXc $ c $ / pre>
每次都获取当前的日期时间。请注意,它在 $()
内部,这使得PowerShell在将其插入字符串之前运行表达式(获取datetime)。
Hi i am not exactly sure if my wording is right but i need a variable which contains current date/time whenever i write data to log ; how can i do that without initializing everytime.Currently everytime i need a update i use these both statements jointly.Is there an other way of doing this?
$DateTime = get-date | select datetime
Add-Content $LogFile -Value "$DateTime.DateTime: XXXXX"
please do let me know if any questions or clarifications regarding my question.
解决方案
Why not use:
Add-Content $LogFile -Value "$((Get-Date).DateTime): XXXXX"
This gets the current datetime every time. Notice that it's inside $( )
which makes powershell run the expression(get the datetime) before inserting it into the string.
这篇关于如何在Powershell中创建动态变量,如日期/时间等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文