1
我已經能夠得到這個工作,並輸出到屏幕上,但我想寫這到一個文本文件..PowerShell Invoke-RestMethod無法寫入輸出文件?
禮貌的: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"
但是,wrting整數輸出文件不起作用:
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
謝謝!
什麼是你的代碼的問題?提示:您應該使用'$ result = Join-Path $ scriptPath'results.txt''。 – sodawillow
它看起來可以很好的編寫字符串,但不能將整數寫入日誌文件,例如:'Add-Content $ result'Memory_Used'+ $ memory.total' – Leptonator
您可以在您的Q中包含您獲得的當前輸出文件? – sodawillow