Powershell Script Error

The following code gives me an error when querying:

Connecting to ...Querying information for ...
psinfo.exe :
At C:\Scripts\SoftwareAudit.ps1:19 char:50
+    $psinfoOutput = C:\WINDOWS\system32\psinfo.exe <<<<  -s Applications \\$computer
    + CategoryInfo          : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
PsInfo v1.77 - Local and remote system information viewer
Copyright (C) 2001-2009 Mark Russinovich
Sysinternals - www.sysinternals.com


Connecting to ...Querying information for ...
Loading-:3ivx MPEG-4 5.0.2 (remove only) 5.0.2
Loading-:Mitel 5000 DB Programming AVDAP Monitor
Loading-:Security Update for Microsoft Office Visio Viewer 2007 (KB973709)
Loading-:Windows Live Essentials 14.0.8089.0726
Loading-:Security Update for Windows XP (KB981997) 1
Loading-:Mitel 5000 DB Programming DB Test
Loading-:Intel(R) Graphics Media Accelerator Driver 6.14.10.4410
Loading-:Windows Live Essentials 14.0.8089.726
Loading-:Security Update for Microsoft Office system 2007 (972581)
Loading-:Hotfix for Windows XP (KB979306) 1
Loading-:WatchGuard Mobile VPN with SSL client 11.0
Loading-:Adobe Acrobat  8 Standard - English, Frantais, Deutsch 8.2.4
Loading-:Microsoft Office Professional Hybrid 2007 12.0.6425.1000
Loading-:Hotfix for Microsoft .NET Framework 3.5 SP1 (KB953595) 1
Loading-:Microsoft Visual C++ 2008 Redistributable - x86 9.0.30729.17 9.0.30729
Loading-:Security Update for Windows XP (KB969947) 1
Loading-:Security Update for Windows Internet Explorer 7 (KB982381) 1
Loading-:Microsoft Silverlight 4.0.50524.0
Loading-:Security Update for Windows XP (KB941569)
Loading-:Security Update for Windows Internet Explorer 7 (KB938127-v2) 2
Loading-:BlackBerry Device Software Updater 5.0.1.65
Loading-:Security Update for Windows XP (KB975713) 1
Loading-:Windows XP Service Pack 3 20080414.031525
Loading-:Microsoft User-Mode Driver Framework Feature Pack 1.0
Loading-:Security Update for Windows XP (KB946648) 1
Loading-:Broadcom Gigabit Integrated Controller 8.10.07
Loading-:Mitel 5000 DB Programming Remote Configuration
Loading-:SoundMAX 5.12.01.5246
Loading-:Microsoft English TTS Engine 2.0.1000.0
Loading-:Security Update for Windows XP (KB958869) 1
Loading-:Security Update for Windows Media Player (KB954155)
Loading-:Microsoft Search Enhancement Pack 3.0.126.0
Loading-:Security Update for Windows XP (KB960803) 1
Loading-:Security Update for Windows Media Player (KB968816)
Loading-:BlackBerry Desktop Software 5.0.1 5.0.1.37
Loading-:Security Update for Windows Media Player (KB978695)
Loading-:Java(TM) 6 Update 21 6.0.210
Loading-:Security Update for Windows XP (KB978601) 1
Loading-:MSN Toolbar Platform 4.0.0379.0
Loading-:Update for Windows XP (KB980182) 1
Loading-:Microsoft Office Proof (French) 2007 12.0.6425.1000
Loading-:Security Update for Windows XP (KB975560) 1
Loading-:Microsoft Office Proof (Spanish) 2007 12.0.6425.1000
Loading-:Security Update for Windows XP (KB956744) 1
Loading-:Security Update for Windows XP (KB975561) 1
Loading-:Security Update for Windows XP (KB950762) 1
Loading-:Mobile Broadband Generic Drivers 2.02.07.002.14
Loading-:Windows Media Format 11 runtime
Loading-:Microsoft .NET Framework 2.0 Service Pack 2 2.2.30729
Loading-:Microsoft Office Outlook MUI (English) 2007 12.0.6425.1000
Loading-:FreeFileSync v3.6
Loading-:Security Update for Microsoft Office Word 2007 (KB2251419)
Loading-:Microsoft UI Engine 4.0.0318.1
Loading-:Security Update for Windows XP (KB978542) 1
Loading-:Hotfix for Windows XP (KB981793) 1
Loading-:Microsoft Office Proof (English) 2007 12.0.6425.1000
Loading-:Security Update for Windows XP (KB974392) 1
Loading-:Security Update for Windows XP (KB954459) 1
Loading-:Security Update for Windows XP (KB960859) 1
Loading-:Security Update for Windows Internet Explorer 7 (KB2183461) 1
Loading-:Security Update for Windows XP (KB974112) 1
Loading-:Security Update for CAPICOM (KB931906) 2.1.0.2
Loading-:EMCO Remote Installer Starter
Loading-:Nmap 4.76
Loading-:Security Update for Windows XP (KB956802) 1
Loading-:Easy CD & DVD Creator 6 6.0.0.171
Loading-:Microsoft Choice Guard 2.0.48.0
Loading-:Update for Windows XP (KB971737) 1
Loading-:Security Update for Windows XP (KB950974) 1
Loading-:Microsoft Compression Client Pack 1.0 for Windows XP 1
Loading-:Security Update for Windows XP (KB958644) 1
Loading-:Hotfix for Microsoft .NET Framework 3.5 SP1 (KB958484) 1
Loading-:Security Update for Windows XP (KB951376-v2) 2
Loading-:Microsoft Office Visio Standard 2007 12.0.6425.1000
Loading-:Security Update for Windows XP (KB923789)
Loading-:Security Update for Windows XP (KB978262) 1
Loading-:Segoe UI 14.0.4327.805
Loading-:Security Update for Windows XP (KB971657) 1
Loading-:Microsoft Office PowerPoint MUI (English) 2007 12.0.6425.1000
Loading-:Microsoft Office Visio 2007 Service Pack 2 (SP2)
Loading-:Security Update for Windows XP (KB977165-v2) 2
Loading-:Windows Driver Package - Inter-Tel Incorporated TAP-Win32 Adapter 5000m (09/13/2006 9.00.00.0004) 09/13/2006 9.00.00.0004
Loading-:Mitel 5000 DB Programming MOH Converter Utility
Loading-:Microsoft Office Access Setup Metadata MUI (English) 2007 12.0.6425.1000
Loading-:Update for Microsoft Office Powerpoint 2007 Help (KB963669)
Loading-:Security Update for Windows XP (KB969059) 1
Loading-:Security Update for Windows Search 4 - KB963093
Loading-:Microsoft .NET Framework 3.0 Service Pack 2 3.2.30729
Loading-:Windows Live ID Sign-in Assistant 6.500.3165.0
Loading-:MSN Toolbar 4.0.0379.0
Loading-:Windows Live Messenger 14.0.8089.0726
Loading-:UberIcon 1.0.4
Loading-:Update for Windows XP (KB968389) 1
Loading-:Adobe Flash Player 10 ActiveX 10.1.53.64
Loading-:IIS 6.0 Resource Kit Tools 6.00.0000
Loading-:Security Update for 2007 Microsoft Office System (KB982331)
Loading-:Security Update for Windows XP (KB970238) 1
Loading-:Windows Genuine Advantage Validation Tool (KB892130)
Loading-:Security Update for Windows XP (KB979309) 1
Loading-:LogMeIn 4.0.982
Loading-:Command WorkStation 5.1.1.04 5.1.1.04
Loading-:Security Update for Windows XP (KB977816) 1
Loading-:Update for Microsoft Office Word 2007 Help (KB963665)
Loading-:Update for Microsoft .NET Framework 3.5 SP1 (KB963707) 1
Loading-:Security Update for Windows XP (KB982665) 1
Loading-:Security Update for Windows XP (KB2286198) 1
Loading-:Security Update for Microsoft Office PowerPoint 2007 (KB982158)
Loading-:Security Update for Windows XP (KB977914) 1
Loading-:Microsoft Software Update for Web Folders  (English) 12 12.0.6425.1000
Loading-:Update for Windows XP (KB961503) 1
Loading-:Adobe Acrobat 8.2.4 Standard 8.2.4
Loading-:Security Update for Windows XP (KB970430) 1
Loading-:MSVCRT 14.0.1468.721
Loading-:Client 9.00.000
Loading-:Security Update for Windows XP (KB975467) 1
Loading-:Security Update for Windows XP (KB960225) 1
Loading-:Security Update for Windows XP (KB973507) 1
Loading-:Security Update for Windows XP (KB951066) 1
Loading-:Microsoft Office 2007 Service Pack 2 (SP2)
Loading-:BlackBerry« Media Sync 3.0.0.39
Loading-:Update for Windows XP (KB943729)
Loading-:Security Update for Windows XP (KB981349) 1
Loading-:Security Update for Windows XP (KB974318) 1
Loading-:Security Update for Windows XP (KB952004) 1
Loading-:Microsoft SQL Server 2008 R2 Report Builder 3.0 10.50.1352.12
Loading-:Windows Management Framework Core
Loading-:Update for Microsoft Office Excel 2007 Help (KB963678)
Loading-:LMS v9.0 9.00.000
Loading-:Adobe Flash Player 10 Plugin 10.1.53.64
Loading-:Security Update for Microsoft Office system 2007 (KB974234)
Loading-:Security Update for Windows XP (KB980436) 1
Loading-:Security Update for Windows XP (KB956572) 1
Loading-:Security Update for Windows XP (KB978338) 1
Loading-:Microsoft Visual C++ 2005 Redistributable 8.0.59193
Loading-:WebFldrs XP 9.50.7523
Loading-:OGA Notifier 2.0.0048.0 2.0.0048.0
Loading-:Security Update for Windows XP (KB981852) 1
Loading-:Microsoft Office Proofing Tools 2007 Service Pack 2 (SP2)
Loading-:Mozilla Firefox (3.6.6) 3.6.6 (en-US)
Loading-:Security Update for Windows XP (KB956844) 1
Loading-:Windows Resource Kit Tools 5.2.3790
Loading-:Hotfix for Windows XP (KB915800-v4) 4
Loading-:BlackBerry Device Software v4.5.0 for the BlackBerry 8330 smartphone 4.5.0.175 (Platform 3.4.0.55)
Loading-:Compatibility Pack for the 2007 Office system 12.0.6514.5001
Loading-:VZAccess Manager for Novatel 6.9.8
Loading-:Update for Microsoft Office 2007 Help for Common Features (KB963673)
Loading-:Microsoft Office Publisher MUI (English) 2007 12.0.6425.1000
Loading-:Collaboration Client 2.0 4.4.3.0
Loading-:Security Update for Windows XP (KB971468) 1
Loading-:Trend Micro Client/Server Security Agent 16.0.1331
Loading-:CCleaner 2.30
Loading-:Security Update for Microsoft Office Excel 2007 (KB982308)
Loading-:PHP 5.3.2 5.3.2
Loading-:Security Update for Windows XP (KB979683) 1
Loading-:Microsoft Default Manager 2.1.54.0
Loading-:Update for Microsoft Office Outlook 2007 Help (KB963677)
Loading-:Security Update for Windows XP (KB2160329) 1
Loading-:Windows Internet Explorer 7 20070813.185237
Loading-:Update for Windows XP (KB955759) 1
Loading-:Security Update for Windows XP (KB982214) 1
Loading-:Microsoft Visual C++ 2005 Redistributable 8.0.56336
Loading-:Security Update for Windows Media Player (KB973540)
Loading-:Microsoft .NET Framework 1.1 1.1.4322
Loading-:winpcap-nmap 4.02
Loading-:Windows Live Call 14.0.8064.0206
Loading-:Microsoft Office Word MUI (English) 2007 12.0.6425.1000
Loading-:Windows Genuine Advantage Validation Tool (KB892130) 1.7.0069.2
Loading-:Security Update for Microsoft Office Access 2007 (KB979440)
Loading-:Microsoft Office Visio MUI (English) 2007 12.0.6425.1000
Loading-:Security Update for Windows XP (KB961501) 1
Loading-:Microsoft Visual C++ 2008 Redistributable - x86 9.0.21022 9.0.21022
Loading-:Security Update for Windows XP (KB980232) 1
Loading-:Microsoft .NET Framework 3.5 SP1
Loading-:Hotfix for Windows XP (KB932716-v2) 2
Loading-:Hotfix for Windows XP (KB954550-v5) 5
Loading-:Security Update for Windows XP (KB978037) 1
Loading-:Security Update for Windows XP (KB974571) 1
Loading-:Security Update for Windows XP (KB2079403) 1
Loading-:Microsoft .NET Framework 3.5 SP1 3.5.30729
Loading-:Microsoft Office Access MUI (English) 2007 12.0.6425.1000
Loading-:Microsoft .NET Framework 1.1
Loading-:Microsoft Office Shared Setup Metadata MUI (English) 2007 12.0.6425.1000
Loading-:Windows Server 2003 Administration Tools Pack 5.2.3790
Loading-:Security Update for 2007 Microsoft Office System (KB976321)
Loading-:Update for Windows XP (KB973815) 1
Loading-:Hotfix for Windows Media Format 11 SDK (KB929399)
Loading-:Update for Microsoft Office Access 2007 Help (KB963663)
Loading-:Security Update for Windows XP (KB971961) 1
Loading-:Security Update for Windows XP (KB980218) 1
Loading-:Update for 2007 Microsoft Office System (KB967642)
Loading-:2007 Microsoft Office system 12.0.6425.1000
Loading-:Security Update for Windows XP (KB973869) 1
Loading-:Update for Microsoft Windows (KB971513)
Loading-:Windows Media Player 11
Loading-:Microsoft Internationalized Domain Names Mitigation APIs
Loading-:Security Update for Windows XP (KB955069) 1
Loading-:Microsoft .NET Framework 1.1 Security Update (KB979906)
Loading-:Security Update for Windows XP (KB980195) 1
Loading-:Security Update for Windows XP (KB979559) 1
Loading-:Mitel 5000 DB Programming Diagnostics Monitor
Loading-:Security Update for 2007 Microsoft Office System (KB969559)
Loading-:SAPI Wrapper 1.0.0.0
Loading-:Security Update for Windows XP (KB959426) 1
Loading-:Microsoft Office Excel MUI (English) 2007 12.0.6425.1000
Loading-:Update for Windows XP (KB967715) 1
Loading-:MSXML 6.0 Parser (KB927977) 6.00.3890.0
Loading-:Microsoft Office Shared MUI (English) 2007 12.0.6425.1000
Loading-:Security Update for Windows XP (KB923561) 1
Loading-:Mitel 5000 DB Programming 3.2.4.54
Loading-:Update for Microsoft Office Script Editor Help (KB963671)
Loading-:Update for Windows Internet Explorer 7 (KB980182) 1
Loading-:Security Update for Microsoft Office Outlook 2007 (KB980376)
Loading-:Security Update for Windows XP (KB972270) 1
Loading-:Security Update for Windows XP (KB975025) 1
Loading-:Adobe Acrobat 8.2.4 - CPSID_83708
Loading-:Security Update for Windows XP (KB978706) 1
Loading-:Security Update for Microsoft Office InfoPath 2007 (KB979441)
Loading-:Update for Outlook 2007 Junk Email Filter (kb2279264)
Loading-:Update for Windows XP (KB973687) 1
Loading-:Windows Live Communications Platform 14.0.8098.930
Loading-:Security Update for Windows XP (KB973904) 1
Loading-:Security Update for Windows XP (KB975562) 1
Loading-:Update for Windows XP (KB951978) 1
Loading-:Java Auto Updater 2.0.2.4
Loading-:Security Update for Windows XP (KB978251) 1
Loading-:Security Update for 2007 Microsoft Office System (KB982312)
Loading-:Security Update for Windows Media Player 11 (KB954154)
Loading-:Microsoft Virtual PC 2007 6.0.156.0
Loading-:Security Update for Windows XP (KB973354) 1
Loading-:Security Update for Windows XP (KB976323) 1
Loading-:Microsoft Base Smart Card Cryptographic Service Provider Package
Loading-:Security Update for Microsoft Office Publisher 2007 (KB982124)
Loading-:Security Update for 2007 Microsoft Office System (KB2277947)
Loading-:Hotfix for Windows XP (KB961118) 1
Loading-:Update for Microsoft Office Publisher 2007 Help (KB963667)
Loading-:Security Update for Windows XP (KB979482) 1
Loading-:Security Update for Windows XP (KB956803) 1
Loading-:Security Update for Microsoft Office Visio 2007 (KB982127)
Loading-:Microsoft RichCopy 4.0 4.0.216
Loading-:Security Update for Windows XP (KB950760) 1
Loading-:Mitel 5000 DB Programming Upload Utility
Loading-:Microsoft Application Error Reporting 12.0.6012.5000
Loading-:Security Update for Windows XP (KB2115168) 1
Loading-:Microsoft National Language Support Downlevel APIs
Loading-:Hotfix for Windows XP (KB952287) 1
Loading-:Security Update for Windows XP (KB952954) 1
Loading-:Security Update for Windows Media Player (KB952069)
Loading-:Microsoft Office Proofing (English) 2007 12.0.4518.1014
Loading-:Security Update for Windows XP (KB2229593) 1
Loading-:Security Update for Windows XP (KB951748) 1
Loading-:Windows Genuine Advantage Notifications (KB905474) 1.9.0040.0
Loading-:VNC Free Edition 4.1.2 4.1.2
Loading-:Windows Search 4.0 04.00.6001.503
Loading-:Hotfix for Windows Media Player 11 (KB939683)
Loading-:Microsoft Visual C++ 2008 ATL Update kb973924 - x86 9.0.30729.4148 9.0.30729.4148


Here is the code, it is pulled from an example and I am not sure I have it all define correctly (this is all the actual code minus the SERVERNAME:

# Open the database connection
$dbConn = new-object System.Data.SqlClient.SqlConnection "server=SERVERNAME;database=BGInfo;Integrated Security=True"
$dbConn.Open()
$sqlQuery = $dbConn.CreateCommand()

# Get all known computers
$sqlQuery.CommandText = "select distinct Host_Name from BGinfoTable"
$reader = $sqlQuery.ExecuteReader()
$computersToQuery = @()
while ($reader.Read()) {
   $computersToQuery += $reader["Computer"]
}

# Close the database connection
$dbConn.Close()

$softwareInventory = @{}
foreach ($computer in $computersToQuery) {
   $psinfoOutput = C:\WINDOWS\system32\psinfo.exe -s Applications \\$computer

   $foundSoftwareInventory = 0
   $computerName = ""
   foreach ($item in $psinfoOutput) {
      if ($foundSoftwareInventory -eq 1) {
         # Force the results to a string
         # Remove any single quotes which interfere with T-SQL statements
         # Load the result into a hash whereby removing any duplicates
         [string]$softwareInventory[$computerName][$item.Replace("'","")] = ""
      }

      if ($item -like "System information for *") {
         $computerName = $item.Split("\")[2].TrimEnd(":")
      } elseif ($item -eq "Applications:") {
         $foundSoftwareInventory = 1
         $softwareInventory[$computerName] = @{}
      }
   }
}

$dbConn = new-object System.Data.SqlClient.SqlConnection "server=SERVERNAME;database=BGInfo;Integrated Security=True"
$dbConn.Open()
$sqlQuery = $dbConn.CreateCommand()

foreach ($computer in $softwareInventory.Keys) {
   foreach ($softwareItem in $softwareInventory[$computer].Keys) {
      "Loading-" + $computer + ":" + $softwareItem

      # Try an Insert than an Update
      trap {
         $sqlQuery.CommandText = "update SoftwareInventory set AuditDate = getdate() where  Computer = '" + $computer + "' and Software = '" + $softwareItem + "'"
         $result = $sqlQuery.ExecuteNonQuery()
         continue
      }

      $sqlQuery.CommandText = "insert into Inventory..SoftwareInventory (Computer,Software,AuditDate) values ('" + $computer + "','" + $softwareItem + "',getdate())"
      $result = $sqlQuery.ExecuteNonQuery()
   }
}

$dbConn.Close()
Sleestack90Asked:
Who is Participating?
 
wmehardtCommented:
I see that same error when $computer is null. Check the output of $computerstoquery.
0
 
davdjevansCommented:
I'm not a programmer, but just having look over the code:
$computersToQuery = @() - This has got round brackets whereas
$softwareInventory = @{} - has curly brackets.
0
 
wmehardtCommented:
Is the formatting of the $computersToQuery OK? Do you maybe need to trim it?

Try doing something like adding this after closing the connection

$computersToQuery | % {Write-Host $_":"$($_.Length)}
break

to make sure things are looking as they should
0
 
Sleestack90Author Commented:
Ok so the line:

$computersToQuery += $reader["Computer"]

Needed to be switched to:

$computersToQuery += $reader["Host_Name"]

In order to pull this information from the correct database file.



Any ideas on this area:

At C:\Scripts\SoftwareAudit.ps1:19 char:50
+    $psinfoOutput = C:\WINDOWS\system32\psinfo.exe <<<<  -s Applications \\$computer
    + CategoryInfo          : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError



I'm not sure why it's giving me errors here.
0
 
Sleestack90Author Commented:
This is actaully all correct, just had to change the code to point to the correct database.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.