写主机与写输出 - 换行符

时间:2014-01-30 13:12:30

标签: powershell newline carriage-return powercli

我在输出与文本字符串相同的行上的变量时遇到困难。它在我使用Write-Host时有效,但在Write-Output时无效。我想使用Write-Output因为它似乎是最佳实践(保持管道中的内容),但Write-Output总是在换行符上输出变量。

我遇到问题的代码行是:

  

Write-Host $ VM“ - VM名称无效:检查输入文件是否有拼写错误。”

另外,我是PowerShell脚本的新手,所以我欢迎这个脚本的任何其他输入。

Function Get-VMSwapping {

    #requires –version 3.0

    <#
    .SYNOPSIS 
    Outputs the following: Cluster, VMHost, VM, SwappedMemory, BalloonedMemory.

    .DESCRIPTION
    Outputs the following: Cluster, VMHost, VM, SwappedMemory, BalloonedMemory.
    Make sure to be connected to the vCenter server that has all of the VMs to enumerate.

    .NOTES
    Author: Nick Sousa
    Date: Jan 28, 2014
    Version: 1.0

    #>

    [CmdletBinding(
        HelpURI='https://www.vmware.com/support/developer/PowerCLI/',
        SupportsShouldProcess=$true,
        ConfirmImpact="High"
        )]

    Param(
        [ValidateScript({
            If(Test-Path -Path $_) {$true}
            Else {Throw "Input filename is not valid."}
        })]
        [Parameter(Mandatory=$false, ParameterSetName="InputFile",Position = 0)][string]$InputFile
    )

    Begin {
        $ErrorActionPreference = "Stop"
    } # End "Begin"

    Process {

        Try {

            If($PSCmdlet.ShouldProcess("VMs that are ballooning & swapping")) { 

                If($PSBoundParameters.ContainsKey('InputFile')) {

                    $VMReport = @()

                    # Loop through each item in the file specified in -InputFile.
                    # If the VM cannot be validated with Get-VM, write an error to the screen.
                    # If the VM is found within vCenter, output its Swap/Balloon amounts.
                    ForEach($VM in Get-Content -Path $InputFile) {

                        If(Get-VM -Name $VM -ErrorAction SilentlyContinue) {
                            $VMFull = Get-VM -Name $VM
                            $VMName = $VMFull.Name

                            $VMView = Get-View -ViewType VirtualMachine -Filter @{"Name" = "^$VMName$"}

                            $obj = "" | Select-Object Cluster, VMHost, VM, SwappedMemory, BalloonedMemory

                            $obj.Cluster = $VMFull.Host.Parent.Name
                            $obj.VMHost = $VMFull.Host.Name
                            $obj.VM = $VMView.Name
                            $obj.SwappedMemory = $VMView.Summary.QuickStats.SwappedMemory
                            $obj.BalloonedMemory = $VMView.Summary.QuickStats.BalloonedMemory

                            $VMReport += $obj

                        } # End "If(Get-VM -Name $VM)"

                        Else {

                            Write-Host $VM  "- VM name not valid: check the input file for spelling errors."

                        } # End Else for "If(Get-VM -Name $VM)"

                    } # End "ForEach($VM in $VMs)"

                    $VMReport | Format-Table -AutoSize

                } # End "If($PSBoundParameters.ContainsKey('InputFile'))"

                Else {

                    # Pull a list of all VMs that have Balloon Memory greater than 0.
                    # Loop through all VMs found and prepare a collection object to be outputted.
                    $VMReport = @()

                    $VMs = Get-View -ViewType VirtualMachine | Where-Object {$_.Summary.QuickStats.BalloonedMemory -ne "0"}

                    ForEach($VM in $VMs) {

                        $VMFull = Get-VM -Name $VM.Name

                        $obj = "" | Select-Object Cluster, VMHost, VM, SwappedMemory, BalloonedMemory

                        $obj.Cluster = $VMFull.Host.Parent.Name
                        $obj.VMHost = $VMFull.Host.Name
                        $obj.VM = $VM.Name
                        $obj.SwappedMemory = $vm.Summary.QuickStats.SwappedMemory
                        $obj.BalloonedMemory = $vm.Summary.QuickStats.BalloonedMemory

                        $VMReport += $obj

                    } # End "ForEach($VM in $VMs)"

                    $VMReport | Format-Table -AutoSize

                } # End Else for "If($PSBoundParameters.ContainsKey('InputFile'))"

            } # End "If($PSCmdlet.ShouldProcess("VMs that are ballooning & swapping"))"

        } # End "Try"

        Catch {
            Write-Output "Caught an exception:"
            Write-Output "Exception Type: $($_.Exception.GetType().FullName)"
            Write-Output "Exception Message: $($_.Exception.Message)"

        } # End "Catch"

        Finally {

            $ErrorActionPreference = "Continue"

        } # End "Finally"

    } # End "Process"

} # End "Function Get-VMSwapping"

1 个答案:

答案 0 :(得分:2)

连接字符串

Write-Output ($VM + "- VM name not valid: check the input file for spelling errors.")