Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 脚本运行时PowerShell更新变量_Windows_Powershell_Scripting - Fatal编程技术网

Windows 脚本运行时PowerShell更新变量

Windows 脚本运行时PowerShell更新变量,windows,powershell,scripting,Windows,Powershell,Scripting,脚本处于运行状态时,如何更新PowerShell中的变量 我遇到这样一种情况:脚本连续监视磁盘的大小,并将其与共享驱动器上文本文件中的数字进行比较(例如Z:\quota\software share size.txt)。如果文本文件中的数字大于它监视的磁盘大小,则它会发送一封电子邮件,将磁盘扩展到文本文件中提到的新大小。但是一旦脚本启动,它就不会从文件中提取新的数字,我不想停止并启动脚本从文本文件加载新内容。请帮助也许这可以帮助您: while($true) { #Here i pull my

脚本处于运行状态时,如何更新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语句中,您可以插入一个中断并对发送电子邮件进行编码(我还不知道),不要忘记中断,否则它将每秒发送一封邮件


我希望它有帮助,或者至少它给了你fresh的想法(我是powershell的初学者,代码可以改进)。

你可以通过赋值来更新变量的值,即
$variable=
。请向我们展示您需要帮助的脚本