gpt4 book ai didi

powershell - 写主机与写输出-换行符

转载 作者:行者123 更新时间:2023-12-02 23:46:19 27 4
gpt4 key购买 nike

我很难在与文本字符串相同的行上输出变量。当我使用Write-Host而不是Write-Output时,它可以工作。我想使用Write-Output,因为这似乎是最佳做法(将内容保留在管道中),但是Write-Output始终在换行符上输出变量。

我遇到麻烦的代码行是:

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



另外,我对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"

最佳答案

连接字符串

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

关于powershell - 写主机与写输出-换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21457530/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com