Powershell Script Assistance - powershell

I have a script that will query domain controllers to get the expiry date for passwords which works great. I have an issue where I can't access a domain controller directly to run this same query as the only way I can access this domain is via a VPN and a Jumpbox.
What I would like to do is just create a simple script that could access a textfile. Inside this file I'd like to have just a number to represent days since last password reset. Each day this number would decrease. Once the number inside the file was below a certain point I could have an email sent out. I'm just not sure how I can do the process for having the file and decreasing the number each day inside the file. I'd also need to reset it once it reached zero of course.
Any assistance with this would be greatly appreciated.
Thank you in advance.
Steve

A text file will save values as string. So you would have to type cast it to integer before you can do operations.
$File = "C:\temp\ADpasspolicy.txt"
[int]$PassPolicyVal = Get-Content -Path $File
#check for threshold value
if (!($PassPolicyVal -gt 10))
{
#send mail
Send-MailMessage -Subject "Password expired" -To "jane.doe#gmail.com" -From "John#gmail.com" -SmtpServer "smtpserveraddress" -Body "add body here"
}
if ($PassPolicyVal -le 0)
{
$PassPolicyVal = 90
}
else
{
#Decrement by 1 if greater than 0
$PassPolicyVal--
}
$PassPolicyVal | Out-File -FilePath $File

Related

Send emails with Powershell mailto with formated text

My current script creates (after an account modification) a .ps1 file that is send to another computer and there it is executed opening a new Gmail tab with some information hosted in several variables. I need this email to have format like bold, hyper-link, etc.
Im using the start-process 'mailto' for this but i can not find the way to give this email a format (believe me, i have tried), is this even possible?
I appreciate any insights on this.
My current script creates (after an account modification) a .ps1 file that is send to another computer and there it is executed opening a new Gmail tab with some information hosted in several variables. I need this email to have format like bold, hyper-link, etc.
Im using the start-process 'mailto' for this but i can not find the way to give this email a format (believe me, i have tried), is this even possible?
Additional information:
Code:
$outPut = 'Start-Process'
$outPut+= '"mailto:'+$userMail+"?Subject=Password Reset"+"&Body=Hi, your password is $Password"
$outPut+= '";'
$mailFile = "Path" + $user.SAM + ".ps1"
$outPut | Out-File $mailFile
So, this takes the information this way and stored it in a ps1 file, then executed, opening a new Gmail tab with proper data.
I need that some words has format, bold for the password or hyper-link for guideness link...
Regards!
You haven't provided any indication of what you are doing. But the way to send emails via PowerShell is with the Send-MailMessage cmdlet. If you are using Send-MailMessage and formatting the body of the message with HTML, you just need to make sure you are using the -BodyAsHtml argument.
Here's an example:
$html = "<body><h1>Heading</h1><p>paragraph.</p></body>"
Send-MailMessage -To "bob#fake.com" -From "me#fake.com" -Subject "Test" -Body $html -BodyAsHtml

powershell time loop issue

Please I need help i have a script which checks folders on servers, if the folders get above a certain number of files, I run a function to send an email to notify us of the issue. Since the script is running permanently, I tried to set a check in the notification function that sends the emails to check when the last email was sent, if it is less than 15 minutes it just exits the if statement, if the time is after 15 minutes it must send another notification, update the last sent time and the process continues. The issue is the last update time the function is seeing is not seeing the updated variable. I have tried setting the $timesent = Get-Date variable initially outside of the function and internally but still the same issue. its almost like its seeing a different variable.
function sendnotification {
$currenttime = Get-Date
$diff = New-Timespan -Start $timesent -End $currenttime
if( $diff.Minutes -ge 15 ) {
send-MailMessage -SmtpServer $smtp -To $to -From $from -Subject $subject -Body $body -BodyAsHtml -Priority high -Credential $cred
$timesent = Get-Date
}
}

Empty .csv with headers shows count as '1'. When .csv has one entry, count still shows as '1'

We have a .csv file with headers that is auto-populated daily with mobile devices enrolled into our MDM, yet are not entered into our device database.
I have script to parse the .csv, tag the devices in the console, and send out an email to the support team to investigate. The script performs a count of the rows, but runs into a problem when there is no data, meaning the rows are empty except for the headers. I'm guessing this is because the script has no data to perform actions against, yet when this happens, the count still shows as '1'.
When there is only one row populated with data, the count still shows as '1', however, always shows correct counts when more data (rows) is populated.
I'm guessing my best solution would be to simply code in a 'script kill switch', meaning if there is no data in the spreadsheet, the script should stop.
I also wanted to only send an email to support if there is data in the .csv file.
Any suggestions would be helpful.
I've tried many methods to count the rows, but when the spreadsheet is empty it still counts the header. The header is not counted when there are rows populated.
# Path to file
$NoICK = Import-Csv \\server\folder\TrueUp.csv
#Device Counts
$devicecount = ($NoICK | Measure-Object).Count
# Creates a copy file and tags all devices (function not show here)
Foreach ($item in $NoICK){
$noICK | Export-Csv -Path $path -NoTypeInformation
tag_NoICKregistration $item.device_id
}
# Only send email to support if there are devices
If ($devicecount -gt '0'){
Send-MailMessage -From $smtpFrom -To $smtpTo -Subject $messageSubject -Body $body -SmtpServer $smtpServer
}
If the true count is zero, meaning no data was entered into the .csv file, I would like the script to stop after creating the copy, and to not send the email.

Define To: field from from various values

In a CSV file I have various columns and two of those are for emails. Each row within these email columns have a group of emails.
I want to be able to send to these email addresses from a script.
I have everything setup and working, except for the TO:.
The idea of the script is that it loops each line of the csv and generates an email grabbing values from the cells of that row into various parts of the body. Then it sends of an email and loops back to the next line of the CSV to do the same, and so on until it reaches the end of the CSV file.
I'm having issues to plug a variable for the email columns, I'm guessing because the emails don't have "quotations".
How do I bring these in?
In a nutshell for the code
data is imported CSV
a loop is created foreach line of imported data
smtp, from, to, subject, attachments, body variables are defined
then the sendmail-message command is provided.
close the loop
##For the purpose of this, the emaildata.csv looks like this sample:
"NameGroup","emailGroupA","emailGroupB"
"Groupabc","a.b#b.com;c.a#b.com","xyv#b.com;xxd#b,com"
"Grouptrd","ca.r#b.com;as.b#b.com","aaa#a.com;bbb#b.com"
"Groupghd","dd.r#b.com;dd.b#b.com","dddaa#a.com;ddddddb#b.com"
$DataDir = "C:\Users\acastrellon\Documents"
$Data= import-csv $DataDir\emaildata.csv
foreach ($i in $Data) {
$NameGroup = $i.NameGroup
$TeamA = $i.emailGroupA.replace(';',"`n")
$TeamB = $i.emailGroupB.replace(';',"`n")
function send-email {
$smtpserver = " server.smtp"
$from = "myemail.com"
$to = $TeamA,$TeamB
send-MailMessage -From $from -To $to -Subject $subject -Body $body -SmtpServer $smtpServer
}
[string] $subject = "some text here: $NameGroup"
#[string] $attachment = "not here yet"
[string] $body = "
Text here that calls out $NameGroup
This also lists: $TeamA
This here lists $TeamB
Done"
send-email -subject $subject -attachment $attachment -body $body
}
#this should loop to get data from next line of csv and generate a new email with data.
Unlike a batch file where environment variables have a global impact to the current session, PowerShell isolates variables in different scopes.
You're referencing the two $TeamA & $TeamB variables inside a function but you set their values outside the function. Because the function scope (where they are read) is different to the script scope (where you set them) those variables will be empty inside the send-email function in your script.
Have a read on PowerShell scopes as you'll need to make some changes to your script functions; to either read the variables from the script scope ($script:TeamA) or to pass them into the function as a parameter

Powershell novice looking to create script to trigger notifications on missing files

Long time lurker first time poster. I'm looking(of my own initiative) to see if there is a method by which I can check for missing files, that we would expect to receive on a daily basis, and be notified via e-mail.
Our company has what I'd call a relatively unhinged systems infrstructure, that since I arrived I've been chipping away here and there putting in some practices and process' to be more proactive with our monitoring.
Specifically in this case, we receive files via FTP from a vendor, that outlines our Sales and other data. These files go through some validation and the data is then imported into our ERP platform. However I am interested to put in a check, that raises and alert when a file has not been received, when expected.
The last part of that requirement can potentially change, I'm not sure how specific I can get when trying to raise an alert from an expected file.
I'll outline this by stating I'm a relative novice in this area, but there is really no one in my department any the wiser. So I've been looking into powershell.
I've created the following two bits of codes so far, that when executed appear to return files that have been created/last writ, within the last day. This would even be enough, to have this output sent via e-mail. I would be able to spot quickly if an expected file is not in the list.
GET-ChildItem -Path "Path I am checking" |
Where-Object {$_.LastWritetime -gt (get-Date).AddDays(-1)}
The above returns one .csv file. I guess if I get a returned file, then I know its been provided, and if the return is blank/zero, then I know I didn't get a file.
I've used the above for four seperate checks, checking other subfolders in the structure.
To outline the folder structure
\"App server"\"Region"\"Vendor"
There are then the following subfolders
Purchases
Sales
Tenders
VAT
Each of the above four folders then has
Incoming
Processed
I am running my checks on the processed folder for each of the four folder outlined above.
Maybe something like this will help you out:
Function Test-NewerFiles {
# We use parameters as it makes things easy when we need to change things
# CmdLetBinding makes sure that we can see our 'Write-Verbose' messages if we want to
[CmdLetBinding()]
Param (
[String]$Path = 'C:\Users\me\Downloads\Input_Test',
[String]$ExportFile = 'C:\Users\me\Downloads\Log_Test\Attachment.txt'
)
# We first save the date, then we don't need to do this every time again
$CompareDate = (Get-Date).AddDays(-1)
# Then we collect only the folders and check each folder for files and count them
Get-ChildItem -Path $Path -Directory -Recurse | ForEach-Object {
$Files = (Get-ChildItem -Path $_.FullName -File | Where-Object {$_.LastWritetime -gt $CompareDate} | Measure-Object).Count
# If we didn't find files the count is 0 and we report this
if ($Files -eq 0) {
Write-Verbose "No files found in folder $($_.FullName)"
Write-Output $_.FullName
}
# If we found files it's ok and we don't report it
else {
Write-Verbose "Files found in folder $($_.FullName)"
}
}
}
# If you don't want to see output you can remove the '-Verbose' switch
Test-NewerFiles -Verbose
$MyNewFiles = Test-NewerFiles
$MyNewFiles | Out-File -FilePath $ExportFile -Encoding utf8
if ($MyNewFiles) {
$MailParams = #{
To = 'Chuck.Norris#world.com'
From = 'MyScriptServer#world.com'
SmtpServer = 'SMTPServer'
}
Send-MailMessage #MailParams -Priority High -Attachments $ExportFile -Body 'We found problems: check attachment for details'
}
else {
Send-MailMessage #MailParams -Priority Low -Body 'All is ok'
}
The Verbose switch is only used to report progress. So we can see what it does when it's running. But when we use this code in production, we don't need these messages and just use Test-NewerFiles instead of Test-NewerFiles -Verbose.