PowerShell - Expirate the Program

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 4 years and 5 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
sergiors
Posts: 3
Last visit: Wed Jul 08, 2020 4:41 pm

PowerShell - Expirate the Program

Post by sergiors »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PowerShell - Expirate the Program

Post by jvierra »

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: 34
Last visit: Mon Oct 30, 2023 10:52 pm
Has voted: 2 times

Re: PowerShell - Expirate the Program

Post by Nillth »

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...
  1. [System.IO.FileInfo]$Filename = $($hostinvocation.MyCommand.path)
  2.  
  3. #region Add this Region to Expire
  4. ##Needs to either be added above Main, or in a seperate file
  5. $DTCurrent = get-date
  6. $DTExpiry = [datetime]::new(2019, 10, 30)
  7. if ($DTCurrent -gt $DTExpiry)
  8. {
  9.     Write-Host "Thats all folks, $($Filename.Name) is expired"
  10.     break
  11. }
  12. else
  13. {
  14.     Write-Host "Keep Running: $($Filename.Name)"
  15. }
  16. #endregion Add this Region to Expire
Attachments
Test-AppRun.zip
(1.71 KiB) Downloaded 158 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PowerShell - Expirate the Program

Post by jvierra »

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...
  1. [System.IO.FileInfo]$Filename = $($hostinvocation.MyCommand.path)
  2.  
  3. #region Add this Region to Expire
  4. ##Needs to either be added above Main, or in a seperate file
  5. $DTCurrent = get-date
  6. $DTExpiry = [datetime]::new(2019, 10, 30)
  7. if ($DTCurrent -gt $DTExpiry)
  8. {
  9.     Write-Host "Thats all folks, $($Filename.Name) is expired"
  10.     break
  11. }
  12. else
  13. {
  14.     Write-Host "Keep Running: $($Filename.Name)"
  15. }
  16. #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
Last visit: Wed Jul 08, 2020 4:41 pm

Re: PowerShell - Expirate the Program

Post by sergiors »

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
This topic is 4 years and 5 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