I am writing a script for “My Birthday is *** days away!” countdown and convert it to HTML as MyBirthday.html.
Idid this so far
function Date {
$Name = Read-Host "Please enter your name"
$BirthMonth = Read-Host "Please enter your Month of Birthday"
$BirthDay = Read-Host "Please enter your Day of Birthday"
$BirthYear = (Get-Date).Year
$NumberOfDays=(New-TimeSpan -End "$BirthYear/$BirthMonth/$BirthDay").Days
if ($NumberOfDays -eq 0) {
Write-Host "Happy Birthday $Name. Today is your Birthday! Yeah!"
}
else {
Write-Host "Hello $Name, there are $NumberOfDays days to your Birthday!"
}
}
Out-File Date > C:\Users\Desktop\Date.html
Ideally you want to keep everything as an object (in this case a DateTime object) as it's much easier to manipulate.
I would do it as follows - I've annotated with comments, but if you have any questions ask.
function Date ($Name,$BirthMonth,$BirthDay)
{
#Create a datetime object
$Date = Get-Date -day $BirthDay -month $BirthMonth -year ((Get-Date).Year)
#Check if the entered date is before today
if($Date -lt (Get-Date))
{
#If so add 1 year
$Date = $Date.AddYears(1)
}
$NumberOfDays = New-Timespan -Start (Get-Date) -End $date
if ($NumberOfDays.Days -eq 0)
{
$result = "Happy Birthday $Name. Today is your Birthday! Yeah!"
}
else
{
$result = "Hello $Name, there are $($NumberOfDays.Days) days to your Birthday!"
}
#Create an object to return (Objects are easier and more flexible to work with down the pipeline)
$return = New-Object PSObject -Property #{
'Days Remaining' = $result
}
#Return the object
$return
}
#User input outside of the function (makes the function re-usable)
$Name = Read-Host "Please enter your name"
$BirthMonth = Read-Host "Please enter your Month of Birthday"
$BirthDay = Read-Host "Please enter your Day of Birthday"
#Call the function, pass the input and pipe the output
Date $Name $BirthMonth $BirthDay | ConvertTo-HTML > C:\users\jacob\desktop\date.html
#Demonstration of the returned object
Date $Name $BirthMonth $BirthDay
Related
I am fairly new to PowerShell and this maybe straight forward for a professional.
I am looking to extend expiration date a bulk of AD usernames in a text file by 6 months.
Preferably if the code could pick up the current date and extend from there.
As I have been doing some googling and testing I am come up with the command to do a single account in PowerShell:
Set-ADAccountExpiration SMahmood -DateTime "06/11/2022"
The above command I obviously have to change the username and date (if I run the command on different day) every time I run the command.
I have also managed to find some script of another person who asked a similar question but his script asks you to define the username each time you would like to extend it (this is not my code but has been tested as working) :
$continue = $true
while ($continue) {
write-host " AD Account Expiration Date Changer" -ForegroundColor White
Write-Host ""
while ($true) {
try {
# Loop until a valid username is entered
$Entered_Username_0 = Read-Host "Enter a username"
$Entered_Username = $Entered_Username_0.Trim()
if (Get-ADUser -Identity $Entered_Username | Out-Null) {
throw
}
break
}
catch {
Write-Host ""
Write-Host "Invalid username entered!" -ForegroundColor Red
Write-Host ""
}
}
$dateMin = [datetime]::Now
$dateMin_short = $dateMin.ToShortDateString()
Write-Host "Press 1 to extend the account expiration date by 6 months"
Write-Host "Press 2 to extend the account expiration date to a sprecific date"
$Choice_input = Read-Host "Please select an option"
while ($true) {
try {
if ($Choice_input -eq 2) {
while ($true) {
try {
# Loop until a valid Date is entered and that Date is above $dateMin
$Entered_Date = [datetime]::ParseExact(
(Read-Host "Enter a new expiry date, in the format DD/MM/YYYY"),
'dd/MM/yyyy',
[System.Globalization.CultureInfo]::new('en-GB')
)
if ($Entered_Date -lt $dateMin) {
throw
}
break
}
catch {
Write-Host ""
Write-Host "Invalid date entered! Format must be DD/MM/YYYY and higher than $dateMin_short." -ForegroundColor Red
Write-Host ""
}
}
}
if ($Choice_input -eq 1) {
$Entered_Date = [datetime]::Now.addmonths(6)
}
else {
throw
}
break
}
catch {
Write-Host ""
Write-Host "Please input a either 1 or 2." -ForegroundColor Red
Write-Host ""
}
}
try {
Set-ADAccountExpiration -Identity $Entered_Username -DateTime $Entered_Date.AddHours(24)
Write-Host ""
Write-Host "New account expiration date for $Entered_Username is $(($Entered_Date).toString('dd/MM/yyyy'))"-ForegroundColor Green
$Entered_Date = ($Entered_date).toString('dd/MM/yyyy')
}
catch {
Write-Host ""
Write-Host "Unable to set account expiry: $_"-ForegroundColor Red
}
Write-Host ""
}
$continue = $true
while ($continue) {
write-host " AD Account Expiration Date Changer" -ForegroundColor White
Write-Host ""
while ($true) {
try {
# Loop until a valid username is entered
$Entered_Username_0 = Read-Host "Enter a username"
$Entered_Username = $Entered_Username_0.Trim()
if (Get-ADUser -Identity $Entered_Username | Out-Null) {
throw
}
break
}
catch {
Write-Host ""
Write-Host "Invalid username entered!" -ForegroundColor Red
Write-Host ""
}
}
$dateMin = [datetime]::Now
$dateMin_short = $dateMin.ToShortDateString()
Write-Host "Press 1 to extend the account expiration date by 6 months"
Write-Host "Press 2 to extend the account expiration date to a sprecific date"
$Choice_input = Read-Host "Please select an option"
while ($true) {
try {
if ($Choice_input -eq 2) {
while ($true) {
try {
# Loop until a valid Date is entered and that Date is above $dateMin
$Entered_Date = [datetime]::ParseExact(
(Read-Host "Enter a new expiry date, in the format DD/MM/YYYY"),
'dd/MM/yyyy',
[System.Globalization.CultureInfo]::new('en-GB')
)
if ($Entered_Date -lt $dateMin) {
throw
}
break
}
catch {
Write-Host ""
Write-Host "Invalid date entered! Format must be DD/MM/YYYY and higher than $dateMin_short." -ForegroundColor Red
Write-Host ""
}
}
}
if ($Choice_input -eq 1) {
$Entered_Date = [datetime]::Now.addmonths(6)
}
else {
throw
}
break
}
catch {
Write-Host ""
Write-Host "Please input a either 1 or 2." -ForegroundColor Red
Write-Host ""
}
}
try {
Set-ADAccountExpiration -Identity $Entered_Username -DateTime $Entered_Date.AddHours(24)
Write-Host ""
Write-Host "New account expiration date for $Entered_Username is $(($Entered_Date).toString('dd/MM/yyyy'))"-ForegroundColor Green
$Entered_Date = ($Entered_date).toString('dd/MM/yyyy')
}
catch {
Write-Host ""
Write-Host "Unable to set account expiry: $_"-ForegroundColor Red
}
Write-Host ""
}
Big thanks Vihaan Reyansh who provided the script above I had to tweak it a bit as it was changing the description field.
Any ideea how can I get file from server via sftp by last modify date? (I have this script but I don't know how to implement)
Thank you,
$TodaysDate = Get-Date -UFormat "%d.%m.%Y %R"
function Read-Date {
param(
[String] $prompt
)
$result = $null
do {
$s = Read-Host $prompt
if ( $s ) {
try {
$result = Get-Date $s
break
}
catch [Management.Automation.PSInvalidCastException] {
Write-Host "Date not valid"
}
}
else {
break
}
}
while ( $true )
$result
}
Write-Host "Today's date: $TodaysDate"
$startDate = Read-Date "Enter Start Date"
$endDate = Read-Date "Enter End Date"
Get-SCPFolder -ComputerName $ComputerName -Credential $credentials -RemoteFolder $RemoteLogFolder -LocalFolder $LocalLogFolder -Verbose
I am having one source file(CertExpiry.csv) in csv format below:
CertName,Expiry,AlertEmail,UserName,WhereUsed,Type,Additional Info
something.test1.com,11/21/2021,tom#xyz.com,Tom,Test-WEU-APPGW,Internal CA,
something.test2.com,7/15/2021,"harry#xyz.com,tom#xyz.com",Harry,Test-SEA-APPGW,Internal CA,
something.test3.com,7/16/2021,"tom#xyz.com,harry#xyz.com",Tom,Test-EUS-APPGW,External CA,
something.test4.com,not set,"tom#xyz.com,harry#xyz.com",Tom,Test-EUS-APPGW,External CA,
and I have written below script to print some data based on matching condition but unfortunately I am not able to enter into else condition and its not printing data as per my wish.
$data=import-csv .\CertExpiry.csv
$days = "-30"
foreach($i in $data) {
$CertName = $i.CertName
$Expiry = $i.Expiry
$AlertEmail = $i.AlertEmail
$UserName = $i.UserName
$WhereUsed = $i.WhereUsed
$Type = $i.Type
write-host "$CertName - $Expiry" -foregroundcolor magenta
if($Expiry -eq "not set"){
write-host "Cert expiration date is not set for $CertName" -foregroundcolor Green
}
else {
$Expiry = get-date $Expiry
$Expiry1 = ($Expiry).adddays($days)
if($Expiry1 -le $date){
write-host "Cert $CertName will expire on $Expiry and alert email is $AlertEmail with $UserName and $WhereUsed and $Type" -foregroundcolor red
}
}
}
Abraham already pointed out where the error was ($date was never defined). I also added some improvements, the Limit Date should be defined outside the loop:
$csv = #'
CertName,Expiry,AlertEmail,UserName,WhereUsed,Type,Additional Info
something.test1.com,11/21/2021,tom#xyz.com,Tom,Test-WEU-APPGW,Internal CA,
something.test2.com,7/15/2021,"harry#xyz.com,tom#xyz.com",Harry,Test-SEA-APPGW,Internal CA,
something.test3.com,7/16/2021,"tom#xyz.com,harry#xyz.com",Tom,Test-EUS-APPGW,External CA,
something.test4.com,not set,"tom#xyz.com,harry#xyz.com",Tom,Test-EUS-APPGW,External CA,
something.test5.com,6/16/2021,"tom#xyz.com,harry#xyz.com",Tom,Test-EUS-APPGW,External CA,
'# | ConvertFrom-Csv
$limitDate = (Get-Date).AddDays(-30)
foreach($i in $csv)
{
$CertName = $i.CertName
$Expiry = $i.Expiry -as [datetime]
$AlertEmail = $i.AlertEmail
$UserName = $i.UserName
$WhereUsed = $i.WhereUsed
$Type = $i.Type
if(-not $Expiry)
{
Write-host "Cert expiration date is not set for $CertName" -ForegroundColor Green
continue
}
if($Expiry -le $limitDate)
{
Write-Host "Cert $CertName will expire on $Expiry and alert email is $AlertEmail with $UserName and $WhereUsed and $Type" -ForegroundColor Red
continue
}
Write-Host "$CertName - $Expiry" -ForegroundColor Magenta
}
This results in:
something.test1.com - 11/21/2021 00:00:00
something.test2.com - 07/15/2021 00:00:00
something.test3.com - 07/16/2021 00:00:00
Cert expiration date is not set for something.test4.com
Cert something.test5.com will expire on 06/16/2021 00:00:00 and alert email is tom#xyz.com,harry#xyz.com with Tom and Test-EUS-APPGW and External CA
All I'm trying to do is to see if the user input $month is in the array $months. But it's not liking something. Help?
Write-host "The script is to collect from the user High Tempature and Low Tempature for a day in degrees F."
$months = #("January", "February","March","April","May","June","July","August","September","October","November","December")
$finished = $false
while ($finished -eq $false){
$month = read-host "Enter the month";
if ($months -Contains $month)
{
write-host "Invalid entry"
$finished = $false
}
else
{
$finished = $true
}
}
You test logic is just not the good one, just reverse youy test or reverse your actions:
Write-host "The script is to collect from the user High Tempature and Low Tempature for a day in degrees F."
$months = #("January", "February","March","April","May","June","July","August","September","October","November","December")
$finished = $false
while ($finished -eq $false){
$month = read-host "Enter the month";
if ($months -Contains $month)
{
$finished = $true
}
else
{
write-host "Invalid entry"
$finished = $false
}
}
Instead of using -Contains you should just run a RegEx match using the -Match operator. Or, as you are currently testing for a negative result, use -notmatch instead. You can use your existing code, just modify it a little by joining your months with a pipe character. Like:
Write-host "The script is to collect from the user High Tempature and Low Tempature for a day in degrees F."
$months = #("January", "February","March","April","May","June","July","August","September","October","November","December")
$finished = $false
while ($finished -eq $false){
$month = read-host "Enter the month";
if ($month -notmatch ($months -join "|"))
{
write-host "Invalid entry"
$finished = $false
}
else
{
$finished = $true
}
}
Better yet, let's get rid of the If/Else and shorten this. Move the Join to where we define $Months, and then ask for a month and if it isn't a match ask for it again until it is with a While.
$months = #("January", "February","March","April","May","June","July","August","September","October","November","December") -join '|'
$month = read-host "Enter the month"
While($month -notmatch $months){
"Invalid Entry"
$month = read-host "Enter the month"
}
How do I put all of the below I have into one line so the user is able to enter at once?
So, instead of it popping up with a Read-Host every time they want to enter something in - it pops up once and states what you want to enter the date as..
So - "Please enter in FULL Date with time - ie 25 Oct 2012 9:00"
Also - if someone enters in a weird character, how do I let it error out?
$Day = $(Read-Host "Enter day of month")
if ($Day -eq ''){$Day = Get-Date -format dd}
$Month = $(Read-Host "Enter Month of Year")
if ($Month -eq ""){$Month = Get-Date -format MM}
$Year = $(Read-Host "Enter Year")
if ($Year -eq "") {$Year = Get-Date -format yy}
$Hour = $(Read-Host "Enter Hour")
if ($Hour -eq "") {$Hour = Get-Date -Format HH}
$Minute = $(Read-Host "Enter Minute")
if ($Minute -eq "") {$Minute = Get-Date -Format mm}
$Second = $(Read-Host "Enter Hour")
if ($Second -eq "") {$Second = Get-Date -Format ss}
You can do something like this:
do
{
$date= read-host "Please enter date & time (i.e.: '25/12/2012 09:00', '25 oct 2012 9:00'; date alone set time to 00:00):"
$date = $date -as [datetime]
if (!$date) {
"Not A valid date and time"
}
} while ($date -isnot [datetime])
$date