繁体   English   中英

Powershell功能内的新线

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM