Last active
January 15, 2018 18:22
-
-
Save Chirishman/7a9b0790b76e7d859503d11888d1e5b2 to your computer and use it in GitHub Desktop.
Cohen's request for PowerShell help
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Option A | |
#run this only the first time | |
Get-PSCredential | Export-Clixml -Path $env:USERPROFILE\TheseCreds.xml | |
Get-PSCredential | Export-Clixml -Path $env:USERPROFILE\FTPCreds.xml | |
#leave this in | |
$cred = Import-Clixml -Path $env:USERPROFILE\TheseCreds.xml | |
$FTPCreds = Import-Clixml -Path $env:USERPROFILE\FTPCreds.xml | |
#Option B | |
#leave this in | |
Import-Module StoredPSCredential | |
#run this only the first time | |
Initialize-StoredCredential -CredName ThisCred | |
Initialize-StoredCredential -CredName FTPCreds | |
#leave this in | |
$cred = Get-StoredCredential -CredName ThisCred | |
$FTPCreds = Get-StoredCredential -CredName FTPCreds | |
# FTP Server Variables | |
$FTPHost = 'ftp://192.168.1.1:9999/html/' | |
#Directory where to find pictures to upload | |
$UploadFolder = 'C:\Temp\' | |
$webclient = New-Object System.Net.WebClient | |
$webclient.Credentials = $ftpcreds.GetNetworkCredential() | |
$SrcEntries = Get-ChildItem -Path $UploadFolder -Include "*.ps1","*.xml" -Recurse | |
$Srcfolders = $SrcEntries | Where-Object{$_.PSIsContainer} | |
$SrcFiles = $SrcEntries | Where-Object{!$_.PSIsContainer} | |
# Create FTP Directory/SubDirectory If Needed - Start | |
foreach($folder in $Srcfolders) | |
{ | |
$SrcFolderPath = $UploadFolder -replace "\\","\\" -replace "\:","\:" | |
$DesFolder = $folder.Fullname -replace $SrcFolderPath,$FTPHost | |
$DesFolder = $DesFolder -replace "\\", "/" | |
# Write-Output $DesFolder | |
try | |
{ | |
$makeDirectory = [System.Net.WebRequest]::Create($DesFolder); | |
$makeDirectory.Credentials = New-Object System.Net.NetworkCredential($FTPUser,$FTPPass); | |
$makeDirectory.Method = [System.Net.WebRequestMethods+FTP]::MakeDirectory; | |
$makeDirectory.GetResponse(); | |
#folder created successfully | |
} | |
catch [Net.WebException] | |
{ | |
try { | |
#if there was an error returned, check if folder already existed on server | |
$checkDirectory = [System.Net.WebRequest]::Create($DesFolder); | |
$checkDirectory.Credentials = New-Object System.Net.NetworkCredential($FTPUser,$FTPPass); | |
$checkDirectory.Method = [System.Net.WebRequestMethods+FTP]::PrintWorkingDirectory; | |
$response = $checkDirectory.GetResponse(); | |
#folder already exists! | |
} | |
catch [Net.WebException] { | |
#if the folder didn't exist | |
} | |
} | |
} | |
# Create FTP Directory/SubDirectory If Needed - Stop | |
# Upload Files - Start | |
foreach($entry in $SrcFiles) | |
{ | |
$SrcFullname = $entry.fullname | |
$SrcName = $entry.Name | |
$SrcFilePath = $UploadFolder -replace "\\","\\" -replace "\:","\:" | |
$DesFile = $SrcFullname -replace $SrcFilePath,$FTPHost | |
$DesFile = $DesFile -replace "\\", "/" | |
# Write-Output $DesFile | |
$uri = New-Object System.Uri($DesFile) | |
$webclient.UploadFile($uri, $SrcFullname) | |
} | |
# Upload Files - Stop | |
$Email = @{ | |
To = @('Chris.one@happyone.com','dwight.two@happyone.com','chilly.@happyone.com') | |
From = 'Outbound@happyone.com' | |
SmtpServer = 'Smtp.office365.com' | |
Subject = "Great news. You have new .one and .two files from happy one land" | |
BodyAsHtml = $true | |
Body = "$($data | ConvertTo-Html)" | |
UseSSL = $true | |
Credential = $cred | |
} | |
Send-MailMessage @Email |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment