?
Solved

Getting the time updated in powershell

Posted on 2014-03-28
4
Medium Priority
?
233 Views
Last Modified: 2014-04-11
I have the need to mark the time in some logs that are created by powershell scripts. I use

$date = get-date

The first time the $date variable is used, the date is correct. Iterations following all have the same date. How can I clear the $date variable so that every time I call it, the current date is used?

clear-variable $date shows error shown:
Clear-Variable : Cannot find a variable with name '03/28/2014 17:19:13'.
0
Comment
Question by:LN41
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 39963058
Just set date to null at the end of each iteration


#Start of loop
$date = Get-Date


End of loop
$date = $null
0
 
LVL 40

Accepted Solution

by:
footech earned 2000 total points
ID: 39963075
Instead of assigning the results of Get-Date to a variable, if you're not going to use that variable again, just call Get-Date where you need it.

For example
"start time -- $(Get-Date)" | Add-content file.txt
0
 
LVL 12

Expert Comment

by:Carlo-Giuliani
ID: 39964297
Footech has the right idea.  Just replace all occurrences of $date with (get-date).

But if you want to use Clear-Variable, the correct way to do it is:
Clear-Variable 'Date'

Open in new window

0
 
LVL 29

Expert Comment

by:becraig
ID: 39965177
Both of the guys above are right unless you are trying to do something midstream in your flow which cause you to get an error when calling Get-Date.

Having an idea of the snippet of code you are trying to fix would help.

Since you indicate clear variable gives you and error you can simply go for:
#Start of loop
$date = $null 
$date = Get-Date

Open in new window


#Do stuff
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question