Solved

Getting the time updated in powershell

Posted on 2014-03-28
4
226 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
  • 2
4 Comments
 
LVL 28

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 39

Accepted Solution

by:
footech earned 500 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 28

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now