PowerShell Invoke-RestMethod无法写入输出文件?

时间:2017-05-20 14:42:23

标签: powershell logging invoke

我已经能够将其工作并输出到屏幕,但我想将其写入文本文件..

礼貌:https://www.reddit.com/r/PowerShell/comments/3iced0/gettomcat_status/

$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
$result=$scriptPath+'\results.txt'
New-Item -ItemType file $result -Force


function Get-TomcatStatus{
     param(
         $tomcatserver ,
         $tomcatport = 8080,
         $tomcatuser = "user",
         $tomcatpassword = "pass"
     )
     #Get Credentials into the right format
     $tomcatpassword = ConvertTo-SecureString -string $tomcatpassword -asplaintext -force
     $cred = New-object -TypeName System.Management.Automation.PSCredential -argumentlist ($tomcatuser,$tomcatpassword)

     #Invoke the restinterface 
     $TomcatStats = Invoke-RestMethod -Uri http://$tomcatserver`:$tomcatport/manager/status?XML=true -Credential $cred

     $threads = $TomcatStats.GetElementsByTagName("threadInfo") | ? {$_.currentThreadCount -ne 0}

     $Memory = $TomcatStats.GetElementsByTagName("memory") 

     $requestInfo = $TomcatStats.GetElementsByTagName("requestInfo") | ? {$_.bytessent -ne 0}

     #Populate output object
     $Output = New-object PsObject
     $output | add-member -type noteproperty -name "Server" -Value $tomcatserver
     Add-Content $result "Server $tomcatserver"
     #Memory
     $output | add-member -type noteproperty -name "Memory_Used" -Value $memory.total
     Add-Content $result 'Memory_Used'+ $memory.total
     $output | add-member -type noteproperty -name "Memory_free" -Value $memory.free
     #Add-Content $result 'Memory_free '+$memory.free
     $output | add-member -type noteproperty -name "Memory_Max" -Value $memory.max
     #Add-Content $result 'Memory_Max '+$memory.max
     #threads
     $output | add-member -type noteproperty -name "Current_Threads_busy" -Value $threads.currentThreadsBusy
     #Add-Content $result 'Current_Threads_busy '+$threads.currentThreadsBusy
     $output | add-member -type noteproperty -name "Current_Thread_Count" -Value $threads.currentThreadCount
     #Add-Content $result 'Current_Thread_Count '+$threads.currentThreadCount
     $output | add-member -type noteproperty -name "Max_Threads" -Value $threads.MaxThreads
     #Add-Content $result 'Max_Threads '+$threads.MaxThreads
     #requestInfo
     $output | add-member -type noteproperty -name "Request_Max_ProcessingTime_ms" -Value $requestInfo.maxTime
     #Add-Content $result 'Request_Max_ProcessingTime_ms '+$requestInfo.maxTime
     $output | add-member -type noteproperty -name "Request_Count" -Value $requestInfo.requestcount
     #Add-Content $result 'Request_Count '+$requestInfo.requestcount
     $output | add-member -type noteproperty -name "ProcessingTime_total_s" -Value $requestInfo.processingTime
     #Add-Content $result 'ProcessingTime_total_s '+$requestInfo.processingTime

     Return $output
 }

Get-TomcatStatus SERVER1
Get-TomcatStatus SERVER2
Get-TomcatStatus SERVER3
Get-TomcatStatus SERVER4
Get-TomcatStatus SERVER5
Get-TomcatStatus SERVER6

我试图解决的问题是我可以在输出文件中写入以下行,就好了:

Add-Content $result "Server $tomcatserver"

但是,输出文件中的整数是不起作用的:

Add-Content $result Memory_Used [string]$memory.total

我应该看到的是:

Server                        : SERVER1
Memory_Used                   : 1948254208
Memory_free                   : 820702992
Memory_Max                    : 1948254208
Current_Threads_busy          : 1
Current_Thread_Count          : 27
Max_Threads                   : 150
Request_Max_ProcessingTime_ms : 156454
Request_Count                 : 669515
ProcessingTime_total_s        : 88929596

谢谢!

1 个答案:

答案 0 :(得分:0)

使用

Add-Content $result ("Memory_Used: " + $memory.total)

Add-Content $result "Memory_Used: $($memory.total)"