Skip to content

Instantly share code, notes, and snippets.

@relyky
Last active August 25, 2023 04:42
Show Gist options
  • Save relyky/c0df82336aaa2e9377bee2ff88e76320 to your computer and use it in GitHub Desktop.
Save relyky/c0df82336aaa2e9377bee2ff88e76320 to your computer and use it in GitHub Desktop.
PowerShell, 常用指令, sc.exe, smtp
### 用 PowerShell 建立新的 Eventlog
# ※需有管理員權限才能建立 EventLog。
New-EventLog -source MyEventSource -LogName MyApplication
### How to check if remote ports are open using PowerShell?
指令: Test-NetConnection -ComputerName <host> -Port <port>
說明: 測試 Port 有否開啟
範例:
PS C:\temp> Test-NetConnection -ComputerName localhost -Port 443
ComputerName : localhost
RemoteAddress : ::1
RemotePort : 7027
InterfaceAlias : Loopback Pseudo-Interface 1
SourceAddress : ::1
TcpTestSucceeded : True
// 建立服務,
// ※注意:需以系統管理員身份執行。
// ※注意:參數間的'空白字元'。
sc.exe create <ServiceName> DisplayName= <顯示名稱> binpath= <EXE完整路徑>
sc.exe create YourJobWorker DisplayName= 你的排程服務 binpath= D:\TheTargetFolder\YourJobWorker.exe
// 啟動服務
sc.exe start <ServiceName>
sc.exe start YourJobWorker
// 停止服務
sc.exe stop <ServiceName>
sc.exe stop YourJobWorker
// 刪除服務:服務若在執行中不能刪除。請先停止服務才能刪除。
sc.exe delete <ServiceName>
sc.exe delete YourJobWorker
### 用 PowerShell 寄出 SMTP 電子郵件。
# 須先用 Get-Credential 指令取得帳密 credential ,因為不能直接輸入帳號密碼!
$smtpCredential = Get-Credential
# 才能用 Send-MailMessage 寄信
Send-MailMessage -SmtpServer "your.smtp.server.com" -Port 587 `
-Credential $smtpCredential `
-From "[email protected]" `
-To "[email protected]" `
-Subject "Test Email" `
-Body "This is a test email sent via PowerShell" `
-UseSsl
# relay mode: 不需帳密也不需SSL
Send-MailMessage -SmtpServer "your.smtp.server.com" -Port 25 `
-From "[email protected]" `
-To "[email protected]" `
-Subject "Test Email" `
-Body "This is a test email sent via PowerShell" `
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment