首页 > 解决方案 > 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")
                        """
                }
            }

问题有几点:

标签: powershelljenkins-pipeline

解决方案


舍入并限制为 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
    }

推荐阅读