脚本为 运行 时 PowerShell 更新变量
PowerShell update variable while script is running
如何在脚本处于 运行 状态时更新 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
}
因为是循环,所以可以在不停止脚本的情况下修改filewithsizeingo.txt,脚本会在每次循环时刷新空闲磁盘空间和限制值。
在elseif语句中,你可以插入一个break并编码发送邮件(我还不知道),不要忘记break,否则它会每秒发送一封邮件。
希望对你有所帮助,或者至少给了你新鲜的想法(我是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
}
因为是循环,所以可以在不停止脚本的情况下修改filewithsizeingo.txt,脚本会在每次循环时刷新空闲磁盘空间和限制值。
在elseif语句中,你可以插入一个break并编码发送邮件(我还不知道),不要忘记break,否则它会每秒发送一封邮件。
希望对你有所帮助,或者至少给了你新鲜的想法(我是powershell的初学者,代码可以改进)。