Yesterday was my first attempt at running my entire Windows 7 PC build Powershell script that I've been working on and I had several problems with some lines of code not actually running and sometimes getting an error and other times not getting an error.
For example, because of a bug in AutoIt, which I install early in the script, I need to load C++ (both x64 and x86). And these are the two lines of code I'm using to install both versions of C++:
$result = Start-Process -Wait -FilePath 'C:\vcredist_x64.exe' -ArgumentList '/passive' -PassThru
$result = Start-Process -Wait -FilePath 'C:\vcredist_x64.exe' -ArgumentList '/passive' -PassThru
and right after that, I need to copy a file added when C++ is installed, rename it and move it into a different directory, so the first line of code after the two above is:
Copy-Item C:\Windows\SysWOW64\msvcr100_clr0400.dll C:\Temp
but that line of code fails saying "msvcr100_clr0400.dll does not exist", so the remaining lines to rename and move that file all also fail. Yet if I go look in that directory right after running the script, the file does indeed exist.
I ran those lines alone under Debug and watched and sure enough, even though I have a -Wait for the .exe, I see the Copy-Item line runs a few seconds before the .exe is actually finished. So how do I correct this if -Wait doesn't seem to be doing what I expect it to be doing? Is there a way for me to make sure the next line of code does not execute until the previous line is truly, completely finished?
On that same note, I had a couple other lines of code that went through without any errors, yet they did NOT do what they were supposed to do. But when I copied those lines and put them alone into a separate Powershell script and ran that script they all executed correctly. These are the lines of code to set the Power Plan to "High Performance" and to set "Change when the computer sleeps" and "Change when to turn off the display" to "Never" -
(Get-WmiObject -Name root\cimv2\power -Class Win32_PowerPlan -Filter 'ElementName="High Performance"').Activate()
powercfg -change -standby-timeout-ac 0
powercfg -change -monitor-timeout-ac 0
They work if I run them alone in their own script. But when they are near the end of my very long script (right after a section where I am using Get-Acl and Set-Acl to change permissions/security on some directory folders) they do NOT make those changes. So is there something I'm not getting about how a long Powershell script executes where certain lines might be "overlapping" each other?