Bat-file prohibit user input - powershell

I'm making a .bat-file run a powershell script, and I need to make sure the script does not ask for user input og anything as such may halt the clean running of the script.
So my question's are:
How do I set input of all kind disabled?
Is there anything else I need to think about when running a powershell script remotely on a computer where the computers users shall have no interaction with the script what so ever?
Thanks
Have a good weekend! :)

Use the -NonInteractive switch on PowerShell.exe. From the usage info:
-NonInteractive
Does not present an interactive prompt to the user.
I've not used it myself so I can't say how well it works. YMMV. :-)

You can disable input with the NonInteractive switch but commands that prompts the user will still wait for input.
There are many cmdlets that prompts for confirmation. You can try and set the $ConfirmPreference preference variable to 'none' to override prompting.
Other than that, the best thing would be to test your script before you use it in production. That's the best way to find out if your scripts can hang waiting for someone to respond.

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'

Powershell script permission

I have a PS Script that I want to share it with some people. I only want people to be able to execute it, not viewing or modifying it. How would I do that?
Also assuming the people I share the script with know little about PowerShell and how to change execution policy to make it run what would be the best way to do it? I was thinking of making a bat file and set execution policy to unsigned because remotesigned doesn't execute files from the internet right? Would unsigned cause any problem for security since I only execute a single script?
Lastly, I will be storing some password with convertto/from securestring
Would it matter to setup securestring if my file can not be modified in question 1? What happens in the case if I forget my password while using securestring method?
You could download and install PowerGui, and then compile your .ps1 file to be a .exe however they would still be able to pull the contents of the file when they run it as it exports a copy to %temp%, but they would not be able to 'easily' modify the file. I say 'easily' because basically anything with computers can be performed with the will, time, and smarts to know what you're doing, and I don't know how secure this password or anything else needs to be.

Start batch through powershell without getting asked for administration rights

Is there a way to start a batch file with administration rights using Powershell? (I mean in a way that you won't have to click a yes start thing that pops up.)
If you
Disable UAC, or
have PowerShell already running with administrative privileges
The entire point of UAC and selective elevation is that it's a noticeable action.
you can set variable __COMPAT_LAYER=RunAsInvoker (though it will give you no admin permissions for some operations pop-up will not appear) or disable UAC

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!

Is there a way to automatically log onto a Windows XP machine at specific time?

How to set a specific logon time for a specific user on Windows XP?
For example, the computer is an "always on machine" and most of the time no one is logged on. I want to set a time for my user to log on, without someone actually having to be in front of the machine. I've tried a workaround with the built-in auto logon function (control userpassword2), but I still need a secure system as I'm not the only user, and would still want a password even if I was the only user.
I have searched for third party software, but to no avail. I've a good knowledge of batch and Python scripting and a little of Java, so any pointers with any of those would help.
How can it be "secure" if it logs you on without you being there? Surely it would be better for it to wait for you to type in a password (i.e. the login prompt)?
If you want a fast "startup" time, then you can lock the workstation (ctrl+alt+del to open the Task Manager and then click 'Lock'. This shows a dialog just like the login prompt, but when you enter your password you instantly continue your existing session). Or if you want to be more eco friendly, put your PC into Sleep mode, which most PCs will return from (again with an optional password prompt) in less than 5 seconds.
One way I just thought of: Run a Vnc Server, and hack into an open source viewer program. Then you can rig it with some code to pass keyboard commands back into the computer, maybe implement some screen scraping to make sure it only does it when appropriate.
But seriously, there has to be a better way. Why are you trying to do this?