My Script is below. It seems to work but I don't get an xml file at the end???
#
//***************************************************************************
# // ***** Script Header *****
# //
# // File: Get-PNPInfo.ps1
# //
# // Purpose: Given a list of Computers will query for PNP Device IDS
# //
# // Usage: powershell .\Get-PNPInfo.ps1 [collection of computers]
# //
# // Notes:
#
//***************************************************************************
Param
(
[array]$computers =$(throw "You must specify an array of computers")
)
$ErrorActionPreference="SilentlyContinue"
for ($i=0;$i -le $computers.length; $i++) {
[int]$progress = ($i * 100) / $computers.Length
Write-Progress -activity "Querying Remote PNP Information" -Status
"Currently Querying $($computers[$i])" -perc $progress
#Attempt to ping the machine
Write-Progress -activity "Details for $($computers[$i])" -id 1 -status
"Trying to ping workstation." -perc 16
$response = Get-WmiObject -query "Select * From Win32_PingStatus Where
Address='$($computers[$i])'"
if( ($response -eq $null) -or ($response.StatusCode -ne 0)) {
Write-Progress -activity "Details for $($computers[$i])" -id 1 -status
"Could not ping workstation" -perc 100
} else {
Write-Progress -activity "Details for $($computers[$i])" -id 1 -status
"Getting remote WMI and Registry" -perc 32
$PNPEntity = Get-WmiObject "Win32_PnPEntity" -ComputerName $computers[$i]
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',
$computer)
#if $reg or wmi action action failed then don't continue.
if ($?)
{
#Create the XML File
$XMLFile = [xml]'<?xml version="1.0"
encoding="utf-8"?><SystemAudit><Devices></Devices></SystemAudit>'
$XMLDevices = $XMLFile.PSBase.SelectSingleNode("//Devices")
foreach ( $device in $PNPEntity) {
$deviceID = $device.PNPDeviceID
Write-Progress -activity "Details for $($computers[$i])" -id 1
-status "Found Key: $deviceID" -perc 48
#create a new entry in the xml file.
$XMLDevice = $XMLFile.CreateElement("Device")
$id = $XMLFile.CreateAttribute("Id")
$id.psbase.Value = $deviceID
$return = $XMLDevice.SetAttributeNode($id)
Write-Progress -activity "Details for $($computers[$i])" -id 1
-status "Getting hardware IDs" -perc 64
#Get HardwareIds and Compatible IDS
#trap{continue}
$devEnum = $reg.OpenSubKey("SYSTEM\CurrentControlSet\Enum\$deviceID")
$hwids = $devEnum.GetValue("HardwareID")
$compatIDs = $devEnum.GetValue("CompatibleIDs")
#List HardwareIDs
if ( -not ($hwids -eq $null) ) {
foreach ($hwid in $hwids) {
$XMLHwid = $XMLFile.CreateElement("HwId")
$attribute = $XMLFile.CreateAttribute("Id")
$attribute.psbase.Value = $hwid
$return = $XMLHwid.SetAttributeNode($attribute)
$return = $XMLDevice.AppendChild($XMLHwid)
}
}
Write-Progress -activity "Details for $($computers[$i])" -id 1
-status "Getting Driver Information" -perc 80
#Get Driver Information
$driver =
$reg.OpenSubKey("SYSTEM\CurrentControlSet\Control\Class\$($devEnum.GetValue('Driver'))")
#XML Element and Attributes for Driver Information
$installedDriver = $XMLFile.CreateElement("InstalledDriver")
$MatchingDeviceId = $XMLFile.CreateAttribute("MatchingDeviceId")
$MatchingDeviceId.psbase.Value = $driver.GetValue("MatchingDeviceId")
$return = $installedDriver.SetAttributeNode($MatchingDeviceId)
$DriverDesc = $XMLFile.CreateAttribute("DriverDesc")
$DriverDesc.psbase.Value = $driver.GetValue("DriverDesc")
$return = $installedDriver.SetAttributeNode($DriverDesc)
$DriverVerDate = $XMLFile.CreateAttribute("DriverVerDate")
$DriverVerDate.psbase.Value = $driver.GetValue("DriverDate")
$return = $installedDriver.SetAttributeNode($DriverVerDate)
$DriverVerVersion =$XMLFile.CreateAttribute("DriverVerVersion")
$DriverVerVersion.psbase.Value = $driver.GetValue("DriverVersion")
$return = $installedDriver.SetAttributeNode($DriverVerVersion)
$Class = $XMLFile.CreateAttribute("Class")
$Class.psbase.Value = $devEnum.GetValue("Class")
$return = $installedDriver.SetAttributeNode($Class)
$Manufacturer = $XMLFile.CreateAttribute("Manufacturer")
$Manufacturer.psbase.Value = $devEnum.GetValue("Mfg")
$return = $installedDriver.SetAttributeNode($Manufacturer)
$Provider = $XMLFile.CreateAttribute("Provider")
$Provider.psbase.Value = $driver.GetValue("ProviderName")
$return = $installedDriver.SetAttributeNode($Provider)
$Model = $XMLFile.CreateAttribute("Model")
$Model.psbase.Value = $driver.GetValue("DriverDesc")
$return = $installedDriver.SetAttributeNode($Model)
#Add the installedDrivers to the Device Node
$return = $XMLDevice.AppendChild($installedDriver)
#add this device to the XML File
$return = $XMLDevices.AppendChild($XMLDevice)
}
Write-Progress -activity "Details for $($computers[$i])" -id 1 -status
"Writing XML File" -perc 100
$XMLFile.Save("output.xml")
}
}
}
--
**********************
Jacob
"Jeff" wrote:
> On Oct 2, 9:46 am, Jacob <jacob(AT)hfws.net.nospam> wrote:
> > Hi Jeff,
> > Thanks for your reply.
> >
> > I'm still struggling with this. I have it sort of working for Get-WmiObject
> > but when i run this line:
> > $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',
> > $computer)
> >
> > It just outputs the red text.
> >
> > In VBScript I've always had a logging class that I used to make log file of
> > errors when i script is running.
> >
> > Is there a way to do this in powershell. Intercept the error, Don't display
> > it to the console, and write it to a log file.
> >
> > Sure there is a powershell equivalent to:
> > If Err Then
> > LogEntrySub "Something went wrong."
> > Exit Function
> > End If
> >
> > Thanks for your help.
> > >
> One more thing that might get you closer to what you want with your
> last example:
>
> & {
> $ErrorActionPreference = "SilentlyContinue"
>
> trap
> {
> "Error!"
> # log your error, exit, or whatever
> }
>
> $hive = "LocalMachine"
> $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($hive, `
> $computer)
> }
>
> OpenRemoteBaseKey does throw an exception, so you can use a trap
> statement here. I am setting the $ErrorActionPreference variable in a
> scriptblock so its old value will be restored outside of the
> scriptblock's scope.
>
> Good luck.
>
> Jeff
>
>