Installing a WinForm application with Deployment Manager

swinghouseswinghouse Posts: 120 Bronze 2
edited January 10, 2013 12:14PM in Deployment Manager
Thanks for the help in setting up Windows Services with Deployment Manager: http://www.red-gate.com/messageboard/vi ... 8646#58646

There's another type of .NET application that I'd like to manage through Deployment Manager, namely WinForms applications.

I suspect I should write a PowerShell script for this as well? Could you point me in the right direction as to which command(s) I should use?

(You'd be correct in identifying me as a PowerShell n00b!)

Please let me know if you think I should direct these PowerShell specific questions to other forums.

/Mattias

Comments

  • Hi Mattias,

    I'm glad we helped you get your Windows Services setup.

    Yes, you could write a powershell script to deploy your WinForms apps, but there are a few possible approaches. How do you currently deploy them?

    Thanks,

    Justin
    Justin Caldicott
    Developer
    Redgate Software Ltd
  • swinghouseswinghouse Posts: 120 Bronze 2
    Hi Justin,

    Thanks for the reply!

    Currently, we deploy the applications by brute force - we generate an installer package through a Setup and Deployment project in Visual Studio, copy the packages to the target servers and run them manually there. Not an ideal situation, to put it mildly... :roll:

    /Mattias
  • Hi Mattias,

    I see, so you have a known set of machines that you want to keep up-to-date with some winforms software.

    The challenge with deploying to many machines like this will likely be the initial setup. That is, any registry settings, shortcuts or other functions the installer performs, would need to be included as part of your PowerShell script, to be used on first deploy.

    Once the software is installed, it would work really nicely keeping these up-to-date from the Deployment Manager UI, without needing to manually install updates on each machine.

    I hope this helps! Kindly let us know how you get on, or if you need further help with any powershell, etc.
    Justin Caldicott
    Developer
    Redgate Software Ltd
  • swinghouseswinghouse Posts: 120 Bronze 2
    Hi,

    Many thanks for your help and sorry for the belated reply.

    The scenario you describe sounds very reasonable.

    Please forgive my ignorance, but currently I'm stuck on a PowerShell detail. I tried to issue the following command through a PreDeploy.ps1 script:
    $processName = "MyProcess"
    Get-Process $processName | % { $_.CloseMainWindow() }
    

    What I want to accomplish is shutting down the "MyProcess" process before deployment starts.

    Alas, the script doesn't manage to shut down the process (and returns false) when it is run by Deployment Manager. In contrast, running it locally on the target server, the script succeeds.

    I suspect this is a security restriction in Windows? Are there any alternative commands I could use, or could I "elevate" the script?

    Any help would be most appreciated!

    /Mattias

    PS. We really should convert this WinForms application to a Windows Service, but for now we have to cope with it. :-( DS.
  • Hi Mattias,

    Have you tried using the PowerShell 'Stop-Process' cmdlet (MSDN)?
    Stop-Process -processname MyProcess
    
    or using the 'kill' alias for Stop-Process...
    kill -processname MyProcess
    
    (by the way, the processname parameter accepts wildcards as well. I decided to try
    kill -processname *
    
    which you'll be glad to know results in an almost immediate BSOD :twisted:)

    The PowerShell runner should have the appropriate permissions, because it runs in process with its host, the Red Gate Deployment Agent service, which normally runs as Local System.
    Development Lead
    Redgate Software
  • swinghouseswinghouse Posts: 120 Bronze 2
    Hi Mike,

    Thank you so much, Stop-Process worked beautifully!

    Kudos as well for the final code sample in your post... :lol:

    (And I really do need to get up to speed with PowerShell - a perfect task for the upcoming holidays when I come to think of it.)

    /Mattias
  • Hi Mattias,

    If you get a chance, it would be great to share your script for deploying Win Forms apps in our new PowerShell forum!

    http://www.red-gate.com/messageboard/vi ... .php?f=166
    Justin Caldicott
    Developer
    Redgate Software Ltd
  • swinghouseswinghouse Posts: 120 Bronze 2
    Hi Justin,

    I'd love to contribute with something profound, but my current PowerShell script is very humble indeed!

    All I do is check if my Win Forms app is running and, if that is the case, I shut it down, like so:
    $fullProcessName = $processName + '.exe'
    if ((Get-WmiObject win32_process -Filter "name = '$fullProcessName'") -ne $null) {Stop-Process -processname $processName}
    

    This is in the PreDeploy.ps1 script. The binaries are deployed through the magic of Deployment Manager and NuGet.

    I'll revisit the PowerShell forum if and when I have some "juicier" scripts to share.

    Update

    I went ahead and linked to this discussion at http://www.red-gate.com/messageboard/vi ... 9594#59594
  • swinghouseswinghouse Posts: 120 Bronze 2
    One drawback to Stop-Process command that I have come to realize is that it terminates the process abruptly, without letting the process perform its usual shutdown activities.

    Therefore I tested this command:
    (Get-Process 'processName').CloseMainWindow()
    

    This works really well when I run it directly on the target server, but unfortunately not when Deployment Manager attempts to execute it from my PreDeploy.ps1 file. In the latter case the process isn't stopped and CloseMainWindow() returns false.

    Is it impossible to run CloseMainWindow() from Deployment Manager?

    (This is related to my question at http://www.red-gate.com/messageboard/viewtopic.php?t=16312)
  • Hi Mattias,

    The Deployment Manager Agent service which runs on your target machine runs is probably running on a different session ID to your WinForms application. If this is the case, the Agent service cannot see your WinForms application UI and hence can't close it.

    You can view the session ID of different processes from Windows Task Manager in 'Processes' tab. Make sure the column for session ID is enabled from View -> Select Columns.

    Because your Agent service starts up at boot time automatically, it runs under SYSTEM username and has session ID 0. Your WinForms application is running in a different session ID.

    One fix to this problem would be to get the agent to run in same Session ID as your WinForms application. Please follow the steps below:
    - Start Task Manager.
    - Navigate to services tab.
    - Right-click 'Red Gate Deployment Agent' and stop service.
    - You can now start the Agent service in Interactive mode from the start menu option 'Deployment Agent (interactive mode). This should run in session ID 1 (unless you have multiple users logged in to your system, each of them have a different session ID allocated).

    You can also configure the agent service to not start up automatically when you boot your server. If you go to start menu option 'Services' -> right click 'Red Gate Deployment Agent' -> Properties -> Change 'startup type' to manual.

    Let us know if it works out!

    Thanks!
    Chirayu Shishodiya
    Software Engineer - Deployment Manager
    Red Gate
  • swinghouseswinghouse Posts: 120 Bronze 2
    Hi Chirayu,

    That worked superbly! Thank you!

    One follow-up question: Our current setup with the WinForms application running on a server, is to 1) automatically log in a user when the computer boots up and 2) auto-start the WinForms application. I guess these steps are required for Deployment Agent as well when running it in interactive mode?

    (I realize that running a WinForms application on a server like this really isn't ideal. We will certainly port this app to a Windows Service, but that's (still!) in the future... On that lucky day we can switch back to running Deployment Agent as a proper service.)

    /Mattias
  • Glad it works, Mattias!

    If you want to deploy to that target machine, then the Agent ought to be running either as a service or in the interactive mode.

    The agent service starts up automatically in the background, when your server boots up (that is the default behaviour at least). You can get the agent in interactive mode to start up automatically when computer boots up by:
    - Place a shortcut of 'Deployment Agent (interactive mode)' on desktop
    - Cut the shortcut and paste it into Start Menu -> All Programs -> Startup

    Thanks!
    Chirayu Shishodiya
    Software Engineer - Deployment Manager
    Red Gate
  • swinghouseswinghouse Posts: 120 Bronze 2
    Thanks Chirayu,

    And by automatically logging in the user in question when the system boots up, Deployment Agent (in interactive mode) is automatically available again.

    I'm happy with this setup for now. Like I wrote earlier, when we get rid of the WinForms application, we'll certainly switch back to running Deployment Agent as a service.

    /Mattias
  • Thats great!
    Chirayu Shishodiya
    Software Engineer - Deployment Manager
    Red Gate
Sign In or Register to comment.