Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
仮想マシンからデータ交換サービスのデータを取得する関数
<#
.SYNOPSIS
仮想マシンからデータ交換サービスのデータを取得します。
.PARAMETER VMName
仮想マシンの名称を指定します
.EXAMPLE
Get-VMKeyExchangeData -VMName 'MyVM'
.NOTES
Windows Server 2012以降でのみ利用可能です。
#>
function Get-VMKeyExchangeData {
[CmdletBinding()]
param (
[Parameter(ParameterSetName = 'VM', Mandatory = $true, ValueFromPipeline = $true)]
[Microsoft.HyperV.PowerShell.VirtualMachine]$InputObject,
[Parameter(ParameterSetName = 'Default', Mandatory = $true, Position = 0)]
[string]$VMName
)
begin {
$RootNameSpace = 'root\virtualization\v2'
}
process {
switch ($PsCmdlet.ParameterSetName) {
'VM' {
$VMName = $InputObject.VMName
}
}
Write-Verbose ("Parameter VMName = {0}" -f $VMName)
# 同名の仮想マシンが複数ある場合がある
$VMSystems = Get-CimInstance -Namespace $RootNameSpace -Query "Select * From Msvm_ComputerSystem Where ElementName='$VMName'"
if ($null -eq $VMSystems) {
return [PSCustomObject]@{}
}
foreach ($VM in $VMSystems) {
$Result = @{}
Get-CimAssociatedInstance -Namespace $RootNameSpace -InputObject $VM -Association 'Msvm_SystemDevice' -ResultClassName 'Msvm_KvpExchangeComponent' `
| Select-Object -ExpandProperty GuestIntrinsicExchangeItems `
| ForEach-Object {
$CimXML = [Xml]$_
$Result[$CimXML.SelectSingleNode("/INSTANCE/PROPERTY[@NAME='Name']").VALUE] = $CimXML.SelectSingleNode("/INSTANCE/PROPERTY[@NAME='Data']").VALUE
}
[PSCustomObject]$Result
}
}
}
<#
.SYNOPSIS
仮想マシンからデータ交換サービスのデータを取得します。
.PARAMETER VMName
仮想マシンの名称を指定します
.EXAMPLE
Get-VMKeyExchangeData -VMName 'MyVM'
.NOTES
Windows Server 2012以前でのみ利用可能です。
#>
function Get-VMKeyExchangeDataV1 {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, Position = 0)]
[string]$VMName
)
begin {
$RootNameSpace = 'root\virtualization'
}
process {
# 同名の仮想マシンが複数ある場合がある
$VMSystems = Get-WmiObject -Namespace $RootNameSpace -Query "Select * From Msvm_ComputerSystem Where ElementName='$VMName'"
if ($null -eq $VMSystems) {
return [PSCustomObject]@{}
}
foreach ($VM in $VMSystems) {
$Result = @{}
Get-WmiObject -Namespace $RootNameSpace -Query "Associators of {$VM} Where AssocClass=Msvm_SystemDevice ResultClass=Msvm_KvpExchangeComponent" `
| Select-Object -ExpandProperty GuestIntrinsicExchangeItems `
| ForEach-Object {
$CimXML = [Xml]$_
$Result[$CimXML.SelectSingleNode("/INSTANCE/PROPERTY[@NAME='Name']").VALUE] = $CimXML.SelectSingleNode("/INSTANCE/PROPERTY[@NAME='Data']").VALUE
}
[PSCustomObject]$Result
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment