Call PowerShell Script Function from a Job

March 13, 2014 Leave a comment

One of the blockers which I’ve had previously in writing concurrent PowerShell scripts is the fact that any new PowerShell job runs in a new PowerShell.exe instance and has no idea about your script. Since it doesn’t know about your script, any references to any script functions can’t be ran in the job. But I have figured out a way.

The following script is one where a new PowerShell job is started, and that job calls on a function from the script. The trick is to dot load the script before calling the function, and have a line in the script which returns if it is being ran because it was dot sourced.

function MyFunc
$tid = [Threading.Thread]::CurrentThread.ManagedThreadId
 Write-Host "$message on $PID $tid"
 "Return value from process $PID thread $tid"
if($MyInvocation.InvocationName -eq '.') { return; }
$def = $MyInvocation.MyCommand.Definition
Write-Host $def
$Jobs = @();
 $data = @();
 $names = @( "John", "George", "Mary" )
 foreach($name in $names){
 $Jobs += Start-Job -ScriptBlock { param($scriptToDotSource, $toPassAlong) . $scriptToDotSource; MyFunc $toPassAlong } -ArgumentList $def, $name
foreach($job in $jobs){
 $returned = Receive-Job $job -Wait
 $data += $returned
Write-Host "Break"

Creating Styles in Outlook

February 10, 2014 Leave a comment

When composing an email an Outlook there might be a font
Style that you like to apply regularly. I have yet to find a way to create a
style for Outlook messages in Outlook itself. Leaving me with only the styles which come with Outlook. I have discovered a way to create custom styles for Outlook messages.

1. Close Outlook.

2. In File Explorer go to %appdata%\Microsoft\Temples. In that directory there will be a file called
NormalEmail.dotm. Right click to open the context menu and select Open. This should open NormalEmail.dotm in Word.

3. In the Styles part of the Ribbon, bring down the
extra options and select “Create a Style”.

4. Create the style you want.

5. Save the file and close Word.

The next time you compose a message in Outlook, you will see
that your new style is there.

Programming should be taught in middle school

February 2, 2014 Leave a comment
In every public school system reading is taught and writing is too. I don’t know of any example where writing isn’t taught. The first few years might focus on only reading, but once the hand eye coordination gets good enough that the kids can start writing, they are taught how to write. Some students show great aptitude for reading, but then don’t write so well, and some students don’t care much for reading, but are excellent writers. Generally strength in one, correlates with strength in the other, but not necessarily. Yet at the moment public education only teaches students how to read math, it doesn’t teach students how to ‘write math’. This results in an imbalance that generates feelings about how learning math is a complete waste of time. Forgot Algebra is an excellent XKCD comics which illustrates this point; the text in the comic says “It’s weird how proud people are of not learning math when the same arguments apply to learning to play music, cook, or speak a foreign language.” I don’t think it is weird because the math education most people get is akin to learning only how to read, with never being taught how to write. Their brain is imbalanced.
So how should students be taught how to ‘write math’? by programming. I don’t think that some computer programming should be taught at the very end of high school, for the more advanced students, it should be taught earlier than that, and to everyone. If I remember my education correctly, I had Pre-Algebra in 7th grade, and Algebra 2 in 9th grade. Toss them out, and replace them with Introduction to Programming and Data Structures. Everything which is taught in Pre-Algebra will accidentally get picked up writing some simple programs, the concepts of Algebra will get picked up faster because the students will have had some hands on experiencing using those concepts (but not being aware of it), and there are bound to be Data Structure problems that can be used to teach the concepts in Algebra 2.
I feel strongly that by teaching students how to inadvertently use math, they’ll be much more likely to pick up the concepts of math which are currently being taught in High School today.
When teaching children how to read, we don’t drill them on every shape and sound that letters make, before teaching them concepts like words and sentences. We start by reading to them, and then go back and teach some basics. We show them examples of what reading can do for them. Each progressing year in school will have more and more teaching on how to read, practice reading, how to write and practice writing. It doesn’t work to teach every concept of reading before moving onto writing, teaching and practicing the principles of both re-enforce the other. The same is true for math and applied math, i.e. programming.
I remember seeing an episode of Full House where one of the daughters storms into the house complaining about how hard math is now. In the previous grade (6th?) math was all numbers, and now the teachers were making it confusing by introducing letters; i.e. variables. This fictional character isn’t the only one who had difficulty crossing this mental math barrier. But if instead of being taught ‘letter math’ she was taught programming instead, the transition to ‘letter math’ would be much more natural. Programming would feel like a completely new subject, not like math at all. And the idea of sticking a number into a variable wouldn’t feel weird at all, it’s just what you do in programming. Then after a year of writing functions and function declarations (a name given to the concept of naming an algorithm, declaring what it takes as an input, and what it will return) in her programming class, when it came time to take algebra it would all feel very second nature to the student.
Personally, I never understood the f(x) notation while I was in high school and middle school. I was very good at math; I loved math, and even got a 5 on the AP Calculus test. But I still struggled with what the f(x) notation was supposed to convey. I understood y = x + c, notation just fine. Plugging in numbers for known variables was simple. But that whole f(x) (pronounced f of x) thing was beyond me. Then one day in my first semester at University in my Introduction to Programming class my professor shorthanded a function declaration on the whiteboard as f(x), and all of a sudden so much of my middle and high school education made a whole lot more sense. If I would have had to opportunity to ‘program’ a couple of functions myself – with proper declarations and understanding why they were needed – the notation and purpose used in math class would have been significantly more relevant.
Programming is a useful skill in and of itself, but it is also the yin to math’s yang. It is time to make our students more well rounded by teaching them the possibilities of what a computing device can do, and show them how to apply math. Just like how kids will spend the first few years of their lives being taught how to read, without really being taught how to write, the elementary school years of math should be kept as is. But once it’s time to introduce variables into the math education, the students should be taught programming, so that they may begin to ‘write math’. Their understanding of math will grow significantly and they’ll pick up a useful skillset at the same time.

Roaming Desktop limitations

January 18, 2014 Leave a comment

In Windows 8 the Desktop Backgrounds which exist for the first account you link to your Microsoft Account, become your roaming desktop backgrounds. What that means is any other computer you sign into with your Microsoft Account gets those desktop backgrounds too. But there is one caveat; the Microsoft Account will only roam up to 20 pictures. As far as I can tell there’s no way to configure which 20, so it’s crap shoot. Better than nothing, and I suspect that the number will increase as time goes on.

Sadly for me I have roughly 60 pictures as my primary wallpapers, and hope that Microsoft will bump the limit soon. 

Dissapointments with Windows 8.1

January 11, 2014 Leave a comment

I know that lots of people heralded Windows 8.1 as an improvement to Windows 8, but I do not share their enthusiasm. I find the update to mainly be a disappointment. The two computers that I primarily use are desktop computers which both originally ran Windows 7, and neither of them have the touch screen. I am a heavy keyboard user and have only found only one scenario where I found using the keyboard to be worse in 8 than in 7 (context menu on start screen items). But upgrading to Windows 8.1 has left me with these disappointments:

  1. The Start Tip, aka. the return of the Start Button. I really liked getting rid of the start button. I hardly ever used it since Vita, and it took up space on the task bar. When the Start Button isn’t there I would virtually never end up with two rows of icons on the Task Bar, and now that it’s back I occasionally do get two rows of icons (which I find to be very annoying).
  2. The removal of the Search Contract. This is a big pain for me. I liked the Search Contract; I like the idea of universal search. I was equally disappointed when Microsoft removed it from Windows Phone in 7.5. I love starting an app, start typing and having the app respond. But with the Search contract removed, and I start typing in an app, nothing happens! It’s so frustrating, I’m trying to communicate with the program and the program isn’t listening. In Windows 8 if I start the market place I can start typing and immediately it starts looking for apps. It was awesome. Now, with the removal of the Search Contract I have to click on a search box to start searching; blech!
  3. Moving items on the Start Screen with a touch screen. This on only affects touch screen systems, and I’m kind of surprised that I missed it. At first I was really frustrated when I tried to move items around, but couldn’t. I just couldn’t do it. My toddlers seemed to have no issue with doing it. So I can see why Microsoft revisited how its done. But I got the hang of it before 8.1 came out, and actually found it to be really smooth once I knew what to do. Now I find myself trying to move an item, and it takes longer, and feels more like I’m using a computer than something that’s smooth and really advanced.
  4. The Messaging App. I don’t miss the Messaging App too much, but I know my brother does. I understand that Microsoft got rid of it in favor of having everything being done by Skype, but I think that it wasn’t time for that yet. The Skype team is still behaving like it think it’ll get sold off one day, and I still don’t find their apps/service to be superior to what Microsoft had at the time of the acquisition.
  5. De-emphasis of libraries. Libraries were introduced in Windows 7, and they were very useful. There are places where they were broken (like if you tried to upload files from a library), but I still think they made if feasible for ‘normal’ people to be able to share files on the same computer. Libraries are still there, but you have to have each user turn them on; and in some cases manually add the Public folders back.
  6. Modifying file properties when viewing the file through a library doesn’t work:
  7. I have ran into weird permissions issue. It’s like the Windows team forgot about the important work they did in Vista to make sure that one can use a computer without having to be the admin. See SettingSyncHost is consuming CPU, and Unable to start some apps after upgrade to Windows 8.1.

Windows 8.1 isn’t all bad; it did fix the stupid Apps vs. Settings search pain point, but I wish that it would have continued on the path that Windows 8 put down more so than what it felt like it did.

Unable to start some apps after upgrade to Windows 8.1

December 29, 2013 Leave a comment

After upgrading my parents Windows 8 computer to Windows 8.1 all of the user accounts on the computer didn’t have the Bing Weather app nor the Skype app. It was strange, the live tile for the apps were where they were on the Start Screen before the u;grade, but instead of showing anything they were rectangles with a small x in the lower right hand corner; and launching the resulted in showing just the background color. When I tried to reinstall the apps from the Windows Store the installation process would die with error 0×8004005, which is error code 5 which is Access Denied. That is really strange, because the whole point of WinRT apps is that a standard user should be able to install them to their own profile with no permissions issues at all.

My parents have a great setup on their computer, they have an administrator account that they never log into, and all of the users on the computer are standard users and sign in with their own Microsoft accounts.

I ran Process Monitor and filtered by Result is Access Denied. This showed a couple of entries but the one which looked interesting was one that was trying to access HKEY_USERS\\Software\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\Notifications\BackgroundCapability\S-1-15-2-2040986369-264322980-3882385089-1970153872-3662121739-3363227934-2464603330\App.AppX0reyrkpf8fvpmyz1tg3wc7fwjvf2e5c5.wwa. I opened regedit.exe as elevated, and could see the S-1-15-2-2040986369-264322980-3882385089-1970153872-3662121739-3363227934-2464603330 regkey, but I couldn’t expand it. That seems to be by design for every key under BackgroundCapability, so I gave it little heed. I then reopened regedit as the standard user and navigated to S-1-15-2-2040986369-264322980-3882385089-1970153872-3662121739-3363227934-2464603330 under HKEY_CURRENT_USER. I could expand it, but I could not open the App.AppX0reyrkpf8fvpmyz1tg3wc7fwjvf2e5c5.wwa child. This was strange because I could open its siblings. I suspect that the issue was similar to a previous issue that I had with the TrainedDataStore where the owner of a registry key under HKCU isn’t the current user.

I then reopened regedit as elevated and deleted the S-1-15-2-2040986369-264322980-3882385089-1970153872-3662121739-3363227934-2464603330 key. Then I went back to the Windows Store and was able to install the app. So the issue is most likely that sometime during upgrade the Owner of some registry keys under the user profile get reassigned to something other than the user (which is bad).

To fix this issue on your own, open regedit and navigate to HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\Notifications\BackgroundCapability. Its children will be Windows Security Principles, in my case the interesting one was S-1-15-2-2040986369-264322980-3882385089-1970153872-3662121739-3363227934-2464603330. Find the one where you can not see the values for it’s children (a pop up dialog will open informing you that you do not have permissions). Then reopen regedit with elevated permissions and find that regkey under HKEY_USERS and delete it. The Windows Store should be able to install the apps after that.

SettingSyncHost is consuming CPU

December 16, 2013 1 comment

I had an issue where the Host Process for Setting Synchronization (SettingSyncHost.exe) process was constantly consuming CPU whenever the computer was connected to a network that wasn’t a metered connection. It would run at around 31% CPU nonstop. The reason why it was running so hard was that it was constantly checking the files in %localappdata%\Microsoft\InputPersonalization\TrainedDataStore\en-us; but that directory had a problem. Instead of having only one or two files like it is designed to have, it had over 100,000 files.

The reason why TrainedDataStore had so many files in it, is that the user account I use didn’t have permissions to the HKEY_CURRENT_USER\Software\Microsoft\InputPersonalization\TrainedDataStore\en-US\2 registry key. Which is very strange, the logged in user should always have permissions to everything under HKEY_CURRENT_USER. So what was happening was that the InputPersonalization process was writing a file to TrainedDataStore\en-us, and then checking the registry key, but would get an error, and bail out. Then it would do that over and over again. The Host Process for Setting Synchronization would see that a change happened in that directory (a new file had been created) and then scan the directory over again. This would consume 31% CPU all day. Thankfully it wasn’t constantly uploading the changes to SkyDrive, that would have been really bad.

The crazy thing is that the local user was the owner over the en-US registry, but the local administrators group was the owner of the en-US\2 directory. Given that that key is under HKEY_CURRENT_USER that is really, really weird. I have no idea how it happened, but apparently the first time that InputPersonalization ran as that user, it must have been kicked off with elevated permissions when creating the registry key.

To fix the issue I logged in as the administrator, found the users key under HKEY_USERS, right clicked on the Software\Microsoft\InputPersonalization\TrainedDataStore\en-US\2 key and selected Permissions. I then pressed Advanced and at the top of the dialog window was able to change the owner. Once the owner was changed to the correct user, the %localappdata%\Microsoft\InputPersonalization\TrainedDataStore\en-us directory emptied quickly and the Host Process for Setting Synchronization hasn’t been a bother since.

Below are two screen shots from regedit. They show that en-US has the correct owner, but en-US\2 does not, it has Administrators as the owner.

TrainedDataStore\en-US Owner: Jared

TrainedDataStore\en-US Advanced Security Settings

TrainedDataStore\en-US\2 Owner: Administrators

TrainedDataStore\en-US\2 Advanced Security Settings


Get every new post delivered to your Inbox.