Power Shell 脚本 SMTP 电子邮件问题
Power Shell Script SMTP Email Issues
1) 如何在此脚本中存储我的凭据 - 它提示我输入凭据
2) 最后两行应该在参数的底部还是在参数之前?我收到此错误 - 我想我没有正确传递我的凭据
Send-MailMessage : The remote name could not be resolved: 'System.Net.Mail.SmtpClient'
At line:21 char:1
+ Send-MailMessage @smtpMessage
Blockquote
param (
[string]$Path = "C:\Users\me\Desktop\ScanFolder",
$From = "me@msn.com",
$To = "you@msn.com",
$Subject1 = "Changes Found",
$Subject2 = "No Changes in last x minutes",
$Body = "anything",
$SMTPServer = "smtp.live.com",
$SMTPPort = "587",
[PSCredential]$Credential #pass in credential
)
$smtpMessage = @{
To = $To
From = $From
Subject = $Subject
Smtpserver = $SMTPClient
Credential = $Credential
BodyAsHtml = $true
}
Send-MailMessage @smtpMessage
$secpasswd = ConvertTo-SecureString "mypassword" -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential ("me@msn.com", $secpasswd)
无论是电子邮件还是其他方式,您都可以在脚本中以纯文本形式输入您的信用(就像您现在所做的那样)、 但确实是一种不明智的做法 或将它们存储在安全文件中(更好的选择)并在需要时调用它们。
整个网络和这个论坛上都有大量关于这种思维过程的例子。例如:
Quickly and securely storing your credentials – PowerShell
https://www.jaapbrasser.com/quickly-and-securely-storing-your-credentials-powershell
# To get a credential object we can either manually create one or use the Get-Credential cmdlet to prompt for the account details:
$Credential = Get-Credential
# To store the credentials into a .cred file:
$Credential | Export-CliXml -Path "${env:\userprofile}\Jaap.Cred"
# And to load the credentials from the file and back into a variable:
$Credential = Import-CliXml -Path "${env:\userprofile}\Jaap.Cred"
Invoke-Command -Computername 'Server01' -Credential $Credential {whoami}
Storing PowerShell Credentials in JSON
https://jdhitsolutions.com/blog/powershell/5396/storing-powershell-credentials-in-json
$secure = ConvertTo-SecureString -String 'P@$$w0rd' -AsPlainText -Force
$cred = New-Object -typename PSCredential -ArgumentList @('company\admin',$secure)
$cred | Export-clixml c:\work\admin.xml
Securely Store Credentials on Disk
http://powershellcookbook.com/recipe/PukO/securely-store-credentials-on-disk
# The first step for storing a password on disk is usually a manual one. There is nothing mandatory about the filename, but we’ll use a convention to name the file CurrentScript.ps1.credential. Given a credential that you’ve stored in the $credential variable, you can safely use the Export-CliXml cmdlet to save the credential to disk. Replace CurrentScript with the name of the script that will be loading it:
PS > $credPath = Join-Path (Split-Path $profile)
# CurrentScript.ps1.credential
PS > $credential | Export-CliXml $credPath
# In PowerShell version 2, you must use the ConvertFrom-SecureString cmdlet:
PS > $credPath = Join-Path (Split-Path $profile) CurrentScript.ps1.credential
PS > $credential.Password | ConvertFrom-SecureString | Set-Content $credPath
这是一个常见问题,因此您的 post 请求可以被视为与以下请求重复:
Using PowerShell credentials without being prompted for a password
How to pass credentials to the Send-MailMessage command for sending emails
1) 如何在此脚本中存储我的凭据 - 它提示我输入凭据
2) 最后两行应该在参数的底部还是在参数之前?我收到此错误 - 我想我没有正确传递我的凭据
Send-MailMessage : The remote name could not be resolved: 'System.Net.Mail.SmtpClient' At line:21 char:1 + Send-MailMessage @smtpMessage Blockquote
param (
[string]$Path = "C:\Users\me\Desktop\ScanFolder",
$From = "me@msn.com",
$To = "you@msn.com",
$Subject1 = "Changes Found",
$Subject2 = "No Changes in last x minutes",
$Body = "anything",
$SMTPServer = "smtp.live.com",
$SMTPPort = "587",
[PSCredential]$Credential #pass in credential
)
$smtpMessage = @{
To = $To
From = $From
Subject = $Subject
Smtpserver = $SMTPClient
Credential = $Credential
BodyAsHtml = $true
}
Send-MailMessage @smtpMessage
$secpasswd = ConvertTo-SecureString "mypassword" -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential ("me@msn.com", $secpasswd)
无论是电子邮件还是其他方式,您都可以在脚本中以纯文本形式输入您的信用(就像您现在所做的那样)、 但确实是一种不明智的做法 或将它们存储在安全文件中(更好的选择)并在需要时调用它们。
整个网络和这个论坛上都有大量关于这种思维过程的例子。例如:
Quickly and securely storing your credentials – PowerShell
https://www.jaapbrasser.com/quickly-and-securely-storing-your-credentials-powershell
# To get a credential object we can either manually create one or use the Get-Credential cmdlet to prompt for the account details:
$Credential = Get-Credential
# To store the credentials into a .cred file:
$Credential | Export-CliXml -Path "${env:\userprofile}\Jaap.Cred"
# And to load the credentials from the file and back into a variable:
$Credential = Import-CliXml -Path "${env:\userprofile}\Jaap.Cred"
Invoke-Command -Computername 'Server01' -Credential $Credential {whoami}
Storing PowerShell Credentials in JSON
https://jdhitsolutions.com/blog/powershell/5396/storing-powershell-credentials-in-json
$secure = ConvertTo-SecureString -String 'P@$$w0rd' -AsPlainText -Force
$cred = New-Object -typename PSCredential -ArgumentList @('company\admin',$secure)
$cred | Export-clixml c:\work\admin.xml
Securely Store Credentials on Disk
http://powershellcookbook.com/recipe/PukO/securely-store-credentials-on-disk
# The first step for storing a password on disk is usually a manual one. There is nothing mandatory about the filename, but we’ll use a convention to name the file CurrentScript.ps1.credential. Given a credential that you’ve stored in the $credential variable, you can safely use the Export-CliXml cmdlet to save the credential to disk. Replace CurrentScript with the name of the script that will be loading it:
PS > $credPath = Join-Path (Split-Path $profile)
# CurrentScript.ps1.credential
PS > $credential | Export-CliXml $credPath
# In PowerShell version 2, you must use the ConvertFrom-SecureString cmdlet:
PS > $credPath = Join-Path (Split-Path $profile) CurrentScript.ps1.credential
PS > $credential.Password | ConvertFrom-SecureString | Set-Content $credPath
这是一个常见问题,因此您的 post 请求可以被视为与以下请求重复:
Using PowerShell credentials without being prompted for a password
How to pass credentials to the Send-MailMessage command for sending emails