Question:
in this script i am pulling info from my SQL servers. i create the psobject and then i attempt to += each object into the $results table so that i can get a complete final report.
for some reason if i run the script all the way through it gives me the error
Method invocation failed because [system.management.managementobject] does not contain a method named ‘op_Addition’
But if i run the script line by line I do not get the error.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
$Results = @() $servers = get-content .\computers.txt ForEach ($server in $servers) { # Ping the machine to see if it's on the network $results = Get-WMIObject -query "select StatusCode from Win32_PingStatus where Address = '$server'" $responds = $false ForEach ($result in $results) { # If the machine responds break out of the result loop and indicate success if ($result.statuscode -eq 0) { $responds = $true break } } If ($responds) { # Gather info from the server because it responds Write-Output "$server responds" } else { # Let the user know we couldn't connect to the server Write-Output "$server does not respond" } # Check to see if a directory exists for this machine, if not create one if (!(Test-Path -path .\$server)) { New-Item .\$server\ -type directory } #get-WMI-Information function getwmiinfo ($srv) { # Get ComputerSystem info and write it to a CSV file gwmi -query "select * from Win32_ComputerSystem" -computername $srv | select Name, Model, Manufacturer, Description, DNSHostName, Domain, DomainRole, PartOfDomain, NumberOfProcessors, SystemType, TotalPhysicalMemory, UserName, Workgroup | export-csv -path .\$srv\BOX_ComputerSystem.csv -noType # Get OperatingSystem info and write it to a CSV file gwmi -query "select * from Win32_OperatingSystem" -computername $srv | select Name, Version, FreePhysicalMemory, OSLanguage, OSProductSuite, OSType, ServicePackMajorVersion, ServicePackMinorVersion | export-csv -path .\$server\BOX_OperatingSystem.csv -noType # Get PhysicalMemory info and write it to a CSV file gwmi -query "select * from Win32_PhysicalMemory" -computername $srv | select Name, Capacity, DeviceLocator, Tag | export-csv -path .\$srv\BOX_PhysicalMemory.csv -noType # Get LogicalDisk info and write it to a CSV file gwmi -query "select * from Win32_LogicalDisk where DriveType=3" -computername $srv | select Name, FreeSpace, Size | export-csv -path .\$srv\BOX_LogicalDisk.csv -noType } # Get the server info getwmiinfo $server #Get-SQLInfo [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null # Create an ADO.Net connection to the instance $cn = new-object system.data.SqlClient.SqlConnection( "Data Source=$server;Integrated Security=SSPI;Initial Catalog=master"); # Create an SMO connection to the instance $s = new-object ('Microsoft.SqlServer.Management.Smo.Server') $server # Set the CSV output file name and pipe the instances Information collection to it $outnm = ".\" + $server + "\" + $instnm + "GEN_Information.csv" $s.Information | export-csv -path $outnm -noType # Set ShowAdvancedOptions ON for the query $s.Configuration.ShowAdvancedOptions.ConfigValue = 1 $s.Configuration.Alter() # Create a DataSet for our configuration information $ds = new-object "System.Data.DataSet" "dsConfigData" # Build our query to get configuration, session and lock info, and execute it $q = "exec sp_configure; " $q = $q + "exec sp_who; " $q = $q + "exec sp_lock; " $da = new-object "System.Data.SqlClient.SqlDataAdapter" ($q, $cn) $da.Fill($ds) # Build datatables for the config data, load them from the query results, and write them to CSV files $dtConfig = new-object "System.Data.DataTable" "dtConfigData" $dtWho = new-object "System.Data.DataTable" "dtWhoData" $dtLock = new-object "System.Data.DataTable" "dtLockData" $dtConfig = $ds.Tables[0] $dtWho = $ds.Tables[1] $dtLock = $ds.Tables[2] $outnm = ".\" + $server + "\" + $instnm + "GEN_Configure.csv" $dtConfig | select name, minimum, maximum, config_value, run_value | export-csv -path $outnm -noType $outnm = ".\" + $server + "\" + $instnm + "GEN_Who.csv" $dtWho | select spid, ecid, status, loginame, hostname, blk, dbname, cmd, request_id | export-csv -path $outnm -noType $outnm = ".\" + $server + "\" + $instnm + "GEN_Lock.csv" $dtLock | select spid, dbid, ObjId, IndId, Type,Resource, Mode, Status | export-csv -path $outnm -noType # Set ShowAdvancedOptions OFF now that we're done with Config $s.Configuration.ShowAdvancedOptions.ConfigValue = 0 $s.Configuration.Alter() # Write the login name and default database for Logins to a CSV file $outnm = ".\" + $server + "\" + $instnm + "GEN_Logins.csv" $s.Logins | select Name, DefaultDatabase | export-csv -path $outnm -noType # Write information about the databases to a CSV file $outnm = ".\" + $server + "\" + $instnm + "GEN_Databases.csv" $dbs = $s.Databases $dbs | select Name, Collation, CompatibilityLevel, AutoShrink, RecoveryModel, Size, SpaceAvailable | export-csv -path $outnm -noType foreach ($db in $dbs) { # Write the information about the physical files used by the database to CSV files for each database $dbname = $db.Name if ($db.IsSystemObject) { $dbtype = "_SDB" } else { $dbtype = "_UDB" } # Write the user information to a CSV file $users = $db.Users $outnm = ".\" + $server + "\" + $instnm + $dbname + "_Users.csv" $users | select $dbname, Name, Login, LoginType, UserType, CreateDate | export-csv -path $outnm -noType $fgs = $db.FileGroups foreach ($fg in $fgs) { $files = $fg.Files $outnm = ".\" + $server + "\" + $instnm + $dbname + "_DataFiles.csv" $files | select $db.Name, Name, FileName, Size, UsedSpace | export-csv -path $outnm -noType } $logs = $db.LogFiles $outnm = ".\" + $server + "\" + $instnm + $dbname + "_LogFiles.csv" $logs | select $db.Name, Name, FileName, Size, UsedSpace | export-csv -path $outnm -noType } # Create CSV files for each ErrorLog file $outnm = ".\" + $server + "\" + $instnm + "_ERL_ErrorLog_.csv" $s.ReadErrorLog() | export-csv -path $outnm -noType $outnm = ".\" + $server + "\" + $instnm + "_ERL_ErrorLog_1.csv" $s.ReadErrorLog(1) | export-csv -path $outnm -noType $outnm = ".\" + $server + "\" + $instnm + "_ERL_ErrorLog_2.csv" $s.ReadErrorLog(2) | export-csv -path $outnm -noType $outnm = ".\" + $server + "\" + $instnm + "_ERL_ErrorLog_3.csv" $s.ReadErrorLog(3) | export-csv -path $outnm -noType $outnm = ".\" + $server + "\" + $instnm + "_ERL_ErrorLog_4.csv" $s.ReadErrorLog(4) | export-csv -path $outnm -noType $outnm = ".\" + $server + "\" + $instnm + "_ERL_ErrorLog_5.csv" $s.ReadErrorLog(5) | export-csv -path $outnm -noType $outnm = ".\" + $server + "\" + $instnm + "_ERL_ErrorLog_6.csv" $s.ReadErrorLog(6) | export-csv -path $outnm -noType $outnm = ".\" + $server + "\" + $instnm + "Gen_Information.csv" $Gen_Information = Import-Csv $outnm $outnm = ".\" + $server + "\" + $instnm + "Gen_Databases.csv" $Gen_Databases = Import-Csv $outnm $outnm = ".\" + $server + "\" + $instnm + "Box_ComputerSystem.csv" $Box_ComputerSystem = Import-Csv $outnm $outnm = ".\" + $server + "\" + $instnm + "Box_OperatingSystem.csv" $Box_OperatingSystem = Import-Csv $outnm $Gen_Info = $Gen_Information[0] $Gen_DBs = ForEach($object in $Gen_Databases){$object.Name} $Box_OS = $Box_OperatingSystem[0] $Box_CS = $Box_ComputerSystem[0] $Box_OSOS = $box_os.Name.Split("|") #region Correct SQL Version Name #correct Version Name if($Gen_Info.VersionString -like "8.*.*.*") { $Gen_InfoVers = "SQL Server 2000" } elseif($Gen_Info.VersionString -like "12.*.*.*") { $Gen_InfoVers = "SQL Server 2014" } elseif($Gen_Info.VersionString -like "10.*.*.*" -and $Gen_info.VersionString -notlike "10.5*.*.*") { $Gen_InfoVers = "SQL Server 2008" } elseif($Gen_Info.VersionString -like "9.*.*.*") { $Gen_InfoVers = "SQL Server 2005" } elseif($Gen_Info.VersionString -like "10.5*.*.*") { $Gen_InfoVers = "SQL Server 2008r2" } elseif($Gen_Info.VersionString -like "11.*.*.*") { $Gen_InfoVers = "SQL Server 2012" } elseif($Gen_Info.VersionString -like "13.*.*.*") { $Gen_InfoVers = "SQL Server 2016" } else{$Gen_InfoVers = "Unknown Version"} #endregion $object = New-Object psobject -Property @{ Server = $Box_CS.Name OS = $box_osos[0] Edition = $Gen_Info.EngineEdition SQLVersion = $Gen_InfoVers.ToString() TotalDBs = $Gen_DBs.count DatabaseName1 = $Gen_DBs[0] DatabaseName2 = $Gen_DBs[1] DatabaseName3 = $Gen_DBs[2] DatabaseName4 = $Gen_DBs[3] DatabaseName5 = $Gen_DBs[4] DatabaseName6 = $Gen_DBs[5] DatabaseName7 = $Gen_DBs[6] DatabaseName8 = $Gen_DBs[7] DatabaseName9 = $Gen_DBs[8] DatabaseName10 = $Gen_DBs[9] DatabaseName11 = $Gen_DBs[10] DatabaseName12 = $Gen_DBs[11] DatabaseName13 = $Gen_DBs[12] DatabaseName14 = $Gen_DBs[13] DatabaseName15 = $Gen_DBs[14] DatabaseName16 = $Gen_DBs[15] DatabaseName17 = $Gen_DBs[16] DatabaseName18 = $Gen_DBs[17] DatabaseName19 = $Gen_DBs[18] DatabaseName20 = $Gen_DBs[19] DatabaseName21 = $Gen_DBs[20] DatabaseName22 = $Gen_DBs[21] DatabaseName23 = $Gen_DBs[22] DatabaseName24 = $Gen_DBs[23] DatabaseName25 = $Gen_DBs[24] DatabaseName26 = $Gen_DBs[25] } $object = $object | select Server,OS,SQLVersion,Edition,TotalDBs,DatabaseName1,DatabaseName2,DatabaseName3,DatabaseName4,DatabaseName5,DatabaseName6,DatabaseName7,DatabaseName8,DatabaseName9,DatabaseName10,DatabaseName11,DatabaseName12,DatabaseName13,DatabaseName14,DatabaseName15, DatabaseName16,DatabaseName17,DatabaseName18,DatabaseName19,DatabaseName20,DatabaseName21,DatabaseName22,DatabaseName23,DatabaseName24,DatabaseName25,DatabaseName26 $object | Export-Csv ".\$server\FinalReport.csv" -NoTypeInformation -Force $Results += $object } $Results | Export-Csv ".\Final\FinalReport.csv" -NoTypeInformation -Force |
Answer:
@alroc has a valid point, that’s a lot of code. You can try typing $Result
as an [array]
.
change
1 2 |
$Results += $object |
to
1 2 |
[array]$Results += $object |