Basics of powershell

Basics of windows powershell

Search for: Basics of windows powershell

Where is Powershell cmdlet online help?

Search for: Where is Powershell cmdlet online help?

Core cmdlets are documented here


powershell.exe
exit

How many categories are there in powershell?

Search for: How many categories are there in powershell?

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


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

#***************************************
#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

Some examples of powershell from an SOF user: mattb


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

$From = "Me <[email protected]>"
$To = [email protected]
$Title = "Subject"
$Body = "Body Text" 
$SmtpClient.Send($From,$To,$Title,$Body)

param(  
        [string] $From = "[email protected]",
        [string] $To = "[email protected]",
        [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)

powershell.exe 
   c:\path\to\sendmail.ps1 
   "[email protected]" 
   "[email protected]" 
   "title" 
   "body"

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

How do i invoke a psl file through powershell

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


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.

How do I set powershell execution policy?

Search for: How do I set powershell execution policy?

Here is some background and examples from ms

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

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

Here are some examples

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

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.


powershell -executionpolicy remotesigned -file sendmail.ps1

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


$emailSmtpServer = "mail.somewhere.com"
$emailSmtpServerPort = "587"
$emailSmtpUser = "username"
$emailSmtpPass = "password"
 
$emailMessage = New-Object System.Net.Mail.MailMessage
$emailMessage.From = "John Smith <[email protected]>"
$emailMessage.To.Add( "[email protected]" )
$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 )

The above taken from the following URL

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

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

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


@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