I'm new to Powershell and am having trouble joining together two scripts I have.
What I want to do is check the length of all the csv files within a particular folder and if any of them are 0 Kb, I want to send off an alert email. So far I have a script which sends an email successfully and I have a script which checks the size successfully, but I am having trouble joining the two together.
Ideally it would send the name of the files which are empty in the body of the email.
The code below checks the file size and if it is greater than 1Kb it returns true.
$file = 'FilePath\File1.csv'
$Result = if (Test-Path $file) { (Get-Item $file).length -gt 1kb }
if ($Result -eq "True") {"File1.csv Contains Data"} ELSE {"File1.csv is Empty!"}
$file = 'FilePath\File2.csv'
$Result = if (Test-Path $file) { (Get-Item $file).length -gt 1kb }
if ($Result -eq "True") {"File2.csv Contains Data"} ELSE {"File2.csv is Empty!"}
$file = 'FilePath\File3.csv'
$Result = if (Test-Path $file) { (Get-Item $file).length -gt 1kb }
if ($Result -eq "True") {"File3.csv Contains Data"} ELSE {"File3.csv is Empty!"}
$file = 'FilePath\File4.csv'
$Result = if (Test-Path $file) { (Get-Item $file).length -gt 1kb }
if ($Result -eq "True") {"File4.csv Contains Data"} ELSE {"File4.csv is Empty!"}
$file = 'FilePath\FileName5.csv'
$Result = if (Test-Path $file) { (Get-Item $file).length -gt 1kb }
if ($Result -eq "True") {"File5.csv Contains Data"} ELSE {"File5.csv is Empty!"}
$file = 'FilePath\FileName6.csv'
$Result = if (Test-Path $file) { (Get-Item $file).length -gt 1kb }
if ($Result -eq "True") {"File6.csv Contains Data"} ELSE {"File6.csv is Empty!"}
Below is the email portion
$subject = "Emailtest"
$body = "test"
$emailTo = "jbloggs#Madeup.com"
$emailFrom ="JohnSmith#123.com"
$smtpServer = “mail.madeup.com”
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$credentials=new-object system.net.networkcredential(”username”,”password”)
$smtp.credentials=$credentials.getcredential($smtpserver,"25","basic")
$smtp.Send($emailFrom, $emailTo, $subject, $body)
Thank you for any help.
That's an awful lot of (manual) work just to check for empty files. What happens when you add a seventh - do you have to edit the script?
$EmptyFiles = (Get-childItem -Path $FilePath -Filter *.csv | `
where-object {$_.length -eq 0}|select-object -expandproperty Name)
$MsgBody = "The following files are empty:";
$EmptyFiles | foreach{$MsgBody+="`n$_";};
$MsgBody; # Just to output to console
$secpasswd = ConvertTo-SecureString "password" -AsPlainText -Force
$credentials= New-Object System.Management.Automation.PSCredential ("username", $secpasswd)
$subject = "Emailtest"
$body = "test"
$emailTo = "jbloggs#Madeup.com"
$emailFrom ="JohnSmith#123.com"
$smtpServer = “mail.madeup.com”
send-mailmessage -smtpserver $smtpServer -subject $subject -to $emailto -Credential $credentials -body $MsgBody
Related
I'm having some troubles to get all workitems/tasks and who is an assignee.
According to this answer is possible get the tasks using the report work, but is retrieving absolutely all.
https://xxx.visualstudio.com/{project}/_apis/wit/reporting/workitemrevisions?includeLatestOnly=true&api-version=5.0-preview.2
Is possible retrieve the id, title and who is an assignee?
You could add parameter fields=System.Id,System.Title,System.AssignedTo in the api:
GET https://{accountName}.visualstudio.com/{project}/_apis/wit/reporting/workitemrevisions?fields=System.Id,System.Title,System.AssignedTo&includeLatestOnly=true&api-version=5.0-preview.2
You can use below PowerShell script to call the REST API and retrieve the work item id, title and assignee and any other elements you needed.
Alternatively you can export the work item list to a *.csv file.
Param(
[string]$collectionurl = "https://xxx.visualstudio.com",
[string]$project = "ProjectName",
[string]$user = "username",
[string]$token = "Password/PAT"
)
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
$baseUrl = "$collectionurl/$project/_apis/wit/reporting/workitemrevisions?includeLatestOnly=true&api-version=5.0-preview.2"
$response = (Invoke-RestMethod -Uri $baseUrl -Method Get -UseDefaultCredential -Headers #{Authorization=("Basic {0}" -f $base64AuthInfo)}).values
$wits = $response | where({$_.fields.'System.WorkItemType' -eq 'Task'}) # Only retrieve Tasks
$witrevisions = #()
foreach($wit in $wits){
$customObject = new-object PSObject -property #{
"WitID" = $wit.fields.'System.Id'
"rev" = $wit.fields.'System.Rev'
"Title" = $wit.fields.'System.Title'
"AssignedTo" = $wit.fields.'System.AssignedTo'
"ChangedDate" = $wit.fields.'System.ChangedDate'
"ChangedBy" = $wit.fields.'System.ChangedBy'
"WorkItemType" = $wit.fields.'System.WorkItemType'
}
$witrevisions += $customObject
}
$witrevisions | Select-Object `
WitID,
rev,
Title,
AssignedTo,
ChangedDate,
ChangedBy,
WorkItemType #| export-csv -Path D:\temp\WIT.csv -NoTypeInformation
I have a script that works properly when run as an administrator but gives a Parse error when run as a normal user. Any Ideas?;
SCRIPT
`NeverExpires = 9223372036854775807;
$ExpireMin = (Get-Date).AddDays(4);
$ExpireMax = (Get-Date).AddDays(9);
$Userlist = Get-ADUser -Filter * -Properties name, samaccountname, accountexpirationdate, enabled, distinguishedname, accountExpires | Where-object {($_.DistinguishedName -notlike "*OU=Terminated,OU=Users,OU=Home Office,DC=Domain,DC=com")} |
Where-Object {$_.accountExpires -ne $NeverExpires `
-and [datetime]::FromFileTime([int64]::Parse($_.accountExpires)) -lt $ExpireMax `
-and [datetime]::FromFileTime([int64]::Parse($_.accountExpires)) -gt $ExpireMin }
$Userlist | select name, samaccountname, accountexpirationdate, enabled, distinguishedname | export-csv $ReportName -notypeinformation
Send-MailMessage -To $To -From $From -Subject $Subject -Body $Body -SMTPServer $SMTPServer -Attachments $ReportName
Get-ADUser -Filter * -Properties accountExpires |
Where-Object {$_.accountExpires -ne $NeverExpires `
-and [datetime]::FromFileTime([int64]::Parse($_.accountExpires)) -lt $ExpireMax `
-and [datetime]::FromFileTime([int64]::Parse($_.accountExpires)) -gt $ExpireMin } | ForEach {
$account = $_
$manager = Get-ADUser -Identity $account -Properties EmailAddress,Manager | %{(Get-AdUser $_.Manager -Properties EmailAddress).EmailAddress}`
I would say that $_.accountExpires is null either because the property could not be retrieved or $_ is itself null. Powershell will quietly convert null to the empty string resulting an invalid format for parsing. Note that the Parse call is completely unnecessary because powershell will automatically try to coerce the string for you and will likely give you a much better error message. Although null will be coerced to 0 as a long.
I've been able to parse a PDF by page multiple ways, the latest being this (not my code):
$reader = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList "oldy.pdf"
for ($page = 1; $page -le $reader.NumberOfPages; $page++)
{
$strategy = new-object 'iTextSharp.text.pdf.parser.SimpleTextExtractionStrategy'
$currentText = [iTextSharp.text.pdf.parser.PdfTextExtractor]::GetTextFromPage($reader, $page, $strategy);
[string[]]$Text += [system.text.Encoding]::UTF8.GetString([System.Text.ASCIIEncoding]::Convert( [system.text.encoding]::default, [system.text.encoding]::UTF8, [system.text.Encoding]::Default.GetBytes($currentText)));
}
I found a post here that suggested using LocationTextExtractionStrategy instead and splitting each line out by '\n'
However, I will admit that the .NET code here is confusing me and i'm not sure how to modify it to parse by string.
Can anyone help?
thanks.
Only a first experiment, but it works as expected:
# Download http://sourceforge.net/projects/itextsharp/
Add-Type -Path itextsharp.dll
$reader = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList MyFile.pdf
for ($page = 1; $page -le $reader.NumberOfPages; $page++)
{
# extract a page and split it into lines
$text = [iTextSharp.text.pdf.parser.PdfTextExtractor]::GetTextFromPage($reader,$page).Split([char]0x000A)
Write-Host "Page $($page) contains $($text.Length) lines. This is line 5:"
Write-Host $text[4]
#foreach ($line in $text)
#{
# any tasks
#}
}
$reader.Close()
I have a pdf document that I would like to extract content out of. The issue I am having is this... I search for the IMEI keyword, and it finds it, but I need the actual IMEI value which is the next item in the loop.
In the PDF the value looks like this:
IMEI 90289393092
returning value via the below script:
-0.1 -8.8 9.8 -0.1 446.7 403.9 Tm (IMEI:) Tj
I only want to have the value:
90289393092
Script I am using:
Add-Type -Path .\itextsharp.dll
$reader = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList "$pwd\PDF\DOC001.pdf"
for ($page = 1; $page -le $reader.NumberOfPages; $page++) {
$lines = [char[]]$reader.GetPageContent($page) -join "" -split "`n"
foreach ($line in $lines) {
if ($line -match "IMEI") {
$line = $line -replace "\\([\S])", $matches[1]
$line -replace "^\[\(|\)\]TJ$", "" -split "\)\-?\d+\.?\d*\(" -join ""
}
}
}
this is the way for using itextsharp.dll and read a pdf as plain text:
Add-Type -Path .\itextsharp.dll
$reader = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList c:\ps\a.pdf
for ($page = 1; $page -le $reader.NumberOfPages; $page++)
{
$strategy = new-object 'iTextSharp.text.pdf.parser.SimpleTextExtractionStrategy'
$currentText = [iTextSharp.text.pdf.parser.PdfTextExtractor]::GetTextFromPage($reader, $page, $strategy);
[string[]]$Text += [system.text.Encoding]::UTF8.GetString([System.Text.ASCIIEncoding]::Convert( [system.text.encoding]::default , [system.text.encoding]::UTF8, [system.text.Encoding]::Default.GetBytes($currentText)));
}
$Reader.Close();
And this can be the regex you need but I haven't tested it
[regex]::matches( $text, '(?<=IMEI\s+)(\d+)(?=\s+)' ) | select -expa value
I am trying to modify the PowerShell script I have found on ElegantCode.Com. I want to change it to specify a large text file of HTTP links, rather than naming the links as a parameter individually.
Once the script parses the file, I want it to pipe or echo out only the links that are valid back to a new file.
I am falling at the first hurdle and can't even figure out how I pass the input file in as a parameter.
Direct link for the script is here
BEGIN {
}
PROCESS {
$url = $_;
$urlIsValid = $false
try
{
$request = [System.Net.WebRequest]::Create($url)
$request.Method = 'HEAD'
$response = $request.GetResponse()
$httpStatus = $response.StatusCode
$urlIsValid = ($httpStatus -eq 'OK')
$tryError = $null
$response.Close()
}
catch [System.Exception] {
$httpStatus = $null
$tryError = $_.Exception
$urlIsValid = $false;
}
$x = new-object Object | `
add-member -membertype NoteProperty -name IsValid -Value $urlIsvalid -PassThru | `
add-member -membertype NoteProperty -name Url -Value $_ -PassThru | `
add-member -membertype NoteProperty -name HttpStatus -Value $httpStatus -PassThru | `
add-member -membertype NoteProperty -name Error -Value $tryError -PassThru
$x
}
}
END {
}
It appears the script it expecting the url to be piped in. The variable $_ represents the current pipeline object. So if the text file contained on URL per line you could do something like this:
Get-Content Urls.txt | Where {$_ -notmatch '^\s*$'} | Check-Url
I put the where in the pipe to eliminate blank lines.
To pipe the valid urls to a file as requested (adding to Keith's answer):
$validUrls = ".\ValidUrls.txt"
if (Test-Path $validUrls) { Remove-Item -Force $validUrls }
$result = New-Item -Type File -Path $validUrls
Get-Content Urls.txt | Where {$_ -notmatch '^\s*$'} | Foreach-Object {
$url = ($_ | Check-Url)
if ($url.IsValid)
{
$url.Url | Out-File $result -Append
}
}