Happy Sys. Adm: Multidimensional arrays in Powershell. Today I want to talk a little about multidimensional arrays in Windows Powershell. Multidimensional arrays are one of the complex data types supported by Powershell. They can be used to dynamically store information in a volatile table without having it written to a real database in a file on the disk. A web site about system administration tasks. Windows, Unix, SQL, VMware, Openview, Linux resources, technical articles, tips, tricks and solutions. In that sense multidimensional arrays extend a lot the functionalities of simple arrays, which are oriented to storing just series of polymorphic values such as: Strings: ‘Apples’, ‘Peaches’, ‘Oranges’, ‘Apricots’Integers: 1,2,3,4,5.. Sergio’,’Leone’,’January’,3,1. Rome’. In fact you can imagine a multidimensional array like a table, with columns and rows, where each cell has its own index (i. To make things clear, let’s set- up a multidimensional array and let’s see how it can be used, for instance, to manage our employees coming and going. First let’s do some cleaning on our screen and let’s define a $emp_counter variable which we can later use to retrieve information about employees: clear- host write- host "==========STARTED==============" $emp_counter = $null. Ok, the game commences here. · Any ideas on what might be going on here? I've been fighting this thing off and on all week and for the time being have removed WSUS from GPO and am. · Solution: KB3148812 was recently replaced with KB3159706. Same thing happens after install. WSUS service won't start with the same message as posted above. We define here the desired array as a dynamic array. We go for the following syntax. This will result in an empty array named $employee_list. The drawback of an empty array such this is that each time you will perform an operation on it, such as adding a row, or just adding a single value, Windows Powershell will have to rebuild (by making a copy) the whole array to follow its structural modifications. And of course this would have a high impact on the performance of your script if thousands or even millions of operations are performed. Nonetheless an array with no predefined size will give us a greater flexibility. That’s why we call it a dynamic array in the end, or a jagged array to be more precise. We could also have start by defining a fixed size array (which is called a true multidimensional array) but in our example we don’t want to stick to a square matrix so we will not use the following command (but you are free to do so if you want to deeper explore arrays in Powershell): $employee_list = New- Object 'object[,]' 4,5. Now let’s check how Powershell sees this array object: write- host "===============================" write- host "Checking array information" write- host "===============================" $employee_list. The output is: Is. Public Is. Serial Name Base. Type. - -- -- -- - - -- -- -- - - -- - - -- -- -- -. True True Object[] System. Array. Now let’s add four rows, one for each of our new IT administrators: Tommy, Jonathan, Mario and Jeremy.$emp_counter ++ $employee_list += ,@($emp_counter, 'Tommy',7. Sysadm','Windows') $emp_counter ++ $employee_list += ,@($emp_counter, 'Jonathan',8. Sysadm','Unix') $emp_counter ++ $employee_list += ,@($emp_counter, 'Mario',6. DBA','Oracle') $emp_counter ++ $employee_list += ,@($emp_counter, 'Jeremy',7. DBA','MS SQL'). Now let’s run through the array to see its content row by row: write- host "===============================" write- host "Array filled with four rows" write- host "===============================" foreach($employee in $employee_list) { Write- host ($employee) }. Here’s the output: 1 Tommy 7. Sysadm Windows. 2 Jonathan 8. Sysadm Unix. 3 Mario 6. DBA Oracle. 4 Jeremy 7. DBA MS SQL. Each time we put a comma, we are like telling Powershell to start a new row in the multidimensional array. Here’s a graphical representation. As you can see, for the moment this jagged array resembles a square matrix with four rows and five columns. The first column is our subjective index, the second is the name, the third is the salary, the fourth is the role, and the fifth is the specialization. This was the easy part. Now let’s have a look at the different operations that can be performed on this scaring polymorphic multidimensional array. In particular, let’s see how to: Add a row to the jagged array. Add a row with a non standard number of values. Delete a row from the jagged array. Sort the content of the array by one known column. Doing these operations will show you the power of Powershell and of its data types!!! Let’s start with adding a full grown- up row for our new networking specialist, Bill: $employee_list += ,@($emp_counter,'Bill',8. Network','Cisco'). Here’s what we have now: write- host "===============================" write- host "Bill is hired" write- host "===============================" foreach($employee in $employee_list) { Write- host ($employee) }. The output is: 1 Tommy 7. Sysadm Windows. 2 Jonathan 8. Sysadm Unix. 3 Mario 6. DBA Oracle. 4 Jeremy 7. DBA MS SQL. 5 Bill 8. Network Cisco. Or graphically. Now let’s see how to add a row with a non standard number of values. For instance, our new employee Cedric has skills in both Windows and NAS worlds, so we will have six columns instead of five like for the others employees. This will change our array from a true multidimensional array to a jagged array (for more details on the difference between jagged arrays and true multidimensional arrays check this cool post.). We would add Cedric to our array this way: $employee_list += ,@($emp_counter,'Cedric',7. Sysadm','Windows','Netapp'). We get: write- host "===============================" write- host "Cedric Is hired" write- host "===============================" foreach($employee in $employee_list) { Write- host ($employee) }. The output will be: 1 Tommy 7. Sysadm Windows. 2 Jonathan 8. Sysadm Unix. 3 Mario 6. DBA Oracle. 4 Jeremy 7. DBA MS SQL. 5 Bill 8. Network Cisco. 6 Cedric 7. Sysadm Windows Netapp. Or to be more clear. As you can see, the array has extended to include one more column and its form is now irregular. Now let’s suppose that our firm is facing an economical crisis and we, the good managers, want to reduce our costs and keep earning more $$$. We could fire Tommy because Cedric is more convenient due to the fact that he has two competences: Windows and NAS. So, let’s have a look at how to delete the row for Tommy from our multidimensional array. As far as I know, no nice easy way to do this. We must go through a foreach() cycle and then overwrite the original array with all the rows except Tommy’s one, which we will have found and excluded using the classical - notmatch operator against field [1] : $employee_list_temp = @() Foreach($employee. If($employee. 2[1] –notmatch "Tommy") { $employee_list_temp += ,($employee. Tommy has left… ; -)" write- host "===============================" foreach($employee in $employee_list) { Write- host ($employee) }. We get: 2 Jonathan 8. Sysadm Unix. 3 Mario 6. DBA Oracle. 4 Jeremy 7. DBA MS SQL. 5 Bill 8. Network Cisco. 6 Cedric 7. Sysadm Windows Netapp. As you can see the row for Tommy has magically disappeared. The array first row is Jonathan’s one now. The last action we want to be able to perform is to sort the content of out multidimensional array. To do so, nothing better than the classical sort- object cmdlet using field number 2, which is the salary in ascending order in our case: $employee_list_by_wage = $employee_list | sort- object @{Expression={$_[2]}; Ascending=$true}. The information is stored in the object $employee_list_by_wage. We can now cycle in $employee_list_by_wage to get the list of our employees sorted by wage: write- host "===============================" write- host "Employees by salary" write- host "===============================" foreach($employee. Write- host ($employee. DONE================". The output will look like this: 3 Mario 6. DBA Oracle. 4 Jeremy 7. DBA MS SQL. 6 Cedric 7. Sysadm Windows Netapp. Jonathan 8. 00. 00 Sysadm Unix. Bill 8. 10. 00 Network Cisco. Here’s the graphical version. Ok guys, now you know the basics of multidimensional array handling in Powershell and you know how to add and remove elements from jagged arrays. Of course this script can be optimized, reorganized, compacted, but this is out of my scope. I just wanted to quickly demonstrate the ease with which Powershell manages data. I hope that you see my point of view. Powershellers, if you want to suggest any improvements or correction, you are warmly welcome to do so! SOLVED] WSUS console can't connect> > System. IO. IOException - - The handshake failed due to an unexpected packet format. This message is almost always an indication of a misconfiguration involving SSL. But neither IIS nor SSL are involved in the service starting up, so there could be multiple issues at work, or the IOException could be a manifestation of the service being broken. Not forgetting that this is a VMWare - > Hyper. V migration, but I'm a bit challenged to consider anything that a migration could impact at this level. Typically migration failures manifest as machine that won't boot.> > All I get from the event view is "Update Services failed its initialization and stopped" with an error 5. With respect to Event. ID 5. 07.. that's just a generic "Wsus. Service. Failed" message, so we're going to need to dig a bit deeper to find some WHY to the situation. All this gives us is the WHAT. However, per this guidance, let's start by running a wsusutil checkhealth and then looking in the App. Event. Log again for anything else of relevance..
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
November 2017
Categories |