各位版友安安。
小弟最近公司專案有一項是用Powershell去查看同仁的設備硬體的狀況
所以小弟用有找到相關的文章後,試著設計後,遇到不太會匯出的狀況
附上小弟的Coding,麻煩各位大大給我相關的建議了!
感恩
Coding:
function get_disk_free(){
$disk = Get-WmiObject -Class win32_logicaldisk
$freedisk = $disk.FreeSpace
$freedisk =(($freedisk | Measure-Object -Sum).sum /1gb)
return @($freedisk)
}
echo "{'硬碟剩餘空間':'$(get_disk_free)'}"
function get_disk_used_percent(){
$disk = Get-WmiObject -Class win32_logicaldisk
$allSpace = $disk.Size
$allSpace =(($allSpace | Measure-Object -Sum).sum /1gb)
$FreeSpace = $disk.FreeSpace
$FreeSpace =(($FreeSpace | Measure-Object -Sum).sum /1gb)
$disk_used_percent = (($FreeSpace/$allSpace)*100)
return @($disk_used_percent)
}
echo "{'硬碟可使用率':'$(get_disk_used_percent)'}"
function get_memory_count(){
"{0:N2}GB" -f (((Get-WmiObject -Class Win32_PhysicalMemory).capacity | Measure-Object -Sum).sum /1gb)}
echo "{'記憶體可使用量': '$(get_memory_count)'}"
#獲取主機ip
function get_ip(){
((Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'").IPAddress -notlike ":")[2]}
echo "主機IP:$(Get-NetIPAddress -AddressFamily IPV4)"
get_ip| Out-File -filepath C:\PS\TEST.txt
#獲取作業系统版本
function get_type(){
Get-WmiObject -Class Win32_OperatingSystem | Select-Object -ExpandProperty Caption}
echo "作業系統:$(get_type)"
function get_logical_cpu_cnt () {
$cpu_info = Get-WmiObject win32_processor
return @($cpu_info).count * $cpu_info.NumberOfLogicalProcessors
}
echo "CPU邏輯核心:$(get_logical_cpu_cnt)"
function free_physics_ram(){
$ops = Get-WmiObject -Class Win32_OperatingSystem
#"可用内存(MB): {0}" -f ([math]::round($ops.FreePhysicalMemory / 1kb, 2))
$ops =([math]::round(($ops.FreePhysicalMemory / (1mb)), 2))
return @($ops)
}
echo "可用內存:$(free_physics_ram) GB"
function cpu_percent()
{
$cpu = Get-WmiObject -Class Win32_Processor
$Havecpu = $cpu.LoadPercentage
return @($Havecpu)
}
echo "CPU使用率:$(cpu_percent)%"
function phy_percent()
{
$men = Get-WmiObject -Class win32_OperatingSystem
$Allmen = ($men.TotalVisibleMemorySize / 1KB)
$Freemen = ($men.FreePhysicalMemory / 1KB)
$Permem = ((($men.TotalVisibleMemorySize-$men.FreePhysicalMemory)/$men.TotalVisibleMemorySize)*100)
return @($Permem)
}
echo "內存使用率:$(phy_percent)%"
3/23新增兩個新問題
一個是想跟各位大大求問,因為目前輸出後是以多行呈現,但我們想要是重複寫入的狀態,所以想改成一次一行的輸出結果
另一個是想跟各位大大求助,要怎麼重複寫入同一份txt。
再麻煩各位大大協助指導了!!
非常感謝
function get_disk_free(){
$disk = Get-WmiObject -Class win32_logicaldisk
$freedisk = $disk.FreeSpace
$freedisk =(($freedisk | Measure-Object -Sum).sum /1gb)
return @($freedisk)
}
echo "{'硬碟剩餘空間':'$(get_disk_free)'}" | Out-File -FilePath ".\硬碟剩餘空間.txt"
匯出文字檔
皮皮大
不好意思 語法是:
function get_disk_free(){
$disk = Get-WmiObject -Class win32_logicaldisk
$freedisk = $disk.FreeSpace
$freedisk =(($freedisk | Measure-Object -Sum).sum /1gb)
return @($freedisk)
}
echo "{'硬碟剩餘空間':'$(get_disk_free)'}" | Out-File -Filepath "C:\PS\test.txt"
function get_disk_used_percent(){
$disk = Get-WmiObject -Class win32_logicaldisk
$allSpace = $disk.Size
$allSpace =(($allSpace | Measure-Object -Sum).sum /1gb)
$FreeSpace = $disk.FreeSpace
$FreeSpace =(($FreeSpace | Measure-Object -Sum).sum /1gb)
$disk_used_percent = (($FreeSpace/$allSpace)*100)
return @($disk_used_percent)
}
echo "{'硬碟可使用率':'$(get_disk_used_percent)'}" | Out-File -Filepath "C:\PS\test.txt" -Append
function get_memory_count(){
"{0:N2}GB" -f (((Get-WmiObject -Class Win32_PhysicalMemory).capacity | Measure-Object -Sum).sum /1gb)}
echo "{'記憶體可使用量': '$(get_memory_count)'| Out-File -Filepath "C:\PS\test.txt"
#獲取主機ip
function get_ip(){
((Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'").IPAddress -notlike ":")[2]}
echo "主機IP:$(Get-NetIPAddress -AddressFamily IPV4)" | Out-File -Filepath "C:\PS\test.txt"
#獲取作業系统版本
function get_type(){
Get-WmiObject -Class Win32_OperatingSystem | Select-Object -ExpandProperty Caption}
echo "作業系統:$(get_type)" | Out-File -Filepath "C:\PS\test.txt"
function get_logical_cpu_cnt () {
$cpu_info = Get-WmiObject win32_processor
return @($cpu_info).count * $cpu_info.NumberOfLogicalProcessors
}
echo "CPU邏輯核心:$(get_logical_cpu_cnt)" | Out-File -Filepath "C:\PS\test.txt"
function free_physics_ram(){
$ops = Get-WmiObject -Class Win32_OperatingSystem
#"可用内存(MB): {0}" -f ([math]::round($ops.FreePhysicalMemory / 1kb, 2))
$ops =([math]::round(($ops.FreePhysicalMemory / (1mb)), 2))
return @($ops)
}
echo "可用內存:$(free_physics_ram) GB" | Out-File -Filepath "C:\PS\test.txt"
function cpu_percent()
{
$cpu = Get-WmiObject -Class Win32_Processor
$Havecpu = $cpu.LoadPercentage
return @($Havecpu)
}
echo "CPU使用率:$(cpu_percent)%" | Out-File -Filepath "C:\PS\test.txt"
function phy_percent()
{
$men = Get-WmiObject -Class win32_OperatingSystem
$Allmen = ($men.TotalVisibleMemorySize / 1KB)
$Freemen = ($men.FreePhysicalMemory / 1KB)
$Permem = ((($men.TotalVisibleMemorySize-$men.FreePhysicalMemory)/$men.TotalVisibleMemorySize)*100)
return @($Permem)
}
echo "內存使用率:$(phy_percent)%" | Out-File -Filepath "C:\PS\test.txt" -Append
這樣對嗎?,還是每一個最後要加-Append?
-Append : 將輸出添加到現有文件的末尾
所以除了第一個輸出不用加,其他的都要在同一個檔案就要加
了解!謝謝皮皮大,目前測試中,PowerShell卡住了XD"
如果是我,我的作法會是先將所有資料存成 Hashtable ,轉成 PSObject ,存入陣列
這樣這個陣列要轉成 CSV/JSON/XML/HTML 都很方便
一次寫一筆也可以 (-Append),但是效率會比較不好,建議全部資料處理完,在一次轉換成輸出的格式來寫入