Page 1 of 1

PowerShell - Expirate the Program

Posted: Tue Oct 08, 2019 3:15 pm
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.

Re: PowerShell - Expirate the Program

Posted: Tue Oct 08, 2019 3:22 pm
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" )

Re: PowerShell - Expirate the Program

Posted: Tue Oct 08, 2019 3:42 pm
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...

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

Re: PowerShell - Expirate the Program

Posted: Tue Oct 08, 2019 4:15 pm
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...

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.

Re: PowerShell - Expirate the Program

Posted: Sat Oct 12, 2019 6:20 am
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