powershell 脚本更新以检查百分比磁盘空间和退出 jenkins 阶段取决于

powershell script update to check percent diskspace & exit jenkins stage depending on that

我会检查百分比磁盘空间并根据它退出 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% 的可用空间,以及是否有 低于 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% 空闲 space):

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% 免费时 space:

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
    }