传递给PowerShell中的函数时,Hashtable为空

时间:2015-05-09 00:44:57

标签: email powershell

我正在尝试设置一个powershell脚本,该脚本在修改服务器之前和之后收集有关服务器的C驱动器的数据。从那里通过电子邮件将该信息发送给我。这是我的第一个powershell脚本,它超出了我能想到的范围。它一直工作到电子邮件的位置。我仍然收到一封电子邮件但没有数据。我认为它与传递哈希表$BeforeScript$AfterScript有关。下面是代码 - 我已经使用的函数。

# Retrieve current disk size, free space, and percent free.
Function Disk_Info{
    $DiskDrive = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'" | Select-Object Size,FreeSpace

    [hashtable]$Return = @{}

    $Return.DiskSize = ([math]::truncate($DiskDrive.size / 10000000) / 100)
    if ($DiskDrive.size -gt 0) {
        $Return.DiskFreeSpace = ([math]::truncate($DiskDrive.freespace / 10000000) / 100)
        $Return.DiskPercentFree = [Math]::round(($Return.DiskFreeSpace/$Return.DiskSize) * 100)
    }
    else {
        $Return.DiskFreeSpace = 0
        $Return.DiskPercentFree = 0
    }

    Return $Return
}

# Send an message to the NOC Oncall email about the server having > 5% free space
Function Send_Email ($BS, $AS){
    $Message = @{
        From =       "DriveSpaceAlert@MyPlace.com"
        To =         "Keith@MyPlace.com"
        Subject =    "$($Server," C: is at ",$AS.DiskPercentFree,"% Free Space.")"
        Body =       "$("The Clean C: Drive Script was unable to raise ",$Server," C: drive above 5%.<br><br>"
                     "C: Size: ", $BS.DiskSize, "GBs<br>"
                     "Before Script: ", $BS.DiskFreeSpace, "GB (",$BS.DiskPercentFree,"%) free space.<br>"
                     "After Script: ", $AS.DiskFreeSpace, "GB (",$AS.DiskPercentFree,"%) free space.<br><br>"
                     "If this is during work hours, contact this severs owner: ",$Owner, ".<br>"
                     "If this is after hours please contact the ST6 Oncall about this issue.<br><br>"
                     "<a href = http://teams/sites/ta/NOC/TriForce%20Wiki/Low%20Disk%20Space%20Issues.aspx>Wiki C: Free Space details</a>.<br>"
                     "<a href = http://teams/sites/ta/NOC/TriForce%20Wiki/Server%20Specific%20Actions.aspx>Wiki Server Specific details</a>.<br>"
                     )"
        SMTPServer = "Something.something.com"
    }

#    How to add extra items to message
#    If($cc){$Message.Add("CC",$cc)}
#    If($Attachment){$Message.Add("Attachment",$Attachment)}

    #Try to get the server owner from the registry
    try{
        $Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$env:COMPUTERNAME)
        $RegKey= $Reg.OpenSubKey("SOFTWARE\\QL")
        $Owner = $RegKey.GetValue("Managed by")
    }
    catch{
        $Owner = "Server owner not specified"
    }

    $Server = $env:COMPUTERNAME
    Send-MailMessage @Message -BodyAsHtml -Priority High
}

$BeforeScript = Disk_Info    #Get disk info pre script
#Sweep_C_Drive                #Delete unnecessary files from C Drive
$AfterScript = Disk_Info     #Get disk info post script

If ($AfterScript.DiskPercentFree -lt 6){
    Send_Email ( $BeforeScript, $AfterScript)
}

这是我在脚本运行后收到的电子邮件:

The Clean C: Drive Script was unable to raise C: drive above 5%.

C: Size: System.Object[] GBs
Before Script: System.Object[] GB ( System.Object[] %) free space.
After Script: GB ( %) free space.

If this is during work hours, contact this severs owner: .
If this is after hours please contact the ST6 Oncall about this issue.

Wiki C: Free Space details.
Wiki Server Specific details.

电子邮件应在清除C驱动器之前和之后说明可用空间的大小(可用空间的百分比)。但事实并非如此。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

丢失括号(和逗号) - 这不是您调用PowerShell函数的方式。即不要打电话

Send_Email $BeforeScript $AfterScript

而是

@Lob
private byte[] picture;