Powershell get current time for Asia/Manila - powershell

I am working with powershell that retrieves timesheet information from a biometric device. One of the search arguments used to query the timesheet is the date and time. Currently, I retrieve the date and time in the local machine where my powershell is installed. My code inside my powershell script for getting the date is:
get-date -format yyyy-MM-dd
However, if someone changes the date and time of the computer, my query gets affected. Is there a way to retrieve the current datestamp for Asia/Manila using powershell? I dont want to use local machine time anymore.

You can convert the date to universal time before formatting it as a string:
(Get-Date).ToUniversalTime().ToString('yyyy-MM-dd')

With the script from https://gallery.technet.microsoft.com/scriptcenter/Get-Network-NTP-Time-with-07b216ca you can get the time from a ntp server.
download the tool and start it like that:
.\Get-NtpTime -Server asia.pool.ntp.org | Format-List *
You can find other ntp pools oh this site http://www.pool.ntp.org/de/

Related

Is there a way to use wildcards when downloading and saving a file using Powershell?

I have a PowerShell script that I am using to download a file from a URL every morning, however it has started putting the date on it every day, therefore making it imppossible to use a scheduled PowerShell everyday to download it.
Is there anyway to use a wildcard to make it pick up all of the dates?
$WebClient = New-Object System.Net.WebClient
$WebClient.DownloadFile("https://test.com/store/scheduled/320%2019.10.22.csv","\\server\shared\Reports\RawData\Cross Selling Data.csv")
The bold bit is what I am looking to Wildcard after '320' as this is the report number but before .csv. I've tried * but that doesn't seem to work. The date obviously changes on each day's report.
When it comes to using wildcard, the answer is no. It's impossible to use wildcard while making web requests as it'd require checking every possible path.
What is possible is to construct the path programmatically if you know the pattern. For example, in your case
# That input
PS> Get-Date -Format 'yy.MM.dd'
# Gives you the following result
19.10.22
which looks similarly to the pattern you included in your code (I assume %20 is space). To use it in string (assuming you use double quotes ") you can use the following:
"https://test.com/store/scheduled/320%20$(Get-Date -Format 'yy.MM.dd').csv"
Last, but not least, as #Tomalak mentioned in comments, Invoke-WebRequest might work for you as well.

Power shell Script to update CSV file date time fields from UTC to local time

We have an automatic scheduled export of Salesforce data into CSV files in specific folder(everyday at 10pm).
In each CSV file there are two datetime columns which are in UTC time format [2018-01-30T05:27:26.000Z].
My requirement is to create a script to read those CSV files and update date time columns into local time zone and format (dd/mm/yyyy hh:mm:ss).
And I need to schedule this script to run everyday (everyday at 10.30pm).
(we know the folder path, file name, column name).
Please help with the script sample.
Thank you
Ditto to what Robert Cotterman has stated.
However, with This well could be considered a duplicate of this discussion.
Convert list of UTC to Current TimeZone
$UTCTime = GC "C:\Scripts\UTC.txt"
$results = '' | Select UTCTime,PST
Foreach ($newtime in $UTCTime){
$strCurrentTimeZone = (Get-WmiObject win32_timezone).StandardName
$TZ = [System.TimeZoneInfo]::FindSystemTimeZoneById($strCurrentTimeZone)
$LocalTime = [System.TimeZoneInfo]::ConvertTimeFromUtc($newtime, $TZ)
$results.UTCTime = $newtime
$results.PST = $LocalTime
$results
}
This answer will get you started, and that last part is just use the script with a scheduled task.

Automating batch script based on end of month

We have a batch file that runs an end-of-month process. Right now it's a manual process, but we'd like to automate it based on when EOM falls. If the last work day of the month is a Friday (or other weekday), we run the script on Friday night or Saturday. If it falls on a Saturday or Sunday, the script is run on Monday following the weekend. There may be a few exceptions, but that's the general idea.
We're having trouble figuring out how to automate this based on date. Any options will be considered. Powershell, batch, etc...
Any help would be greatly appreciated.
Edit - The dates it selects to run can be a bit random. If we could have it read in a text file with a list of dates to run that would work too.
So we could have a list of dates like:
04-30-2015,
05-31-2015,
06-29-2015,
Then a script could be run that says if today is equal to any of these dates, run the batch file.
The logic isn't completely clear to me, but as said in the comment above, you could run a PowerShell script using Windows Task Scheduler every day (or only Friday/Monday?) and have that script check if the time is right to do something.
From what I can tell it either has to run on Friday or Monday.
You can get the current date in PowerShell with the Get-Date command.
If you pass this through Get-Member you can see all the methods you have on the date object to figure out if the time is right to do something.
get-date | get-member
You'll probably need some methods or properties like this to implement the check:
$today = get-date
$today.DayOfWeek # prints e.g. "Monday"
$today.DayOfWeek -eq 1 # Returns True on Monday
$today.AddDays(1) # Next day, the number can be negative or positive
$today.Day # Returns 6 right now (april 6th)
There are plenty of resources that discuss calling a PowerShell script in Task Scheduler. If what you currently do is run a batch then configure your task to run at 5:00pm every day checking the date against all the dates in your text file.
$milestones = Get-Content c:\temp\dates.txt
$today = Get-Date -Format "MM-dd-yyyy"
If($milestones -contains $today){
# Do stuff and things.
# cmd.exe /K C:\Path\To\Batch.bat
}
If there was a line in the text file "c:\temp\dates.txt" for "04-06-2015" that would satisfy the If condition. Then you could uncomment the line with cmd and update as required.
If you have issues with these concepts it is expected that you do a little research before you ask. If you are still stuck after that please either edit your question of ask a new question.

Powershell (exchange message search)

I've literally just C+P this code into my PowerShell, related to this question I asked on Super User.
It gives me exactly what I'm looking for but only for today's emails, can someone tell me how to add a a filter for emails the last two months for example? I looked through the code and I can't see anything related to data range so there might be another cmdlet that needs to be ran?
From a cursory glance at the script you linked, the individual emails appear to be iterated through in a foreach loop that uses the output of Get-MessageTrace as the collection of objects to iterate through. According to this documentation, Get-MessageTrace has a -StartDate and -EndDate parameter that you can specify a range of dates with.
So you just need to use those parameters to get a longer range of dates. Here's an untested example of what you'll probably need to do for the past two months:
Get-MessageTrace -StartDate (Get-Date).AddMonths(-2) -EndDate (Get-Date)
Edit: According to the parameter documentation, you may have to do some additional formatting after getting the date. I'm unfortunately not somewhere where I can test this, but here's what it says:
Use the short date format defined in the Regional Options settings for the computer on which the command is run. For example, if the computer is configured to use the short date format mm/dd/yyyy, enter 03/01/2010 to specify March 1, 2010. You can enter the date only, or you can enter the date and time of day. If you enter the date and time of day, you must enclose the argument in quotation marks ("), for example, "10/05/2010 5:00 PM".
This may not be necessary since you're already passing a DateTime object (the output of Get-Date) instead of a string... but worth mentioning, if you simply want to hard-code a string instead of getting the current time.

How can I use get-date to change the month while controlling the format in Powershell

I just started working with Powershell and I am trying to manipulate a date such that I add two months to the date and set it as a var. Powershell handles this nicely and even handles year rollover. However I am stuck trying to figure out how to also control the format of the date before, during or after adding two months to the date. Both of the statements below give me what I want but i have not been able to figure out how to combine them.
$ndate = (Get-Date).AddMonths(2)
$exp = date -format MM/dd/yyyy
Thank you,
Get-Date (Get-Date).AddMonths(2) -f MM/dd/yyyy
When the -Format operator is not avaiable you can use the ToString method:
(Get-Date).AddMonths(2).ToString('MM/dd/yyyy')