【发布时间】:2015-04-16 07:08:44
【问题描述】:
我有一些 PS 脚本执行如下:
Script1:仅执行一次,并将输入的密码作为安全字符串存储在文本文件中
Script2:作为计划任务重复执行。这会调用另一个脚本 - Script3。
Script3:每次,这个脚本都能很好地完成它的主要任务。但它有一个邮件发送功能,每次都会失败。脚本如下图:
$time = (Get-Date).ToShortTimeString()
$destzone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Central Standard Time")
$desttime = [System.TimeZoneInfo]::ConvertTimeFromUtc((Get-Date).ToUniversalTime(), $destzone)
$desttime = $desttime.ToShortTimeString()
if($time -eq $desttime)
{
#do nothing
}
else
{
***Main Function of script***
#E-mail Function
#Getting Credential from txt file
$passw = Get-Content C:\timezone\mailcred.txt | convertto-securestring
$crede = new-object -typename System.Management.Automation.PSCredential -argumentlist "user@domain.in",($passw)
#Mail Function
$param = @{
SmtpServer = 'smtp.gmail.com'
Port = 587
UseSsl = $true
Credential = $crede
From = 'user@domain.in'
To = 'user@domain.in'
Subject = 'Alert - Timezone changed'
Body = "Time zone changed to Central Standard Time for system with IP $ip"
}
Send-MailMessage @param
}
我收到的错误是:
Exception calling ".ctor" with "2" argument(s): "Cannot process argument because the value of argument "password" is null. Change the value of argument "password" to a non-null value."
Cannot validate argument on parameter 'Credential'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
单独执行相同的邮件功能脚本时,它工作正常。但是与其他脚本块一起执行是行不通的。
【问题讨论】:
-
您能否在任务调度程序(microsoft -> windows -> powershell)中查找计划任务并查找运行该任务的凭据?尝试在 taskrunner 中添加凭据(默认运行,没有特殊权限)。添加作业时添加凭据是一些更多的 powershell 工作,首先测试它是否有帮助。
-
@WalterA:我手动运行它。但那个时候也行不通。
-
以管理员身份使用 powershell ise 查找出现问题的行。
$passw = Get-Content C:\timezone\mailcred.txt | convertto-securestring有效吗?$passw = ConvertTo-SecureString -string "myPassword" -asplaintext -force有效吗? -
没有任何作用。我刚刚显示了 $passw 的值,它显示了 m System.SecureString 。纯文本不起作用,因为我以加密形式而不是纯文本存储它。
-
您是否使用与运行 Script3 相同的凭据来运行 Script1?
标签: email powershell