PowerShell - Expirate the Program

Ask your Windows 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.
Post Reply
sergiors
Posts: 3
Joined: Tue Oct 08, 2019 3:11 pm

PowerShell - Expirate the Program

Post by sergiors » Tue Oct 08, 2019 3:15 pm

Hello

I developed an application in PowerShell with windows Forms.
I would like to make a condition for the executable to expire on the date I determine.
I made this condition in another application that I developed in VB.NET, but I didn't find how to do it in power shell

in VB it looked like this:

Dim dt1 = DateTime.Now
Dim dt2 = DateTime.Parse("30/09/2019")

If dt1 >= dt2 Then
MsgBox("Expired Date - Please Contact Administrator")
Application.Exit()

End If

Does anyone know how I do this in powerShell? no need to have msgbox, just want the program not to run, not open, do nothing when it arrives on the set date

Thank you very much in advance.

User avatar
jvierra
Posts: 13793
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: PowerShell - Expirate the Program

Post by jvierra » Tue Oct 08, 2019 3:22 pm

help Get-Date -online

or

[datetime]::ParseExact('30/09/2019','dd/MM/yyyy',$null)

For a message box use the Microsoft.VisualBasic namespace to access the MsgBox.

[Microsoft.VisualBasic.Interaction]::MsgBox( " my message" )

User avatar
Nillth
Posts: 15
Joined: Thu Aug 01, 2013 6:14 pm
Location: Melbourne, Australia

Re: PowerShell - Expirate the Program

Post by Nillth » Tue Oct 08, 2019 3:42 pm

Example Project Attached,

Super simple addition to your code required... I also have a more advanced version
Where you can use the creation date (signing time stamp) +x days,
So each build expires after a defined period of time...

Code: Select all

[System.IO.FileInfo]$Filename = $($hostinvocation.MyCommand.path)

#region Add this Region to Expire
##Needs to either be added above Main, or in a seperate file
$DTCurrent = get-date
$DTExpiry = [datetime]::new(2019, 10, 30)
if ($DTCurrent -gt $DTExpiry)
{
Write-Host "Thats all folks, $($Filename.Name) is expired"
break
}
else
{
Write-Host "Keep Running: $($Filename.Name)"
}
#endregion Add this Region to Expire
Attachments
Test-AppRun.zip
(1.71 KiB) Downloaded 3 times

User avatar
jvierra
Posts: 13793
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: PowerShell - Expirate the Program

Post by jvierra » Tue Oct 08, 2019 4:15 pm

Nillth wrote:
Tue Oct 08, 2019 3:42 pm
Example Project Attached,

Super simple addition to your code required... I also have a more advanced version
Where you can use the creation date (signing time stamp) +x days,
So each build expires after a defined period of time...

Code: Select all

[System.IO.FileInfo]$Filename = $($hostinvocation.MyCommand.path)

#region Add this Region to Expire
##Needs to either be added above Main, or in a seperate file
$DTCurrent = get-date
$DTExpiry = [datetime]::new(2019, 10, 30)
if ($DTCurrent -gt $DTExpiry)
{
Write-Host "Thats all folks, $($Filename.Name) is expired"
break
}
else
{
Write-Host "Keep Running: $($Filename.Name)"
}
#endregion Add this Region to Expire
Do not use break in this way as it can create unexpected issues. An if/else filter never requ9ies a break.

"[codebox}" is not correct and will do nothing.. Use "

Code: Select all

" to post code.

sergiors
Posts: 3
Joined: Tue Oct 08, 2019 3:11 pm

Re: PowerShell - Expirate the Program

Post by sergiors » Sat Oct 12, 2019 6:20 am

it worked with the code below

$DTCurrent = get-date
$DTExpiry = [datetime]::ParseExact('30/11/2019','dd/MM/yyyy',$null)

if ($DTCurrent -gt $DTExpiry)
{
Write-Host "Thats all folks, $($Filename.Name) is expired"
break
}
else
{
Write-Host "Keep Running: $($Filename.Name)"
}

Thank you all

Post Reply