0

私はpowershell + powercliに非常に慣れていないため、手を必要としています。

3 つまたは 4 つのホストが vCenter インスタンスに接続されています。実行中の VM を識別し、名前をファイルに記録し、マシンを一時停止 (またはシャットダウン) する PowerShell スクリプトを実行したいと考えています。

次回スクリプトが実行されると、名前のリストが読み取られ、関連する VM の電源がオンになります。PowerCLI/Powershell 環境でこれを行う最も簡単な方法は何ですか。

私はストリームリーダー/ライターを考えていましたが、それは複雑に思えます!

4

2 に答える 2

0

を使用することを検討し、Join-Pathより多くの Powershell の方法を検討してください。そのようです、Set-ContentAdd-Conent

# Combine $filepath and MTS_ON.txt, adds slashes if need be
$pfile = join-path $filePath "MTS_ON.txt" 
# Create empty file or truncate existing one
set-content -path $pfile -value $([String]::Empty) 

foreach($objHost in $ESXHost) {
    $PoweredVMs += Get-VMHost -Name $objHost | Get-VM | where {$_.Powerstate -eq "PoweredON" }
    foreach($objVM in $PoweredVMs) {
        add-content -path $pfile -value $objVM
        Get-VM -Name $objVM | Suspend-VM
    }
} # No need to close the pfile
于 2012-09-19T09:40:24.760 に答える
0

これはトリックを行うようです!助言がありますか?

#File Storage Path
$filePath = "D:\"

#Get the host lists and sort
$ESXHost= Get-VMHost | Sort-Object -Property Name

function storeEnvironment
{#Store the powered VM list to a simple file
    #Use Streamwriter for simpler output, just a string name
    $PowerFile = New-Object System.IO.StreamWriter($filePath+"MTS_ON.txt")

    foreach($objHost in $ESXHost)
    {
        $PoweredVMs += Get-VMHost -Name $objHost | Get-VM | where {$_.Powerstate -eq "PoweredON" }
        foreach($objVM in $PoweredVMs)
        {
            $PowerFile.WriteLine($objVM)
            Get-VM -Name $objVM | Suspend-VM
        }
    } 
    $PowerFile.close()
}

function restoreEnvironment
{
    [array] $VMs = Get-Content -Path $filePath"MTS_ON.txt"
    foreach($VM in $VMs)
    {
        Get-VM -Name $VM | Start-VM
    }
    #Delete the configuration file
    Remove-Item $filePath"MTS_ON.txt"
}

#MAIN

#Test to see if the config file exists
if(Test-Path $filePath"MTS_ON.txt") 
{
    Write-Host "Restore from file? [Y]es or [N]o"
    $response = Read-Host
    if($response -eq "Y")
    {
        #Use file to restore VMs
        Write-Host "Restore Environment"
        restoreEnvironment
    }
    else
    {
        #Delete the configuration file
        Remove-Item $filePath"MTS_ON.txt"
    }   
}
else
{#Save the powered VMs to a file
    Write-Host "Saving Environment"
    storeEnvironment
}
于 2012-09-19T08:24:52.103 に答える