如何在Powershell中创建动态变量,如日期/时间等 [英] How to create a dynamic variable in Powershell, sucha as date/time etc

查看:622
本文介绍了如何在Powershell中创建动态变量,如日期/时间等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我的措辞是否正确,但我需要一个变量,其中包含当前日期/时间,每当我写数据到日志;我每次都需要更新我需要更新的时候,我可以一起使用这两个语句。有另外一种方法吗?

  $ DateTime = get-date |选择datetime 
添加内容$ LogFile -Value$ DateTime.DateTime:XXXXX

如果有任何关于我的问题的问题或澄清,请告诉我。

解决方案

为什么不使用:

$ b $ $ $((Get-Date).DateTime):XXXXX
c $ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆