Powershell environment variables are not updated - powershell

I've have wix installer that updates environment variable using <Environment /> element.
The environment variable value is updated in registry. But when I try to open Powershell it doesn't see any changes unless I reboot my PC.
Does anyone have any thoughts about why/how and how can I avoid reboot.
Thanks in advance,
nomail
Update: I've found why this is happening. It turns out that the session variables are updated on reboot or if a system broadcast message is sent.
To send a broadcast message
SendMessage (HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)"Environment");
Another dirty way to send a message is to use SETX command that will write new value to environment variables and update session info
SETX something_that_is_not_important value_that_is_not_important
Mind you that you will still have to close and open CommandLine or PowerShell console

This is almost certainly because of a missing a <WriteEnvironmentStrings> element in the <InstallExecuteSequence> section of your WIX file. That custom action is responsible for sending the broadcast WM_SETTINGSCHANGE message.
This isn't exactly a bug in the WIX, rather a poorly documented requirement. To effectively use WIX you also have to understand the Windows Installer Database reference docs, not just the XML. In the MSDN topic on the Environment Table the need to invoke the WriteEnvironmentStrings and RemoveEnvironmentStrings custom actions is described, but in terminology that may be foreign to someone without native MSI background.
An article showing this is at http://blogs.technet.com/b/alexshev/archive/2008/03/28/from-msi-to-wix-part-13-installable-items-environment-variable.aspx

seems to be a specific wix problem, see comments at the bottom of : this page

Related

Powershell won't pass keystroke to cli window

I first need to offer the disclaimer that I am barely a novice when it comes to scripting or coding, so I might be doing this all wrong in the first place. I'm running into an issue with a script I had written in powershell a while ago that is used on several hundred computers in my company daily to automate starting a certain app that everyone uses. So far everyone else hasn't had an issue except one machine.
Normally when I run the script, it starts the services and cli for the program which opens in a new window. Then the script, still open behind that, sends all of the key commands to that cli window, pauses for a brief line of user input after which it closes that cli window, does a few other clean up tasks and closes out itself.
The only difference on this one machine that's not working is the script won't pass the keystrokes to the cli window. If I put them in manually including the normal user input, it finishes the rest of the script just fine, but the cli window is just not receiving the keystroke input from the main script.
The only part of the script that send the keystrokes to the cli window is a few lines of this:
[System.Windows.Forms.SendKeys]::SendWait("Text{Enter}")
I have tried using the sleep command before and in between the key commands to make sure it's not going through the keystrokes to early. I've checked and windows had gone through an Update two days ago, but we would've heard about this issue right then if that was it. I'm at a loss for what the issue could be and would apreciate and help, tips, or direction to help figure this one out.
Is the console window the active window when the keys are sent? The documentation for the class you are calling indicates that it sends the keys to the active window. Not only that, but the documentation also seems to indicate that there are some issues that developers have run into when using that class to sends keys. I would try using the autoit powershell module instead. Autoit is its own scripting language which specializes in automating windows processes and being capable of automating interactions with windows forms, but also comes bundled with its own powershell module that I think is exactly what you need, so I would download the portable "zip" package, extract the powershell module from "Autoitx", and that should help you accomplish what you need
PowerShell automation especially when using SendKeys can / is glitchy due to many varying reasons. Can you use SendKeys sure, but you have to know the environment it will be run in and the needed performance details. Hence your futzing/guessing with Sleep.
There are purpose-built tools to help.
Auto HotKey
'PowerShell auto hotkey'
or the UIAutomation tool
'PowerShell automating other applications'

Start an application at system start without login

We have a new server running and we got some new programs doing import routines. So far so good... But there is one program that is put into autostart folder. So it doesn't run until admin logs in and it stops if we logout.
I'd like to put this one into a seperate session so it may work without any interaction by simply starting it with the task scheduler at startup. Is this the right way to do this? Is it safe if I log in later and log out?
Many thanks!
Edit: The applications shows as a symbol in the task bar if running, it can be configured by this. Anything I must know about this if I change?
Edit: It is not my application, I cannot rewrite it as a service.
I successfully added the application by using task schelduler on startup. Login and logout will not quit the application but no symbol is shown. Please add details to my side questions and I'll mark your answer as the accepted one.
Edit: Ended up using this one. If I have to configure, I stop the application in task manager and start it again by link. After that I quit the application and restart it by task scheduler manual start.
You need to run your program as a Windows Service. One way of doing it is using the sc.exe program:
> sc create <new_service_name> binPath= "c:\myapp\myapp.exe"
You can read about it here.
You need to separate your application in two.
To allow it to run without a user session, you need a windows service. That should handle all the background stuff. You can then register the service and set it to start when the system starts.
To allow it to have a UI, and show up in the notification area, you need a windows application. This will be in autostart as usual, and will communicate with the service - for example, over named pipes.
While it is still (barely) possible to run an UI application without a user session, it's only maintained for backwards compatibility, and already shows a lot of problems. It will likely be removed altogether in the future, because it breaks quite a few contracts. Do not rely on hacks like this.
I also used the task scheduler to create the application at system startup. It should be noted that if you want to use for mining, you have to disable an option in "settings" where it says that if the application lasts more than three days in a row it will end.
It really works wonderfully!
it is a old question but I recently solved in another way...
(before I was using a scheduled-task for startup but this gave me diverse problems with lots software...)
Some programs also for diverse reasons must be run at a user level... or even inside a specific user session...
So the best way I found was to use a tool like Sysinternal/Autoruns to program the auto-logon to a specific user (it is a registry setting)... and in the startup-folder of that user (or any other "autorun/autolaunch" task)... run a script that first locks the screen... and next runs the other intended programs... that will run under that user profile...
so you can choose a standard user or a administrator... or even launch programs from a standard user in adminsitrator mode...
I hope will help...
This "hack" solved me many problems with startup apps...
I could not get the "sc create" command to work. Instead I manually edited the registry using regedit. I added a new key in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services.
I used the following page to figure out required parameters and their values. Note that the names do not map.
https://learn.microsoft.com/en-us/windows-hardware/drivers/install/inf-addservice-directive
Old question, but for anyone that stumbles here. Use srvany to set the program as a custom service.
Note that when you do this with for example dropbox, googledrive, etc., you will need stop the service, then open the program normally to make changes like password, updates, etc.
below is a well enough intro.
https://www.iceflatline.com/2015/12/run-a-windows-application-as-a-service-with-srvany/
Download the tool kit here
https://www.microsoft.com/en-us/download/details.aspx?id=17657
Convert user application to Service and Register it using Regsvr32 or installutil.exe. It will start the service using SYSTEM user account. Which is a high privilege account.
Note : You can`t run any Window based application. Even a Message only window.

Automated Updating of a Program via powershell

I am trying to updating a software that is company wide. When the update is applied to the server, the client machines recognize they need an update and ask if you wish to update or not. To update, the user would need to run as admin, which is'nt an option in this case.
We would like to automate this process using powershell, using the Invoke-Command feature. For the most part, the only thing that the update does is copy new files to the programs folder, which we have achieved with robocopy. However, there is one registry key that needs to be added in multiple locations. There is a setup file that does this, but requires a user (with admin privileges) click a couple buttons, and we want this to be completely automated.
So I guess the short version of my question is, what is the best way to handle the registry changes that setup.exe does? It would be nice if there was a way to invoke the script that the executable does.
As for my question, I solved the problem with a slightly diferent approach. (One that should have been tried initially)
When (ProgramName).exe is run, if it sees that it needs updated, it runs a program called (ProgramName).setup.exe with the parameters :
Client="Local folder" server="server location"
These parameters did NOT work from the command line, however, and so I ended up using a powershell script to make a scheduled task that ran (ProgramName).setup.exe with said parameters.
Another huge advantage to this was the fact that I could create an icon that allowed a regular user to run the scheduled task with admin privileges. I couldn't setup a shortcut directly, however, I wrote an AUTO-it Executable that would run the task as admin.
I hope someone can get some level of help out of this post!

MS Access 2007 error number: 2950 on Trusted Document?

In my *.mdb file I have a macro with an action that runs TransferText and gives me an Error Number of 2950 which according to Microsoft means that the document is not trusted, as far as I know it is trusted! It's located in the Trust Center Trusted Locations.
Is there an explanation point next to the Action causing this in the macro whose tooltip reads This action will not be allowed if the database is not Trusted.
I already tried running a compact and repair on it, what could be causing this?
Method 1: Enable the database for the current session
When you use this method, Access 2007 enables the database until you close the database. To enable the database for the current session, follow these steps:
On the Message Bar, click Options.
In the Microsoft Office Security Options dialog box, click Enable this content, and then click OK.
Note You must repeat these steps every time that you open the database.
Check here: https://support.microsoft.com/en-us/kb/931407
The problem can be as simple as not having first established an ODBC connection. I was getting this macro error (2950), and I ended up reading all the support threads, getting nowhere.
Then it struck me to install our ERP client first --which is the front-end for the database I was trying to access ...with Access.
Doing this established an ODBC configuration, so now when I opened my Access file, an ODBC prompt opened up before I even got to any of the functions that were triggering the error. In hindsight, I could have passed on installing the ERP client, and just went to the ODBC settings in Windows, and pointed a connection to the SQL table.
In my case there was a naming conflict. The macro in turn called a VBA function, and I had inadvertently named the function with the same name as the module it was in. They were both named "RobertsReport". Once I changed the name, everything worked as expected.
Tip: As troubleshooting steps, try running the macro solo by double-clicking on it. If you can, copy only the line that generates the error to a new macro to test it by itself, and of course include any preceding commands it may need to run properly. Once I did this it greatly narrowed down the problem until the lightbulb came on over my head.

Perforce auto sync

I work in a large evolving code base and use perforce to manage it. The problem is that I need to update it everyday and it takes a long time to do it. I am looking for ways to automate this process.
I first thought of writing an script and make it a scheduled task. I could not do it since running "p4 sync" gives me "p4 protect" related error. I don't have, and will not get, admin rights to server so I can't add myself to the protect table.
Since I can sync through P4V - perforce UI- I guess there should be a way to achieve this through custom tools or something similar.
Can you guys please provide pointers on how to approach this problem or if there is already a solution for it.
If 'p4 sync' gets a protect error in your script, but not when you use P4V, you most likely have the wrong environment in your script. P4PORT, P4USER, and P4CLIENT need to have the exact same settings in our script as they do in your P4V connection.