如何解决脚本运行时PowerShell更新变量
在脚本运行状态下如何在PowerShell中更新变量?
我遇到一种情况,脚本会连续监视磁盘的大小,并将其与共享驱动器上的文本文件中的数字进行比较(例如Z:\ quota \ software-share-size.txt)。如果文本文件中的数字大于它监视的磁盘大小,则它将发送电子邮件以将磁盘扩展到文本文件中提到的新大小。但是一旦脚本启动,它就不会从文件中提取新的数字,我也不想停止并启动脚本来从文本文件中加载新内容。请帮助
解决方法
也许这可以帮助您:
while($true)
{
#Here i pull my disk (C:) infomations (i use localhost for my computer,but you can use an IP or a file with multiple IP)
$diskinfo = Get-WmiObject Win32_LogicalDisk -ComputerName localhost | where {$_.DeviceId -eq 'C:'}
#Here you pull only the freespace value with Gb format (default is byte)
$freespace = $diskinfo.freespace/1Gb
$freespace = [int]$freespace
#here you pull the "limit number" off your file,must be in Gb and only the number is written in the file.
$limit=Get-Content -Path "B:\Dev\filewithsizeingo.txt"
$limit = [int]$limit
if ($freespace -gt $limit) #The free diskspace is greater than the limit
{
Write-Host "Diskfreespace is Above Limit" -f Green
}
elseif ($freespace -lt $limit) #The free diskspace is inferior than the limit
{
Write-Host "Free diskspace below limit" -f Red
#break
#code mail sending
}
Start-Sleep 1
}
由于是循环,因此可以在不停止脚本的情况下修改文件withsizeingo.txt,该脚本将在每次循环时刷新可用磁盘空间和限制值。
在elseif语句中,您可以插入一个中断并对电子邮件的发送进行编码(我尚不知道),不要忘记该中断,否则它将每秒发送一封邮件。
我希望它能对您有所帮助,或者至少可以给您带来新鲜的想法(我是Powershell的初学者,可以改进代码)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。