Jump to content
brett.savage

Converting PowerShell Scripts to CWA Scripts

Recommended Posts

I am working on converting out existing powershell scripts to CWA Scripts and running into some issues. 

 

1st I tried to use the "Execute Script" with Powershell (and PowerShell Bypass) and just copied the existing powershell script into the editor...it would just get hung up and I would have to reboot the PC to run another script against it. 

Then I  tried downloading the script using the CWA Download URL option to a temp folder (which works) run the script with the same "Execute Script" with powershell and the bypass option, nothing.

last thing i tried was to break apart the script (my test one was only a few lines) and use "shell Enhanced" with powershell.exe in front of it. 

every time I try this CMD "Invoking Script" will be stuck at running. 

 

All these things failed to work or run the simple tasks. The entire Script will run just fine if I run the .ps1 file from the test PCs. 


Example Script

#Stopping Software
Stop-Process -Name "software name"
#Creating Temp Folder
New-Item -Path "c:\" -Name "dbitemp" -ItemType "directory"
#Downloading File
Start-BitsTransfer -Source https://URLtoStoredFile/software.msi -Destination c:\dbitemp\software.msi
#Installing the software
"C:\dbitemp\software.msi /passive" | cmd
sleep 60
#removing temp folder
Remove-Item –path c:\dbitemp –recurse
 

Share this post


Link to post
Share on other sites
Posted (edited)
On 5/20/2019 at 8:46 AM, brett.savage said:

#Stopping Software
Stop-Process -Name "software name"
#Creating Temp Folder
New-Item -Path "c:\" -Name "dbitemp" -ItemType "directory"
#Downloading File
Start-BitsTransfer -Source https://URLtoStoredFile/software.msi -Destination c:\dbitemp\software.msi
#Installing the software
"C:\dbitemp\software.msi /passive" | cmd
sleep 60
#removing temp folder
Remove-Item –path c:\dbitemp –recurse

Some basics to know about Execute Script.  The file will always be ANSI. Unicode characters will be lost. It is easily apparent that the "-" characters in the Remove-Item parameters are not a standard dash, but are the Unicode long dash. Delete and re-type those characters. Other common problem characters are "pretty quotes" (not ANSI straight quotes). A good test is to save your .ps1 as a plain ANSI file. Then re-open and review. You will find the invalid characters have become weird glyphs.

 

On 5/20/2019 at 8:46 AM, brett.savage said:

"C:\dbitemp\software.msi /passive" | cmd

The Unicode->ANSI alone will mess you up, but it should just cause the script to fail, not to hang.  Things that will cause a hang are closing input/output handles, launching a GUI application in a non-GUI service context, or launching a process that stays running, which causes powershell.exe to not exit.

I would not use "/passive", that still enables a progress window and almost certainly is your problem. You are also using an unusual invocation for cmd.exe, which seems unnecessary as you are already in a shell.

Instead try:
& msiexec.exe /i c:\dbitemp\software.msi /quiet

Edited by DarrenWhite99
Added "/i" to msiexec.exe

Share this post


Link to post
Share on other sites

Thank you for the reply. I did what you suggested, I converted the text to ANSI, I also tried  manually typed it into CWA Script, to make sure it was in right. but it is still getting stuck at Invoke Script "Executing" inside CWA.

 

I even tried making a CWA script that would just download the known working .ps1 and run it (with and with out excutionpolicy -bypass) and it still gets suck. So I think there is something wrong with my CWA itself. 

Share this post


Link to post
Share on other sites

Did you stop using the /passive parameter like I mentioned? I also just realized that the msiexec command was missing the "/i" option. I updated my earlier post.

Share this post


Link to post
Share on other sites

With UI:

prompt for stopping process which stops script waiting for input, used A, script continued

Created Folder

Error on the download file line: "Start-BitsTransfer : The operation being requested was not performed because the user has not logged on to the network. The specified service does not exist."

Failed install (no file) 

Deleted Folder

Ran again with UI, this time putting the installer in place manually.

prompt for stopping process which stops script waiting for input, used A, script continued

failed on create folder (duh it already existed)

Failed on download: same as before

install worked

deleted folder worked. 

 

With /v:on

Failed to close out the app before install, but the rest of the steps went through no issues.

 

Share this post


Link to post
Share on other sites

Added a -force after the stop process and that resolved the /v:on from stopping and from the hang with the on UI

Edited the "excute PowerShell with Bypass" with the force and that seemed to resolve the issue with the CWA script as well.  It runs almost perfectly, the only thing it does not do is open up the application, but that's not to much of a big deal. 

 

 

Thank you for your assistance on this matter. I will book mark the page and use this to see if any of my future converted scripts have any issues. 

Share this post


Link to post
Share on other sites
9 hours ago, brett.savage said:

Edited the "Execute PowerShell with Bypass" with the force and that seemed to resolve the issue with the CWA script as well.  It runs almost perfectly, the only thing it does not do is open up the application, but that's not to much of a big deal. 

The key is that the commands MUST be silent, and they CANNOT interact with the UI/User.  I'm not sure why you are commenting that it does not open the application, that should never have been a goal.

If you DO want to open an application for the console user, you need to use a different function. @Gavsto has a great post on this at https://gavsto.com/running-programs-scripts-as-a-logged-in-user-in-a-labtech-automate-script/

Share this post


Link to post
Share on other sites

I've found that running msiexec with a log parameter helps greatly when diagnosing installations. If the log is not created then msiexec didn't even start to run the installer. If you get a log its an indication that your script has correctly download an msi and correctly tried to install it. Finally the contents will at least tell you something about what went wrong.

  • Like 1

Share this post


Link to post
Share on other sites

We have clients who do not like change, and they have come to expect this application to always be running. Ill add the CWA option to start the program and remove it from the PS script. 

 

Thank you again for your assistance. 

Share this post


Link to post
Share on other sites

I've had the similar problem recently - the problem seemed from my end the script never executed, yet redered to be hanged.

But there's a way to proove if it's being executed - transcripting it. You can enable this GPO (on some test box) and see what happens:

https://sid-500.com/2017/11/07/powershell-enabling-transcription-logging-by-using-group-policy/

Apparently, not all POSH commands run under SYSTEM account as you'd expect them to be.

Share this post


Link to post
Share on other sites
8 hours ago, kprocyszyn said:

Apparently, not all POSH commands run under SYSTEM account as you'd expect them to be.

And, to find out which commands work as system and which don't, you can test as described here: 

 

Just fire up the system sessions, and run powershell.exe inside, then test the same as described in that post.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...