I'm trying to update the JVM Heap Size from Powershell by running this PS Script. It works on the local machine and persists as an env variable:
test.ps1
& cmd /c 'SETX _JAVA_OPTIONS "-Xms256m -Xmx256m"' | Write-Host
But when triggering this same test.ps1 script from a Remote Machine (through Nagios NRPE), the value of this env variable does not update though a success message is returned to the remote machine.
If running the Powershell command for setting the env variable (in a non-admin console), like below:
[Environment]::SetEnvironmentVariable("Test3", "test string", "Machine")
I'm getting this error:
Exception calling "SetEnvironmentVariable" with "3" argument(s): "Requested registry access is not allowed."
So how to trigger this PowerShell script remotely in an admin prompt mode?
You likely need to start the service that is running NRPE (NSClient++?) with an elevated account.
Related
I have a Windows Client machine and I would like to invoke a powershell script on that client from my local machine i.e. Mac.
I have ssh on Windows via freesshd.
Also, i need to provide password via script hence I ended up using sshpass.
From my Mac Terminal, while the command:
sshpass -v -p xxx ssh administrator#x.x.x.x "powershell.exe dir"
runs perfectly and returns the content of whatever directory the shell lands in, i am unable the get the same result from inside a shell script.
The simple script is as below:
cmd='sshpass -v -p xxx ssh administrator#x.x.x.x "powershell.exe dir"'
echo `$cmd`
What I get is:
Unable to execute command or shell on remote system: Failed to Execute
process.
I have tried various permutations of " and ' to get the correct command string but to no avail.
What is wrong here?
I have created a PowerShell script for changing password for Windows services.
Using sc.exe in code for the same.
When I execute the script it changed password in few servers but failed for others.
Next, I executed the command directly on the servers to check. The command is not returning anything.
On servers where it was successful, it will return a success message or some error if the password in not changed or if command not proper.
Also, same command is used with same values in all servers. Following is the command:
sc.exe config "$Service" obj= "$UserName" password= "$Password"
I am trying to run a VBScript via cscript.exe within PowerShell (by invoking command on a remote server).
It keeps erroring out with a permissions error.
Below is the command that is failing.
Invoke-Command -Computer testserver1 -ScriptBlock {
Set-Location 'C:\Test1\Test2' |
cscript.exe -NoLogo test_v1.03.vbs /anlyze:false /reboot:false
}
In above command I'm invoking a command to change the directory to where the VBScript is located and then run the VBScript via cscript.exe.
When I run the same command locally on the server in testserver1 it works fine, but when I invoke the same command on remote server, VBScript output file gives me a permission denied error.
Error Report 2 - 70 Permission denied
My user has full access on remote servers and the local server from where I am invoking the command.
Is it cscript.exe or VBScript that is causing issue to me?
I have a bat file that tries to stop a service on a remote machine, the file contains the following
psservice \\remoteServerName -u domainName\userName -p password stop serviceName
where userName is a name of a user with Admin privileges on the remote machine.
If I run the last line using cmd then the requested service does stop.
I, however, run the bat file using psexec, since it contains more operation than just stopping the service. This is where my problem occurs:
If I run
psexec -u domainName\userName -p passsword batFilePath
the cmd window seems to get stuck.
But if I run
psexec batFilePath
then the psservice executes correctly.
My problem is that I need the user "domainName\userName" to be able to run the other commands in the bat file.
What can I do ? Why does the cmd got stuck when I gave psexec the parameters of the userName and password ?
Situation: Running the bat file on windows machine:
1. When I double click the bat file: Bat running is failed.
2. When I right click on bat file and run as administrator: Bat run is successful.
Now I have to run this bat file successfully from remote machine.
What I did:
1. Installed freeSSDd on remote machine and configured administrator user on freeSSHd to access shell and SFTP.
2. Now I am able to login to the remote machine using putty.
Problem:
I am not able to run the bat file successfully. How can I achieve this?
I also used runas /savecred /user:administrator C:/install.bat, but It didn't helped.
There is a way to get this working without any 3rd party software.
You have to create a task on the remote machine using the windows task scheduler which simply executes the desired command. There is an option where you can tell the scheruler to run a bat with a specific account. Enter an admin account and the password and check the "run with highest privileges" box. Leave "Triggers" empty, go to "Settings" and check the "Allow task to be run on demand" box. That's it!
Now when you want to run your file from a different location do
SCHTASKS /RUN /S <RemoteServerName> /U username /P password /TN "<task name>"
If you don't want to enter username and password each time you can adept the user policy (e.g. add the calling machine to the trusted list of the server).
If you have installed an ssh daemon, then you can run your BAT in a remote shell, but you remote shell may open up in something other than CMD.COM. I use cygwin to set up sshd and then from a remote machine, if I ssh in to run a command, it is using cygwin's bash. I can run a BAT file, but need to call CMD first:
ssh WINDOWS_SERVER "cmd /C D:\PATH_TO_BAT\BATCHFILE.BAT"
But there are some pieces missing here. I looked briefly at the Freesshd page and saw only graphical interfaces. Does freesshd support remote command execution, or just secure fire transfer? And what sort of shell get executed on the windows server when you run it?
cygwin is an entire Linux subsystem that runs under Windows and includes an sshd server, but might be a bit much for someone starting out: https://cygwin.com/
\n makes a powershell remote server that listens on port 22 (ssh) and dumps you into a powershell prompt, you can then use my steps above to call CMD from powershell, versus a bash shell.
http://www.powershellserver.com/