I have a .bat file that runs a powershell script. This is it
powershell -Command "& {Enable-WindowsOptionalFeature -Online -FeatureName IIS-WebServerRole}"
I have done this before and if I wanted to add another command I would put a semicolon before the closing brace and continue the script.
This command however after completed requires that I choose wheter or not to restart the computer with a Y or N.
Does anyone know how to pass this in an argument or avoid this step all together?
Thanks in advance
Note: you have to run the .bat file as adminstrator
From Get-Help Enable-WindowsOptionalFeature -Full:
-NoRestart [<SwitchParameter>]
Suppresses reboot. If a reboot is not required, this command does nothing. This option will keep the
application from prompting for a restart or keep it from restarting automatically.
Add the -NoRestart flag to Enable-WindowsOptionFeature to suppress the reboot. If you do want to reboot after completion, use the command Restart-Computer.
Related
I've found a very strange issue, when in linux terminal I type "enable -n trap", it would disable the trap linux builtin command. But if I put it into a script like
#!/bin/bash
enable -n trap
and then run the script, there's no error but the command is also not disabled. Really appreciate if someone could share what is happening and how to run it in some file instead of directly in the terminal. Thank you!
The enable command only affects the current shell. When you run a script, that script is executed in a new process, so:
A new shell starts
The enable command runs and disables the trap command in that shell
The shell exits
If you want to affect the current shell, your only option is to source the script using the . (or source) command. If the script is named disable-trap.sh and is in your $PATH, you can run:
. disable-trap.sh
You can also provide a full path to the script:
. /path/to/disable-trap.sh
Sourcing a script like this is largely equivalent to typing the same commands in at the command line: it executes the instructions in the script in the current shell, rather than spawning a new process.
I have seen many ways to launch a script like putting it in profile.D, rc.local, or creating a auto start file but none of those launch the file in a visible window if at all. I need it to be in a visible window in Ubuntu. I need to do this because I am using several emulators to stream to different services, and I don't want to have to start the script on each manually.
I am using visual box for the emulator. The sh file is on a removable drive because it is an external file. I also need it to run as sudo.
Edit: I don't actually need it to run at startup. I just need to have the script run. I can probably just sleep really long for graphic to load.
Edit 2: So I created a service that launched a sh file in /usr/bin/ which was supposed to create a gnome-terminal window that ran my script. It ran, however It didn't create a visible window for some reason. I then tried to specify a display which caused gnome to freak out. Dbus was not launching correctly. another question stated that gnome would not work because of how it was designed and stated to use konsole instead. Konsole also stated that it could not connect to a display, giving a QXcbConnection error. Konsole does not have an option to specify display. I don't know what else to try
Edit 3: So I did the thing in the comment. And the service works. However it only works after I run the file that the service runs in usr/bin manually after every restart. The important parts of the file:
#!/bin/bash
sleep 60
ufw disable
ssh nateguana#$(hostname) -X
xhost +
*launch Gnome**only works after file ran manually*
I have also tried exporting DISPLAY, and changing users with su. I have not tried importing SSHD, as another question said to do, as I think that is only for non local connections. I have also tried every single arrangement of commands possible. Xhost errors stating that it is unable to open display "".
You can use gnome-terminal -e <command> to spawn a new bash terminal which runs the command.
You could use something like
gnome-terminal -e /path/to/bashfile
Bear in mind, this will end the terminal after the bash scipt is done executing.
To avoid this,in a newline add $SHELL to the end of your bash script.
PS: the -e argument is deprecated and might be removed in later versions
I run a PowerShell script form the PowerShell and whatever method I choose it always cleans my current PowerShell window which is pretty annoying. That doesn't happen with Linux PowerShell.
This is how I run a PowerShell script:
powershell.exe -File "/path/to/file.ps1
or
powershell -c ". /path/to/file.ps1"
No matter how I call the script, the current PowerShell window is cleaned and the only thing I can see is the output of the script that I called. All previously displayed text is deleted.
Maybe there are options to prevent this behavior?
See also my screenshots as a visual representation of the problem:
My PowerShell window before I ran the command -
My PowerShell window after I ran a command -
Adding -NoProfile solves the problem:
powershell -NoProfile -c 'Write-Host OK'
I am trying to use PowerShell to send a file from a Windows 10 dev box to a CentOS 7 server across a local area network.
When I run the following command from PowerShell in the Windows 10 dev box, Putty opens, but then closes before asking for password. And then the test file (index.html) is not present in the intended destination folder on the CentOS 7 server.
Start-Process 'C:\Program Files (x86)\PuTTY\pscp.exe' -ArgumentList ("-scp -pw password C:\projects\temp\junk\index.html some_user_name#192.168.1.5:/home/some_user_name/")
How can I diagnose what problem is causing this command to fail? And what specific steps need to be taken in order for the file to be successfully transferred from the devbox to the server?
I have double checked the obvious things, like the file paths, the user name, and the IP.
The above command is derived from this suggestion by #GrahamGold.
In general, there's nothing wrong with your command. It should work (if all arguments are correct).
To diagnose the problem, first test the command on Windows command-line (cmd.exe):
"C:\Program Files (x86)\PuTTY\pscp.exe" -scp -pw password C:\projects\temp\junk\index.html some_user_name#192.168.1.5:/home/some_user_name/
This way, the output is preserved and you will see, what went wrong (if anything).
The next step is to do the same in PowerShell script using call operator & (though the result should be the same):
& "C:\Program Files (x86)\PuTTY\pscp.exe" -scp -pw password C:\projects\temp\junk\index.html some_user_name#192.168.1.5:/home/some_user_name/
If that works, it's time to replace & with the Start-Process cmdlet, if you really want that. But in general, I do not see a point of that, unless you want to run the transfer asynchronously.
Though if you want to make you command portable, you need to add -hostkey switch.
See my answer to Using echo y as an automated response to a pcp hostkey prompt.
I try to do a migration to Windows Server 2008 and i am new with script concept.
I have a .sh file to launch which contains :
cd RADIOROOT/PLAYERS
killall player_1
su - robotstream --command='/RADIOROOT/PLAYERS/player_1 -t /RADIOROOT/PLAYERS/player_1.conf' & > dev/null
First, how can i launch this .sh file on my IIS ? Do i have to change it in a powershell script ?
I tried to change it to a .ps1 file with some changes but not working. Anyone know how to do it ?
Then, do you have any website about changing this sort of script in order to make it work ?
Thanks for your help.
Assuming everything is the same on the Windows server as it was on your *nix server (same paths, robotstream takes the same arguments, etc) this should do the same thing in PowerShell:
cd C:\RADIOROOT\PLAYERS
Stop-Process player_1
$command = "robotstream --command='/RADIOROOT/PLAYERS/player_1 -t /RADIOROOT/PLAYERS/player_1.conf'"
Invoke-Expression $command
Save that with a .ps1 extension, and then run it from a PowerShell console.
As far as a website about converting SH scripts to PowerShell, you'll need to learn Bash (so you can understand the script you're converting), and PowerShell (so you know how to write the new script). There are plenty of good websites out there for both. Just Google "powershell tutorial".
Hope that helps. Feel free to ask for clarification.