Powershell确认文件夹存在并发送电子邮件(如果它不是

时间:2017-06-26 22:07:09

标签: python powershell

我正在尝试搜索SQL数据库以确认目录是否存在。如果目录不存在,脚本应发送电子邮件以进行通知。我试图创造一些东西,但我不熟悉PowerShell。

我能够从SQL服务器获取所有数据。我遇到了$($ Row。[Last Name])的错误。它声明它无法找到[Last Name]类型,但它找到了Account和IsActive就好了。

无法找到类型[姓氏]:确保已加载包含此类型的程序集。 在\ cottonwood \ users \ CB \ My Documents \ SQLserver-search.ps1:44 char:17 + $ Row。[姓氏]<<<<     + CategoryInfo:InvalidOperation :( Last Name:String)[],RuntimeException     + FullyQualifiedErrorId:TypeNotFound

我不确定我的问题是否清楚。我是Stack Overflow的新手。任何帮助将不胜感激。提前谢谢。

Param (
    $Path = "\\cottonwood\users\Shared\Pool Acquisitions",
    $SMTPServer = "generic-mailserver",
    $From = "generic-outbound-email",
   #The below commented out line is used to test with just one individual. Be sure to comment out the one with all individuals before troubleshooting.
    #$To = @("generic-email"),
    $port = "587",
    $To = @("generic-inbound-email"),
    $Subject = "Folders Added in",
    $logname = "\\cottonwood\users\Shared\Loan Documents - Active\logs\New Folders$date.txt",
    $date = (Get-Date -Format MMddyyyy),
    $SMTPBody = "`nThe following Pool Acquisitions folders have been added in the last 24 hours:`n`n"
)


$SQLServer = "REDWOOD" #use Server\Instance for named SQL instances! 
$SQLDBName = "MARS"
$SqlQuery = "select Account, IsActive, [Last Name] FROM vw_loans WHERE LEFT(Account,1)<>'_' ORDER BY Account"

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True"

$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection

$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd

$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)


$SqlConnection.Close()

clear

$DataSet.Tables[0]

foreach ($Row in $dataset.Tables[0].Rows) { 
    write-Output "$($Row)"
    write-Output "$($Row.Account)"
    write-Output "$($Row.IsActive)"
    write-Output "$($Row.[Last Name])"
}

if($Row.IsActive -eq $True){
$ChkPath = "U:\Shared\Loan Documents - Active\$Row.Account - $Row.[Last Name]"
}
else{
$ChkPath = "U:\Shared\Loan Documents - Inactive\$Row.Account - $Row.[Last Name]"
}

$FileExist = Test-Path $ChkPath

$SMTPMessage = @{
    To = $To
    From = $From
    Subject = "$Subject $Path"
    Smtpserver = $SMTPServer
    Port = $port
}

If($FileExist -eq $True) {Write-Host "Null Response"}
else
{    $SMTPBody = "Loan folder is missing. Please advise."
    $LastWrite | ForEach { $SMTPBody += "$($_.FullName)`n"  }
   UseSSL = $true
}

1 个答案:

答案 0 :(得分:0)

if($Activity = 'y')应为if($Activity -eq 'y')

在PowerShell =中始终设置一个值,因此该语句始终为true。

我也非常确定param块必须位于脚本的顶部。