First script - PowerShell
We create the first script in powershell
Create a new file named first script.ps1
Write-Host "Hello, World!" |
Save the file and open PowerShell window.
There are 4 settings of policies:
-
Restricted – Scripts won’t run. Period. (Default setting)
-
RemoteSigned – Locally-created scripts will run. Scripts that were created on another machine will not run unless they are signed by a trusted publisher.
-
AllSigned – Scripts will only run if signed by a trusted publisher (including locally-created scripts).
-
Unrestricted – All scripts will run regardless of who created them and whether or not they are signed.
We change policies RemoteSigned
Set-ExecutionPolicy RemoteSigned
correct syntax to run the script
& "c:\folder\script.ps1
c:\folder\script.ps1
incorrect syntax to run the script
PS C:\folder> script.ps1
writing scripts Powershell
writing scripts Powershell
Powershell scripts written as a text file with the extension .ps1. Run the script from the PowerShell console:
Run PS script:
write full path PS C:\windows\script\test_script.ps1
send e-mail just a script PowerShell Send-MailMessage
send e-mail just a script PowerShell
Send-MailMessage
help send-MailMessage:
NAME
Send-MailMessage
SYNTAX
Send-MailMessage [-To] <string[]> [-Subject] <string> [[-Body] <string>] [[
-SmtpServer] <string>] -From <string> [-Attachments <string[]>] [-Bcc <stri
ng[]>] [-BodyAsHtml] [-Encoding <Encoding>] [-Cc <string[]>] [-DeliveryNoti
ficationOption <DeliveryNotificationOptions> {None | OnSuccess | OnFailure
| Delay | Never}] [-Priority <MailPriority> {Normal | Low | High}] [-Creden
tial <pscredential>] [-UseSsl] [-Port <int>] [<CommonParameters>]
ALIASES
None
REMARKS
Get-Help cannot find the Help files for this cmdlet on this computer. It is
displaying only partial help.
-- To download and install Help files for the module that includes this
cmdlet, use Update-Help.
-- To view the Help topic for this cmdlet online, type: "Get-Help Send-
MailMessage -Online" or
go to http://go.microsoft.com/fwlink/?LinkID=135256.
Send mail:
type powershell send-mailmessage
cmdlet Send-MailMessage at command pipeline position 1
Supply values for theh following parameteres:
From:
Subject:
To[0]:
sample script:
$sender = “Ten adres pocztowy jest chroniony przed spamowaniem. Aby go zobaczyć, konieczne jest włączenie w przeglądarce obsługi JavaScript.” $receiver = “Ten adres pocztowy jest chroniony przed spamowaniem. Aby go zobaczyć, konieczne jest włączenie w przeglądarce obsługi JavaScript.” $subject = “test email from powershell” $content = “content email from powershell.” $server_smtp = “smtp.domena.pl” Send -MailMessage -SmtpServer $server_smtp -From $sender -To $receiver -Subject $subject -Body $content |
a list of programs that run from the "Startup" - powershell
a list of programs that run from the "Startup" - powershell
type the following command:
Get-CimInstance -ClassName Win32_StartupCommand | Select-Object -Property Name, Command | format-table