Powershell函数中的新行
此函数使用win32_logicaldisk类列出服务器详细信息,以提供特定服务器的信息。提供一台服务器时,表中的所有信息都清晰可读,但使用10台以上的服务器时,会变得更复杂一些。在功能内部或外部是否有一种方式为它所接收的每台服务器提供空间Powershell函数中的新行,powershell,powershell-2.0,Powershell,Powershell 2.0,此函数使用win32_logicaldisk类列出服务器详细信息,以提供特定服务器的信息。提供一台服务器时,表中的所有信息都清晰可读,但使用10台以上的服务器时,会变得更复杂一些。在功能内部或外部是否有一种方式为它所接收的每台服务器提供空间 Function Get-DiskInfo { param ($System =".") $display = @{label = "Server name" ; Expression={$_.systemname}}, `
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 ($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自动格式化。要在函数中处理多个服务器,请执行以下操作:
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自动格式化。在我的示例中,我使用$server,这将调用服务器列表10+您的示例使用相同的服务器名称。我想向函数提供任意数量的服务器,并让它为找到的每台服务器放置一个空间^!但是,您需要为列表中的每台服务器调用该函数,并以另一种方式调用它。例如,我有一个包含100台服务器的txt文件,这将是重复的codeAhh。。基思用一种方法回答细节。或者您可以看到我的更新。在我的示例中,我使用$server,这将调用服务器列表10+您的示例使用相同的服务器名称。我想向函数提供任意数量的服务器,并让它为找到的每台服务器放置一个空间^!但是,您需要为列表中的每台服务器调用该函数,并以另一种方式调用它。例如,我有一个包含100台服务器的txt文件,这将是重复的codeAhh。。基思用一种方法回答细节。或者你可以看到我的更新。谢谢!你能解释一下参数([string[]]$System=@(“.”)吗??我是新来的powershell,你可能会注意到!感谢您提供的任何帮助,这些帮助告诉PowerShell,
System
参数应采用字符串数组,如果未提供数组,PowerShell将使其成为字符串数组,例如,使-System server1
成为一个包含一个字符串的数组(server1)。这很有意义为帮助凯斯干杯谢谢!你能解释一下参数([string[]]$System=@(“.”)吗??我是新来的powershell,你可能会注意到!感谢您提供的任何帮助,这些帮助告诉PowerShell,System
参数应采用字符串数组,如果未提供数组,PowerShell将使其成为字符串数组,例如,使-System server1
成为一个包含一个字符串的数组(server1)。这很有意义为帮助凯斯干杯