Skip to content

Instantly share code, notes, and snippets.

@janegilring
Created September 11, 2016 07:16
Show Gist options
  • Save janegilring/afec8213d3d14e4f436d0f9d88804f74 to your computer and use it in GitHub Desktop.
Save janegilring/afec8213d3d14e4f436d0f9d88804f74 to your computer and use it in GitHub Desktop.
Get-DPMAgentInfo
#requires -Version 3.0
function Get-DPMAgentInfo
{
[CmdletBinding()]
param (
[Parameter(Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[string[]] $ComputerName = 'localhost'
)
try
{
$session = New-PSSession -ComputerName $ComputerName -ErrorAction Stop
$output = New-Object -TypeName pscustomobject -Property @{
ComputerName = $session.ComputerName
Connection = 'Success'
IsInstalled = $null
Version = $null
ActiveOwner = $null
DPMServer = $null
}
}
catch
{
$output = New-Object -TypeName pscustomobject -Property @{
ComputerName = $env:ComputerName
Connection = 'Success'
IsInstalled = $null
Version = $null
ActiveOwner = $null
DPMServer = $null
}
}
if ($session)
{
$DPMAgentIsInstalled = Invoke-Command -Session $session -ScriptBlock {
Test-Path -Path 'HKLM:\SOFTWARE\Microsoft\Microsoft Data Protection Manager'
}
if ($DPMAgentIsInstalled)
{
$output.IsInstalled = $DPMAgentIsInstalled
$ActiveOwnerInfo = Invoke-Command -Session $session -ScriptBlock {
try
{
$Path = (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Microsoft Data Protection Manager\Setup' -ErrorAction Stop).InstallPath
}
catch
{
Write-Verbose -Message "DPM not installed on $($env:ComputerName)"
break
}
$ActiveOwnerFilePaths = Get-ChildItem -Path "$Path\ActiveOwner" | Select-Object -ExpandProperty FullName
foreach ($thisPath in $ActiveOwnerFilePaths)
{
$fileStream = New-Object -TypeName System.IO.FileStream -ArgumentList ($thisPath, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read)
$fileReader = New-Object -TypeName System.IO.BinaryReader -ArgumentList $fileStream
$outputString = ''
$continue = $false
do
{
$readBytes = $fileReader.ReadBytes(2)
$unicodeString = [System.Text.Encoding]::Unicode.GetString($readBytes)
if ($unicodeString -eq '')
{
$continue = $true
}
else
{
$outputString += $unicodeString
}
}
until ($continue)
[pscustomobject]@{
ActiveOwnerFile = (Split-Path -Path $thisPath -Leaf)
ActiveOwnerFileLastWriteTime = (Get-Item $thisPath).LastWriteTime
DPMServerName = $outputString
}
}
$fileReader.Dispose()
$fileStream.Dispose()
} | Select-Object -Property DPMServerName, ActiveOwnerFile, ActiveOwnerFileLastWriteTime
if ($ActiveOwnerInfo)
{
$output.ActiveOwner = $ActiveOwnerInfo
$output.DPMServer = ($ActiveOwnerInfo | Select-Object -Property DPMServerName -Unique).DPMServerName -join ','
}
$DPMVersionInfo = Invoke-Command -Session $session -ScriptBlock {
$DPMRAPath = Join-Path -Path $Path -ChildPath bin\DPMRA.exe
(Get-Item -Path $DPMRAPath).VersionInfo.FileVersion
}
if ($DPMVersionInfo)
{
$output.Version = $DPMVersionInfo
}
}
Remove-PSSession -Session $session
}
$output | Select-Object -Property ComputerName, Connection, IsInstalled, Version, DPMServer, ActiveOwner
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment