powershell - powershell 脚本更新以检查磁盘空间百分比并退出 jenkins 阶段,具体取决于
问题描述
我会检查磁盘空间百分比并退出詹金斯阶段,这取决于
我想我可以解决以下问题:
PS C:\Users\Administrator> Get-WmiObject win32_logicaldisk | select name,freespace,size,@{Name='%';Expression={($_.freespace/ $_.size)*100}}
name freespace size %
---- --------- ---- -
C: 15582232576 79473668096 19.6067866871043
D:
E:
F: 247559806976 449998483456 55.0134758399038
PS C:\Users\Administrator>
我当前的 Jenkins groovy 文件步骤如下所示:
stage ('test') {
agent {
node {
label "test"
customWorkspace "F:\\app"
}
}
steps
{
script
{
bat """
f:
cd \\app
python somescript.py
python otherscript.py
Powershell("Get-wmiObject -Class win32_logicaldisk")
"""
}
}
问题有几点:
- 如何只选择 F: ?
- 如何将 % 舍入为整数?
- 如何将其与固定值(比如说 80%)进行比较?
- 如果未超过此值,无论状态如何,如何将 Jenkins 步骤退出为“OK”?(和“KO” exle)
解决方案
舍入并限制为 F 加上检查是否超过 80% 的空闲以及是否低于80% 的空闲退出 1 这应该促使 Jenkins 认为该步骤失败:
$Full=Get-WmiObject win32_logicaldisk |
select name,freespace,size,
@{Name='%';Expression={[math]::Round(($_.freespace/ $_.size)*100)}},
@{Name='Full';Expression={[math]::Round(($_.freespace/ $_.size)*100) -lt 80}} |
where name -eq "F:" |
select -Unique Full ;
if ($Full.Full -eq $True) { $host.SetShouldExit(1); } else { $host.SetShouldExit(0); }
手动测试它(这里使用 F: 分区,24% 可用空间):
PROMPT>powershell -command "$Full=Get-WmiObject win32_logicaldisk | select name,freespace,size,@{Name='%';Expression={[math]::Round(($_.freespace/ $_.size)*100)}},@{Name='Full';Expression={[math]::Round(($_.freespace/ $_.size)*100) -lt 80}} | where name -eq "F:" | select -Unique Full ; if ($Full.Full -eq $True) { $host.SetShouldExit(1); } else { $host.SetShouldExit(0); }"
PROMPT>echo %ERRORLEVEL%
1
当阈值设置为 20% 可用空间时:
PROMPT>powershell -command "$Full=Get-WmiObject win32_logicaldisk | select name,freespace,size,@{Name='%';Expression={[math]::Round(($_.freespace/ $_.size)*100)}},@{Name='Full';Expression={[math]::Round(($_.freespace/ $_.size)*100) -lt 20}} | where name -eq "F:" | select -Unique Full ; if ($Full.Full -eq $True) { $host.SetShouldExit(1); } else { $host.SetShouldExit(0); }"
PROMPT>echo %ERRORLEVEL%
0
groovy 文件中的命令应嵌入整行,如下所示:
Powershell("$Full=Get-WmiObject win32_logicaldisk | select name,freespace,size,@{Name='%';Expression={[math]::Round(($_.freespace/ $_.size)*100)}},@{Name='Full';Expression={[math]::Round(($_.freespace/ $_.size)*100) -lt 80}} | where name -eq "F:" | select -Unique Full ; if ($Full.Full -eq $True) { $host.SetShouldExit(1); } else { $host.SetShouldExit(0); }")
如果您想直接在 jenkins 节点中编写动作脚本:
def status = Powershell(returnStatus: true, script: "$Full=Get-WmiObject win32_logicaldisk | select name,freespace,size,@{Name='%';Expression={[math]::Round(($_.freespace/ $_.size)*100)}},@{Name='Full';Expression={[math]::Round(($_.freespace/ $_.size)*100) -lt 80}} | where name -eq "F:" | select -Unique Full ; if ($Full.Full -eq $True) { $host.SetShouldExit(1); } else { $host.SetShouldExit(0); }")
if (status == 0) {
// F: has enough space
} else {
// F: is Full
}
推荐阅读
- php - WordPress:从我的内容模板中删除 get_the_title
- android - 通过 Intent 启动 Here 地图导航
- java - 无法在eclipse中观看表达式
- linux-kernel - 内置驱动程序是否总是优先于可加载模块?
- php - soundcloud api 给出空白页
- c# - 真的没有办法从包含带有方法体的 IL 的 byte[] 中“生成”C#?
- excel - 查找在两个单独的列中有重复的行
- r - 在R中的给定子数据中划分所有可能的行
- javascript - Firebase 函数中的条件
- javascript - 内容丰富:documentToHtmlString 不包含富文本中的嵌入图像