jenkins PowerShell脚本自动生成转储失败

b4lqfgs4  于 2023-04-29  发布在  Jenkins
关注(0)|答案(1)|浏览(223)

目标:编写一个powershell脚本来自动生成堆转储和线程转储(Tomcat)。我已经在Jenkins中配置了一个使用脚本的作业,这个想法是让任何人都能够使用Jenkins作业生成这些转储

编码

#######################################
Write-Host "Powershell Version"
$PSVersionTable.PSVersion
$env:UserDomain
$env:ComputerName
$env:UserName
$env:BUILD_USER
Write-Host "---------------------------------------------------`n"  
#########################################
    
$SecurePassword = $env:jenkins_password | ConvertTo-SecureString -AsPlainText -Force
$jenkins_username = $env:jenkins_username
$username = $env:jenkins_username
$credential = New-Object System.Management.Automation.PSCredential -ArgumentList $jenkins_username, $SecurePassword
$environment = $env:environment
#get server name
$servers = $env:servers.split(',')
#Current date
$datetime = Get-Date -Format "MMddyy_HHmmss"
$zip_sw = "\\usfl01.nexttravel.com\Software\7Zip"
$shareloc = "\\usfl01.nexttravel.com\JavaDumps"
$workdir = "D:\temp\dumps"
$smtpsrv = "us03smtphost.nexttravel.com"
$toemail = $env:toemail 

foreach ($server in $servers){
        Write-Host "$server create dump site using $jenkins_username"
            Invoke-Command -ComputerName $server -credential $credential -Scriptblock {
                    $workdir = "D:\temp\dumps"
                                        if (Test-Path $workdir) {
                        Write-Host "$workdir exists"
                    } else {
                    Write-Host "$workdir does not exist, creating it"
                    mkdir -p $workdir
                    }
                                  net share "temp_dump=d:\temp\dumps" /grant:"Authenticated Users",FULL
                } 
            }

### Copy 7zip executable to servers ###
foreach ($server in $servers) {
        Write-Host "Copy 7zip executable to server"
        Robocopy $zip_sw "\\$server\temp_dump" 7zr.exe
            }

#Dump commands 
foreach ($server in $servers) {
        Write-Host "Creating dumps"
            Invoke-Command -ComputerName $server -credential $credential -Scriptblock {
                                $workdir = "D:\temp\dumps"
                                $datetime = Get-Date -Format "MMddyy_HHmmss"
                                $hostname = $env:ComputerName 
                    #Get pid for tomcat process 
                $ServicePID = Get-Process -Name tomcat | select -expand id

                                cd D:\java\bin 
                                #look at changing order of dumps
                                .\jmap.exe -dump:format=b,file=$workdir\$hostname-$datetime-heapdump.hprof $ServicePID
                    .\jhsdb jstack --pid "$ServicePID" -l > $workdir\$hostname-$datetime-threaddump.txt
                                Write-Host "Thread dump file created"
                }
            }   

#Compress files,copy dumps off server(s)
foreach ($server in $servers) {
        Write-Host "Compressing dumps"
            Invoke-Command -ComputerName $server -credential $credential -Scriptblock {
                                $workdir = "D:\temp\dumps"
                                $datetime = Get-Date -Format "MMddyy_HHmmss"
                                $hostname = $env:ComputerName
                                cd $workdir
                .\7zr.exe a -t7z $hostname-$datetime-heapdump.hprof.7z *.hprof
                        .\7zr.exe a -t7z $hostname-$datetime-threaddump.txt.7z *.txt
                }
            }
#Get heap dump name for summary section#
foreach ($server in $servers) {
        Write-Host "Filenames"
            $heapdumpname = Invoke-Command -ComputerName $server -credential $credential -Scriptblock {
                                $workdir = "D:\temp\dumps"
                                if ((Test-Path -Path $workdir) -eq $true){
                                   $heapdumpname =  Get-ChildItem -Path $workdir | where {$_.Name -like "*heapdump.hprof.7z"}                              
Return "$heapdumpname"
                                }

                      }
                    }
Get thread dump name for summary section#
foreach ($server in $servers) {
        Write-Host "Filenames"
            $threaddumpname = Invoke-Command -ComputerName $server -credential $credential -Scriptblock {
                                $workdir = "D:\temp\dumps"
                                if ((Test-Path -Path $workdir) -eq $true){
                                   $threaddumpname =  Get-ChildItem -Path $workdir | where {$_.Name -like "*threaddump.txt.7z"}

Return "$threaddumpname"
                }
            }
                    }

问题:脚本抱怨Get的一些问题。有人能建议我应该做什么改变来避免这个错误吗?如果我尝试在QA机器上运行我的脚本,我就不会有这个错误。

Creating dumps
Heap dump file created
Thread dump file created
Compressing dumps
The term '..\7zr.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the 
spelling of the name, or if a path was included, verify that the path is correct and try again.
    + CategoryInfo          : ObjectNotFound: (..\7zr.exe:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
    + PSComputerName        : us03l11app01.nexttravel.com
 
The term '..\7zr.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the 
spelling of the name, or if a path was included, verify that the path is correct and try again.
    + CategoryInfo          : ObjectNotFound: (..\7zr.exe:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
    + PSComputerName        : us03l11app01.nexttravel.com
 
Filenames
Get : The term 'Get' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the 
spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\svcjenkins\AppData\Local\Temp\jenkins13475594361797696998.ps1:90 char:1
+ Get thread dump name for summary section#
+ ~~~
    + CategoryInfo          : ObjectNotFound: (Get:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
jfewjypa

jfewjypa1#

第一个建议是在Get thread dump name for summary section中添加“#”来注解这部分(我不知道你复制代码时是否有错别字)。
您是否检查了7z二进制文件是否放置在正确的文件夹中,并将其移动到相同的位置?

相关问题