Skip to content

Instantly share code, notes, and snippets.

@stknohg
Last active November 30, 2017 01:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stknohg/0561fe2e6eeebe3d38bf232ad1948e37 to your computer and use it in GitHub Desktop.
Save stknohg/0561fe2e6eeebe3d38bf232ad1948e37 to your computer and use it in GitHub Desktop.
仮想マシンからデータ交換サービスのデータを取得する関数
<#
.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