编辑:问题已经回答,看我的底部我的最后脚本…
男人,我是Powerhell noob …
我有这个链接:
http://gallery.technet.microsoft.com/ScriptCenter/en-us/da3fee00-e79d-482b-91f2-7c729c38068f
我想用它来获取服务器列表,在列表中运行它,然后获取每个服务器的磁盘空间类似于的报告:
SERVER1
C:总计= 120GB可用空间= 60GB D:总计= 400GB可用空间= 200GB
等等
问题是…
我不知道如何处理该链接上的脚本来使其工作。 复制/粘贴到记事本并保存为.ps1 ?? 在描述中似乎不是那样的。
如果我从我的Win7中使用PS运行它,还是每台服务器都必须安装Powershell以使远程PS脚本能够正常工作,它会起作用吗?
有没有办法设置脚本来给我发电子邮件? 这样我可以把它设置为每周的任务或类似的。
谢谢!
========
最终脚本
function Get-FreeDisk { [CmdletBinding()] param( [Parameter(Position=0, ValueFromPipeline=$true)] [string[]]$Computername="localhost", [int] [ValidateRange(0,100)] $MinPercFree = 100, [Management.Automation.PSCredential]$Credential = ([Management.Automation.PSCredential]::Empty) ) begin{ [String[]]$computers = @() } process{ $computers += $Computername } end{ Get-WmiObject -computername $computers -Credential $Credential ` -Query "select __SERVER, Caption,Label,Capacity,FreeSpace from Win32_Volume where DriveType != 5 and Capacity > 0" | ` Add-Member -Name Free -MemberType ScriptProperty -PassThru -Value {($this.FreeSpace/10000) / ($this.Capacity/1000000)} | ` Where { $_.Free -lt $MinPercFree } | ` sort __SERVER,Caption | ` Format-Table @{Label="Computer"; Expression={$_.__SERVER}}, Caption, Label,` @{Label="Size/MB"; FormatString="{0,7:N0}"; Expression={$_.Capacity / 1mb}},` @{Label="FreeSpace/MB"; FormatString="{0,7:N0}"; Expression={$_.Freespace / 1mb}}, ` @{Label="Free"; FormatString="{0,3:N0}%"; Expression={$_.Free}} -AutoSize } } Get-Content .\servers.txt | Get-FreeDisk | Format-Table -AutoSize | Out-File diskusage.txt Send-MailMessage -To [email protected] -Subject "Server Disk Report" -From [email protected] -SmtpServer mail.domain.com -Attachments "diskusage.txt"
你的观点:
这是一个函数,所以你需要将它加载到你的环境中。 你可以通过几种方法来做到这一点。
将其添加到您的个人资料:
notepad $profile ,这将启动与您的configuration文件脚本在记事本中。 configuration文件脚本是每次启动PS会话时执行的东西,并允许您自动执行设置的环境variables,别名,会话函数(如这个)等。请注意,该文件默认情况下不存在,所以您如果你还没有这样做,可能需要创build它。 path为%USERPROFILE%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 (使用My Documents而不是Documents for XP) function开始到最后} 。 如果你想要的话,你可以包含new-alias ,但这不是必须的。 这个new-alias命令将使你可以键入df而不必input整个Get-Freedisk命令 . $profile . $profile使更改生效。 例如:
function Get-FreeDisk { [CmdletBinding()] param( [Parameter(Position=0, ValueFromPipeline=$true)] [string[]]$Computername="localhost", [int] [ValidateRange(0,100)] $MinPercFree = 100, [Management.Automation.PSCredential]$Credential = ([Management.Automation.PSCredential]::Empty) ) begin{ [String[]]$computers = @() } process{ $computers += $Computername } end{ Get-WmiObject -computername $computers -Credential $Credential ` -Query "select __SERVER, Caption,Label,Capacity,FreeSpace from Win32_Volume where DriveType != 5 and Capacity > 0" | ` Add-Member -Name Free -MemberType ScriptProperty -PassThru -Value {($this.FreeSpace/10000) / ($this.Capacity/1000000)} | ` Where { $_.Free -lt $MinPercFree } | ` sort __SERVER,Caption | ` Format-Table @{Label="Computer"; Expression={$_.__SERVER}}, Caption, Label,` @{Label="Size/MB"; FormatString="{0,7:N0}"; Expression={$_.Capacity / 1mb}},` @{Label="FreeSpace/MB"; FormatString="{0,7:N0}"; Expression={$_.Freespace / 1mb}}, ` @{Label="Free"; FormatString="{0,3:N0}%"; Expression={$_.Free}} -AutoSize } } $emailFrom = "[email protected]" $emailTo = "[email protected]" $subject = "Free Disk Space Report" $body = Get-Content .\servers.txt | Get-FreeDisk | Out-String $smtpServer = "mail.example.com" $smtp = new-object Net.Mail.SmtpClient($smtpServer) $smtp.Send($emailFrom, $emailTo, $subject, $body)
把它放在一个名为email_freedisk.ps1的文件里; 然后设置一个计划任务来运行它。
您只需要一个包含服务器名称列表的文件,每行一个,名称为servers.txt。