modifying output from MonthCalendar

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 11 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
nodgiles
Posts: 11
Last visit: Mon Aug 10, 2015 2:26 pm

modifying output from MonthCalendar

Post by nodgiles »

I'm using the MonthCalendar in a project to scheduled tasks on a remote server. Unfortunately if you pick a single digit day such as the ninth or month such as February the output is 2/9/2012. Powershell doesn't recognize single digits as valid entries. How do I modify the MonthCalendar so that it outputs as 02/09/2012 and not 2/9/2012?

$m = $monthcalendar1.SelectionEnd.month
$d = $monthcalendar1.SelectionEnd.day
$y = $monthcalendar1.SelectionEnd.Year

Function Create-ScheduledTask {
param(
[string]$ComputerName2 = "myserver",
[string]$RunAsUser2 = "domainaccount",
[string]$RunPass2 = "password",
[string]$TaskName2 = "$taskname",
[string]$TaskRun2 = "'PowerShell.exe -NoLogo -File myserverc$powershelldisable-userDisableUserSchedualed.ps1'",
[string]$Schedule = "$hour"+":"+"$minute",
[string]$Date2 = "$m"+"/"+"$d"+"/"+"$y"
)

$Command2 = "schtasks.exe /create /s $ComputerName2 /U $RunAsUser2 /P $RunPass2 /ru $RunAsUser2 /rp $RunPass2 /tn $TaskName2 /tr $TaskRun2 /sc ONCE /sd $date2 /st $Schedule /F"
Invoke-Expression $Command2
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

modifying output from MonthCalendar

Post by davidc »

The DateTime object has a ToString function that allows you to format the date:

http://msdn.microsoft.com/en-us/library ... tring.aspx

In your case, you will need to do the following:

$date.ToString('MM/dd/yyyy')

David
David
SAPIEN Technologies, Inc.
This topic is 11 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked