I am having difficulty outputting a variable on the same line as a string of text. It works when I use Write-Host
, but not Write-Output
. I want to use Write-Output
because it is seemingly the best practice (keeping things in the pipeline), but Write-Output
always outputs the variable on a newline.
The line of code that I am having trouble with is:
Write-Host $VM "- VM name not valid: check the input file for spelling errors."
Also, I am fairly new to PowerShell scripting so I welcome any other input on this script.
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"
Concatenate the string
Write-Output ($VM + "- VM name not valid: check the input file for spelling errors.")