Basics of powershell

satya - 8/9/2016, 10:52:04 AM

Basics of windows powershell

Basics of windows powershell

Search for: Basics of windows powershell

satya - 8/9/2016, 11:08:36 AM

Where is Powershell cmdlet online help?

Where is Powershell cmdlet online help?

Search for: Where is Powershell cmdlet online help?

satya - 8/9/2016, 11:10:33 AM

Core cmdlets are documented here

Core cmdlets are documented here

satya - 8/9/2016, 11:10:56 AM

How to enter and exit


powershell.exe
exit

satya - 8/9/2016, 11:25:04 AM

How many categories are there in powershell?

How many categories are there in powershell?

Search for: How many categories are there in powershell?

satya - 8/9/2016, 11:29:33 AM

This guy seem to show some of the categories (These may be dynamic, I wonder)

This guy seem to show some of the categories (These may be dynamic, I wonder)

satya - 8/9/2016, 11:30:33 AM

Some of the categories


Alias
Cmdlet
Provider
General
FAQ
Glossary
HelpFile
ScriptCommand
Function
Filter
ExternalScript
AllDefaultHelp
Workflow

satya - 8/9/2016, 11:33:38 AM

You can do 2 things with get-help cmdlet


#***************************************
#Get help on a named cmdlet called new-object
#***************************************
get-help -name new-object

#***************************************
#List all cmdlets in the system
#***************************************
get-help -category cmdlet

#Note..there are way too many cmdlets
#you should use the online help links above for reference

satya - 8/9/2016, 3:15:58 PM

Some examples of powershell from an SOF user: mattb

Some examples of powershell from an SOF user: mattb

satya - 8/9/2016, 3:16:31 PM

Example 1


$SmtpClient = New-Object System.Net.Mail.SmtpClient
$SmtpServer = "your.mail.host.com"
$SmtpClient.host = $SmtpServer 

$From = "Me <User@example.com>"
$To = User2@example.com
$Title = "Subject"
$Body = "Body Text" 
$SmtpClient.Send($From,$To,$Title,$Body)

satya - 8/9/2016, 3:17:11 PM

Example 2


param(  
        [string] $From = "from@example.com",
        [string] $To = "to@example.com",
        [string] $Title = "title",
        [string] $Body = "body"
    )
    $SmtpClient = New-Object System.Net.Mail.SmtpClient
    $SmtpServer = "your.mail.host.com"
    $SmtpClient.host = $SmtpServer 
    $SmtpClient.Send($From,$To,$Title,$Body)

satya - 8/9/2016, 3:18:44 PM

With example 2 you can do


powershell.exe 
   c:\path\to\sendmail.ps1 
   "from@example.com" 
   "to@example.com" 
   "title" 
   "body"

Of course you will need to put them on the same line or using some kind of a sentence terminator

satya - 8/9/2016, 3:32:05 PM

How do i invoke a psl file through powershell

How do i invoke a psl file through powershell

Search for: How do i invoke a psl file through powershell

satya - 8/9/2016, 3:33:05 PM

Apparently only the following seem to work


powershell -file sendmail.ps1

the -file has to be there. Also the extension must be .ps1 (ONE not L)

Then looks like you have to allow execution permissions.

satya - 8/9/2016, 3:36:59 PM

How do I set powershell execution policy?

How do I set powershell execution policy?

Search for: How do I set powershell execution policy?

satya - 8/9/2016, 3:43:18 PM

Here is some background and examples from ms

Here is some background and examples from ms

satya - 8/9/2016, 3:47:36 PM

How do I run a powershell command from a .cmd file?

How do I run a powershell command from a .cmd file?

Search for: How do I run a powershell command from a .cmd file?

satya - 8/9/2016, 3:50:09 PM

Here are some examples

Here are some examples

satya - 8/9/2016, 3:52:05 PM

How can i temporarily change execution policy in powershell for one file

How can i temporarily change execution policy in powershell for one file

Search for: How can i temporarily change execution policy in powershell for one file

satya - 8/9/2016, 3:54:16 PM

Here is an answer

Here is an answer

Once only...

To run a single PowerShell session with a different execution policy, use powershell.exe -ExecutionPolicy this will not affect the default policy setting for any future sessions.

satya - 8/9/2016, 3:56:07 PM

Here is an example


powershell -executionpolicy remotesigned -file sendmail.ps1

This will only temporarily effect the policy for that batch file.

satya - 8/9/2016, 3:59:16 PM

To understand TLS needs


$emailSmtpServer = "mail.somewhere.com"
$emailSmtpServerPort = "587"
$emailSmtpUser = "username"
$emailSmtpPass = "password"
 
$emailMessage = New-Object System.Net.Mail.MailMessage
$emailMessage.From = "John Smith <john@somewhere.com>"
$emailMessage.To.Add( "jane@somewhere.com" )
$emailMessage.Subject = "Testing e-mail"
$emailMessage.IsBodyHtml = $true
$emailMessage.Body = @"
<p>Here is a message that is <strong>HTML formatted</strong>.</p>
<p>From your friendly neighborhood IT guy</p>
"@
 
$SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort )
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass );
 
$SMTPClient.Send( $emailMessage )

satya - 8/9/2016, 3:59:42 PM

The above taken from the following URL

The above taken from the following URL

satya - 8/9/2016, 4:22:45 PM

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required

Search for: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required

satya - 8/9/2016, 4:27:43 PM

Read this to understand to turn off less secure sign on in your google account

Read this to understand to turn off less secure sign on in your google account

satya - 8/9/2016, 4:28:08 PM

So clearly you should use a dedicated google account to do this!!!

So clearly you should use a dedicated google account to do this

satya - 8/10/2016, 9:23:06 AM

Example of putting a powershell in a command file


@rem ************************************************
@rem * This runs on server on a local drive
@rem * C:\some-server-dir
@rem * Make sure the local directory is set 
@rem * Files that must be coresident
@rem *     a) this command file
@rem *     b) sendmail.ps1
@rem * if running from a task scheduler set the starting directory
@rem ************************************************
@rem set cur_wking_dir=C:\some-server-dir

powershell -executionpolicy remotesigned -file sendmail.ps1