K2000 Post Install Task - Powershell script won't run properly
Hi,
I'm relatively new to powershell and cannot figure out why this script will not run as a Post Install Task via the K2000. The task uses a batch file to call a simple powershell script, which matches the computer name with certain criteria. If the criteria is met an .exe file is placed into the C:\users\public\desktop folder. I created an Application task, added my .zip file, used call batchfile for the command, and added the task to my System Image.
The script works fine if I run the batch from C:\kace\applications\script# and also fine if I make it a K1000 task. If I add a little bit of logging to the powershell and run it as a Post Task, I can see it is running as Workgroup\System and that the computer name has already been correctly applied by an earier Post Task. I also see, by adding write-host lines, that the criteria is being met, but for some reason the Move-Item does not take place. I tried removing all my other PostTasks, to see if one could have been hanging it up, but that didn't work either.
Contents of Zip (3 files):
1) Lockdown Browser.exe - This file should get moved from C:\kace\applications\script# to C:\users\public\desktop if the criteria in the powershell is met.
2) batch file - Used to just call the powershell
@ECHO OFF
powershell.exe -nologo -executionpolicy bypass -WindowStyle hidden -noprofile -file maps.ps1
3) maps.ps1
if ((gwmi Win32_ComputerSystem).Model -like "*2110*") {Exit}
if ($env:computername -like "E-*") {move-item -path "Lockdown Browser.exe" -destination "C:\Users\Public\Desktop\Lockdown Browser.exe" -force}
if ($env:computername -like "I-*") {move-item -path "Lockdown Browser.exe" -destination "C:\Users\Public\Desktop\Lockdown Browser.exe" -force}
if ($env:computername -like "O-*") {move-item -path "Lockdown Browser.exe" -destination "C:\Users\Public\Desktop\Lockdown Browser.exe" -force}
if ($env:computername -like "P-*") {move-item -path "Lockdown Browser.exe" -destination "C:\Users\Public\Desktop\Lockdown Browser.exe" -force}
if ($env:computername -like "R-*") {move-item -path "Lockdown Browser.exe" -destination "C:\Users\Public\Desktop\Lockdown Browser.exe" -force}
if ($env:computername -like "W-*") {move-item -path "Lockdown Browser.exe" -destination "C:\Users\Public\Desktop\Lockdown Browser.exe" -force}
So if the system model is not a Dell 2110, and the system name begins with E-, I-, O-, P-, R-, or W- then the .exe should be copied over.
I've tried using Copy-item instead of move-item, using -path and -destination, not using -path and destination, using -force, not using -force, and using the absolute path for the .exe ("C:\kace\applications\script#\lockdown browser.exe") but nothing seems to be working.
I can verify the powershell is launching during postinstall, and can also verify the machine criteria is being met. Any ideas why this would not be working? Again, the script works fine run manually or through the K1000. Our environment is Windows 7 32 bit Enterprise. Our K1000 is on v5.4.70402 and the K2000 is on v3.4.54256.
Could it have something to do with Public being hidden? - I think -force bypasses that
Could it be some kind of permissions issue to the source or destination paths? It should be running as system so I wouldn't think so
Is the powershell for some reason not able to find the dependancy file even though they should technically by in the same directory?
Any help is greatly appreciated. I've already spent way too much troubleshooting this. Thanks.
Answers (3)
I used your resource to rewrite my 2 scripts with the try/catch, variables for paths, and the get-items. Looks much cleaner now. The scripts worked fine locally and I was able generate errors to the logs with the catch by tweaking the scripts. But again when ran from the k2000, nothing. No logs generated either. (no run or no error)
I also tried removing my zips and calling the powershells directly from the command line of the application post tasks (removing the dependancies since they were local to the system now anyway from the mid lvl task) and a few other things... but no go. I wonder if it's some kind of timing issue with the powershells. Kind of like how they say to use call to call bats in post task cmd or it can cause issues.
Gave up on the powershells and after a little research I was able to peice together the same script in batch code, which I didn't think was possible at first. I'm assuming this will solve my problem once I run it next week. Thanks again for all the suggestions though they will come in handy.
The kace batch runs from \applications, so if that batch is calling powershell you are looking for the -path exe in \applications not \script#. Try putting complete path in your powershell lines instead of just the exe name.
Comments:
-
Hi SMal,
Thanks for the response. I tried -path "C:\Kace\Applications\101\Lockdown Browser.exe", but that didn't work. The applications\101 directory is where my zip get extracted and exe resides when I stop kcleanup.exe from running. Are you saying I should use -path "C:\Kace\Applications\Lockdown Browser.exe" instead? - SDNBTP 11 years ago-
Yes give that a try.
Also for troubleshooting you can caste an image, do not reboot, go to the main menu and choose recovery. you can then go to the c: drive and look at the kace directory and the Batch/script files, this may also tell you whats going wrong. - SMal.tmcc 11 years ago-
One other strange thing I have seen sometimes \public\public desktop\ works when \public\desktop\ does not in other circumstances. - SMal.tmcc 11 years ago
-
Test 1) -path "C:\Kace\Applications\Lockdown Browser.exe" - no luck with this.
Test 2) Public\Public Desktop - Great suggestion, Public\Desktop has always worked in the past for me so I never even realized it was structured that way. Unfortunately just changing that didn't work either.
Another good tip for the Recovery area, thanks. Very helpful in verifying things. But everything seems to be where it should be. The file was here: C:\Kace\Applications\101\Lockdown Browser.exe but for whatever reason it's still not getting pulled. Another weird thing is if I remove kcleanup task I can run InstallCMDs.bat after the post task does not succeed and the script/file move works perfectly fine.
I think my next step is to try something you suggested on another post, creating a mid level copy task from the temp directory into the C:\kace folder. Maybe then I can pull the files from there. If that doesn't work I may just give up and keep this as a after image step/K1000 script, I'm really at a loss here. - SDNBTP 11 years ago-
Could you post the bat file and the file it calls in 101.
Is this a sysprep'd or scripted install?
Before reboot after imaging can you go to public desktop on the c drive?
Is it running before the computer name is applied?
If you think it is the directory being hidden you can issue an attrib command prior to the powershell copy to change the flag - SMal.tmcc 11 years ago
-
No luck with the mid-level task either. The files show up in C:\Kace but no copy over even from the new location. - SDNBTP 11 years ago
-
1) Sure... Here is the code of the latest failure.
My mid level task to copy files
xcopy "Y:\applications\101\contents\Lockdown Browser.exe" C:\Kace
xcopy Y:\applications\102\contents\Elm.iw C:\Kace
xcopy Y:\applications\102\contents\Ike.iw C:\Kace
xcopy Y:\applications\102\contents\OL.iw C:\Kace
xcopy Y:\applications\102\contents\PC.iw C:\Kace
xcopy Y:\applications\102\contents\RR.iw C:\Kace
xcopy Y:\applications\102\contents\WE.iw C:\Kace
(verified files are being copied over to C:\Kace)
Post Task: (Application script, Attached my zip)
Command: call callmaps.bat
callmaps.bat:
@ECHO OFF
powershell.exe -nologo -executionpolicy bypass -WindowStyle hidden -noprofile -file maps.ps1
maps.ps1:
if ($env:computername -like "E-*") {move-item -path "C:\Kace\Lockdown Browser.exe" -destination "C:\Users\Public\Public Desktop\Lockdown Browser.exe" -force}
if ($env:computername -like "I-*") {move-item -path "C:\Kace\Lockdown Browser.exe" -destination "C:\Users\Public\Public Desktop\Lockdown Browser.exe" -force}
if ($env:computername -like "O-*") {move-item -path "C:\Kace\Lockdown Browser.exe" -destination "C:\Users\Public\Public Desktop\Lockdown Browser.exe" -force}
if ($env:computername -like "P-*") {move-item -path "C:\Kace\Lockdown Browser.exe" -destination "C:\Users\Public\Public Desktop\Lockdown Browser.exe" -force}
if ($env:computername -like "R-*") {move-item -path "C:\Kace\Lockdown Browser.exe" -destination "C:\Users\Public\Public Desktop\Lockdown Browser.exe" -force}
if ($env:computername -like "W-*") {move-item -path "C:\Kace\Lockdown Browser.exe" -destination "C:\Users\Public\Public Desktop\Lockdown Browser.exe" -force}
I can verify the conditions of the powershell are being met by using write-host with the same conditions but unique output. They show in the powershell output log. For example: if I use 'start transcript file'/stop transcript to enable the logging, then add the 2nd 'if line, it appears in the powershell log. But the move never happens:
if ($env:computername -like "I-*") {move-item -path "C:\Kace\Lockdown Browser.exe" -destination "C:\Users\Public\Public Desktop\Lockdown Browser.exe" -force}
if ($env:computername -like "I-*") {write-host "I-* system detected, moving shortcut"}
Log: I-* system detected, moving shortcut
2) This is a sysprep'd system image
3) Yes I can access the users\public\public desktop folder before the reboot, and see existing shortcuts from my image.
4) The script is running after the name is applied. I also verified this using Start Transcript in the powershell. The output file says running as: Workgroup/System and Computername: I-TEST-6410.
5) Well here's the thing. I'm trying something similar with a different program (hence the 102\contents\*.iw moves mid task) that just copies a config file based on the computer name via powershell (using most of the same code) to a Program Files\ directory. That is not working either, the file never moves but the conditions are being met. (I isolated both tasks into their own separate test image, just to make sure no other post tasks were conflicting) - SDNBTP 11 years ago-
read thru this on logging the powershell and dump the file to c:\ so you can read it after
http://community.spiceworks.com/how_to/show/3016-error-handling-and-logging-in-powershell - SMal.tmcc 11 years ago
-
Thanks by the way for digging into this. Appreciate it. - SDNBTP 11 years ago
-
It is frustrating when things like this happen, it usually some little change in wording.
If this is a script install or syspred image do you autologin in the answer file? if so is that user an admin?
Try taking the space out of the name. - SMal.tmcc 11 years ago
-
Try using the full path to call powershell
%windir%\system32\WindowsPowerShell\v1.0\powershell.exe -nologo -executionpolicy bypass -WindowStyle hidden -noprofile -file maps.ps1
Corey
Lead L3 Enterprise Solutions Consultant, K2000 - cserrins 11 years ago