[英]Powershell New line within function
此函數使用win32_logicaldisk類列出服務器詳細信息,以提供有關特定服務器的信息。 提供一個服務器的表提供了清晰可讀的所有信息,但使用10多台服務器會變得有點復雜。 在函數內部或外部是否有一種方法為它所拾取的每個服務器提供空間。
Function Get-DiskInfo
{
param ($System =".")
$display = @{label = "Server name" ; Expression={$_.systemname}}, `
@{label = "Drive" ; Expression={$_.DeviceID}}, `
@{label = "Volume Name" ; Expression={$_.volumename}}, `
@{label = "File Sytem" ; Expression={$_.filesystem}}, `
@{label = "size (GB)" ; Expression={ [Math]::round($_.size / 1gb)}}, `
@{label = "Free Space (GB)" ; Expression={ [Math]::round($_.freespace / 1gb)}}, `
@{label = "Free %" ; Expression={ [Math]::round($_.freespace / $_.size * 100)}}
Get-WmiObject win32_logicaldisk -computername $server | format-table $display -auto
write-host "testing"
# THIS FUNCTION DETAILS DISK SPACE AND $% REMAINING FOR A SERVER
}
只需在函數末尾添加""
即可。 這將在結尾處放一個空行。 你甚至可以想象並做一些事情:
Function Get-DiskInfo
{
param ($System =".")
write-host $system "----------------"
$display = @{label = "Server name" ; Expression={$_.systemname}}, `
@{label = "Drive" ; Expression={$_.DeviceID}}, `
@{label = "Volume Name" ; Expression={$_.volumename}}, `
@{label = "File Sytem" ; Expression={$_.filesystem}}, `
@{label = "size (GB)" ; Expression={ [Math]::round($_.size / 1gb)}}, `
@{label = "Free Space (GB)" ; Expression={ [Math]::round($_.freespace / 1gb)}}, `
@{label = "Free %" ; Expression={ [Math]::round($_.freespace / $_.size * 100)}}
Get-WmiObject win32_logicaldisk -computername $System | format-table $display -auto
""
# THIS FUNCTION DETAILS DISK SPACE AND $% REMAINING FOR A SERVER
}
Get-DiskInfo localhost
Get-DiskInfo .
Get-DiskInfo 7vm01
這會產生以下結果:
localhost ----------------
Server name Drive Volume Name File Sytem size (GB) Free Space (GB) Free %
----------- ----- ----------- ---------- --------- --------------- ------
7VM01 A: 0 0
7VM01 C: NTFS 80 29 36
7VM01 D: 0 0
. ----------------
Server name Drive Volume Name File Sytem size (GB) Free Space (GB) Free %
----------- ----- ----------- ---------- --------- --------------- ------
7VM01 A: 0 0
7VM01 C: NTFS 80 29 36
7VM01 D: 0 0
7vm01 ----------------
Server name Drive Volume Name File Sytem size (GB) Free Space (GB) Free %
----------- ----- ----------- ---------- --------- --------------- ------
7VM01 A: 0 0
7VM01 C: NTFS 80 29 36
7VM01 D: 0 0
要在機器列表上運行,可以將列表加載到字符串中,然后使用foreach遍歷字符串。
foreach($server in $serverlist){
Get-DiskInfo $server
}
要處理函數內的多個服務器,請執行以下操作
Function Get-DiskInfo
{
param ([string[]]$System =@("."))
foreach ($s in $server) {
$display = @{label = "Server name" ; Expression={$_.systemname}}, `
@{label = "Drive" ; Expression={$_.DeviceID}}, `
@{label = "Volume Name" ; Expression={$_.volumename}}, `
@{label = "File Sytem" ; Expression={$_.filesystem}}, `
@{label = "size (GB)" ; Expression={ [Math]::round($_.size / 1gb)}}, `
@{label = "Free Space (GB)" ; Expression={ [Math]::round($_.freespace / 1gb)}}, `
@{label = "Free %" ; Expression={ [Math]::round($_.freespace / $_.size * 100)}}
Get-WmiObject win32_logicaldisk -computername $s | format-table $display -auto
# THIS FUNCTION DETAILS DISK SPACE AND $% REMAINING FOR A SERVER
}
}
但是,當您從這樣的函數輸出格式化文本時,您無法真正使用來自的數據。 你可以看到它經過精心設計的格式化,但是以編程方式使用它 - 你又回到解析文本了。 我會這樣做:
Function Get-DiskInfo
{
param ([string[]]$System =@("."))
foreach ($s in $server) {
Get-WmiObject win32_logicaldisk -computername $s | Foreach {new-object psobject `
-property @{ServerName = $_.systemname; `
Drive=$_.DeviceID; `
VolumeName=$_.volumename; `
FileSystem=$_.filesystem; `
SizeGB=[Math]::round($_.size / 1gb); `
FreeSpaceGB=[Math]::round($_.freespace / 1gb); `
FreePercent=[Math]::round($_.freespace / $_.size * 100)} `
}
# THIS FUNCTION DETAILS DISK SPACE AND $% REMAINING FOR A SERVER
}
}
現在,您正在輸出包含所需數據的對象。 您可以在函數調用后始終使用Format-Table格式化字段。 或者,如果您想獲得真正的冒險,您可以考慮使用Update-FormatData來使您的自定義對象由PowerShell自動格式化。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.