powershell command doesn't run successfully via ruby interpreter - ruby

I was trying to run some commands inside a powershell script. The scrpt execution doesn't show any error. Script content is like:
Export-PfxCertificate –Cert $selectedcert_CA –FilePath "C:\\Windows\\System32\\CA_cert.pfx" -Password $SecurePassword -Verbose -force
But when I execute the powershell script from ruby as:
powershell.exe -file file.ps1 arg1 arg2
It displays file is created on path but I don't see any file created. If I run the same command separately (not in script just as a powershell command), it creates the file perfectly.
Is there any rights issue or I need to run the script with some permissions.

This snippet will run the powershell console with administrative rights
PS> Start-Process powershell -Verb runAs

Related

How do I run bash commands under windows server 2019 with powershell remoting?

I have build a powershell script to run commands remotely. Nothing really to it, just open a session, invoke-command, do some logging and exit. That script is called remoteExecute.ps1
It is working fine as intended when I run it the outputs are what to be expected.
I'm having trouble running shell scripts using bash command. I have a server on which I installed WSL2 Linux distribution Ubuntu 20. When I log in on the server I can open a terminal window and run the bash command to execute a shell script without issues.
BUT, when I call this remotely from the powershell script it returns an error:
PS D:\batch> .\remoteExecute.ps1 -computer myserver -command "bash -c /mnt/c/somefile.sh"**
Opening new section on remote computer myserver
**The term 'bash -c myshellscript.sh' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.**
+ CategoryInfo : ObjectNotFound: (bash -c /mnt/c/somefile.sh:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
+ PSComputerName : myserver
Executed: 'bash -c /mnt/csomefile.sh'
Closed session
Powershell version used on both machines is v7.1 and I defaulted it to windows by setting this registry key
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon' -name Shell -Value 'C:\Program Files\PowerShell\7\PowerShell.exe -noExit'
What am I missing?

Can't execute AWS CLI command with user_data in Terraform

I am using Terraform to spin up an EC2-instance where I have defined some user_data. In the user_data I download AWS CLI and set up the config file and credentials file. I then want to grab a file from my s3 bucket. This is what I've written so far:
data "template_file" "scripts" {
template = <<EOF
<powershell>
$dlurl = "installpathtoawscli.msi"
$installerPath = Join-Path $env:TEMP (Split-Path $dlurl -Leaf)
Invoke-WebRequest $dlurl -OutFile $installerPath
Start-Process -FilePath msiexec -Args "/i $installerPath /passive" -Verb RunAs -Wait
mkdir C:\temp
New-Item -Name 'b.bat' -Path 'C:\temp' -Value 'aws s3 cp s3://mybucketname/myfile.extension C:\temp\extension > C:\temp\test.txt'
New-Item -Name 'a.bat' -Path 'C:\Users\Administrator' -Value 'mkdir C:\Users\Administrator\.aws
echo [default]>> C:\Users\Administrator\.aws\credentials
echo aws_access_key_id = MY_ACCESS_KEY>> C:\Users\Administrator\.aws\credentials
echo aws_secret_access_key = MY_SECRET_KEY>> C:\Users\Administrator\.aws\credentials
echo [default]>> C:\Users\Administrator\.aws\config
echo region = MY_REGION>> C:\Users\Administrator\.aws\config
echo output = MY_OUTPUT>> C:\Users\Administrator\.aws\config'
C:\Users\Administrator\a.bat
C:\temp\b.bat
</powershell>
EOF
}
b.bat is executed and a txt file is created, however, it is empty and no file is grabbed from the s3 bucket. If I try to execute it manually (by RDPing to the instance) it grabs the file. Any ideas why this is happening?
I found the solution after several days of trying.
The AWS.exe location was not being registered in the PATH environment yet. Restarting the command prompt would do the trick, however, Terraform can't do this, thus explaining why it worked when I did it manually.
I had to provide the full path for AWS.exe in order to make it work e.g. C:\Program Files\Amazon\AWSCLI\bin\aws.exe or C:\Program Files\Amazon\AWSCLIV2\aws.exe depending on the version of AWS CLI.

RUN powershell failing in docker build on windows 2019 server

I am running on Windows 2019 server.
I am getting an error whenever I invoke powershell from a dockerfile on docker build
Error is..
---> Running in 6efa29aa8a4a
The command 'powershell -Command DIR' returned a non-zero code: 3221226505
Dockerfile..
# escape=` (backtick)
FROM mcr.microsoft.com/windows/servercore:ltsc2019
RUN DIR
RUN ["powershell", "-Command", "DIR"]
COPY ./ app/
WORKDIR app
CMD [ "someapp", "somearg" ]
I have tried replacing cmd with powershell via
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
RUN DIR
and the results are the same.
Thanks
Try running a dockerfile more like
FROM mcr.microsoft.com/windows/servercore/iis
RUN powershell -NoProfile -Command Remove-Item -Recurse
C:\inetpub\wwwroot*
WORKDIR /inetpub/wwwroot
COPY . /inetpub/wwwroot
maybe you gotta move your COPY command after the WORKDIR ?
Try different things out
Check to make sure you have update KB4532691 installed on your build machine (and host). The newest image of ltsc2019 (20/2020) has issues without it.
See https://hub.docker.com/_/microsoft-windows-servercore and https://support.microsoft.com/en-us/help/4542617/you-might-encounter-issues-when-using-windows-server-containers-with-t for more info

Can I just include a script in ebextensions instead of having to build one?

I have seen examples like this for creating and running a script via ebextensions config:
files:
"C:\\scripts\\whoami.ps1":
content: |
date | out-file -append c:\scripts\whoami.log
whoami | out-file -append c:\scripts\whoami.log
hostname | out-file -append c:\scripts\whoami.log
get-module | out-file -append c:\scripts\whoami.log
commands:
whoami:
command: powershell.exe -ExecutionPolicy Bypass -Command "C:\\scripts\\whoami.ps1"
ignoreErrors: false
waitAfterCompletion: 5
That's fine, but can I just include a script in ebextensions and refer to it instead of having to define its content in a config?
for example I would have
myapp
--.ebextensions
----myconfig.config
----myscript.ps1
And then "myconfig.config" would copy "myscript.ps1" somewhere and run it.
How can I do this?
Edit:
So I put this command in a config to see what the current path is:
commands:
whereami:
command: dir > c:\whereami.log
ignoreErrors: false
waitAfterCompletion: 5
Its c:\windows\system32 which makes sense because it runs in the context of SYSTEM.
Is there no self referential method/keyword I can use to point to a script I have located in the beanstalk project I upload?
The options that worked for me (thanks to littleforest).
Create a folder structure inside of the web deployment package and refer to it with container_commands:
package/myfolder/myscript.ps1
container_commands:
relativeprogrampath:
command: "myfolder/myscript.ps1"
Create it in a parent folder and refer to it:
example folder structure:
parentfolder/myfolder/myscript.ps1
parentfolder/package/<the web deployment package>
container_commands:
relativeprogrampath:
command: "..\myscript.ps1"
If you place the myscript.ps1 file in the .ebextensions folder next to the config files, you can run it from a container command like this:
container_commands:
00-myscript:
command: powershell.exe -ExecutionPolicy Bypass -Command ".\\.ebextensions\\myscript.ps1"
waitAfterCompletion: 0

How to run database scripts on database shell in jenkins?

I added an Execute Shell build step in my jenkins job.
I need to start Oracle NoSQL shell and run some DDLs.
I wrote shell script.
#Run Client
java -jar /root/software/kv-3.2.5/lib/kvcli.jar -host localhost -port 5000
#Run scripts
plan remove-table -name PersonnelUni1To1FK -wait
plan remove-table -name HabitatUni1To1FK -wait
When I excecute the same from my teminal, it's working fine. Becuase after 1st command database shell started and remaining scripts ran over that shell.
But build failed in Jenkins.
Jenkins job console output:
+java -jar /root/software/kv-3.2.5/lib/kvcli.jar -host localhost -port 5000
kv-> + plan remove-table -name PersonnelUni1To1FK -wait
/tmp/hudson2708562803834708095.sh: line 17: plan: command not found
Build step 'Execute shell' marked build as failure
Its trying to execute this all on my ubuntu shell not on database shell.
plan is a command to be executed by the shell, right? The error message just says that it doesn't find the command, so the problem could be the setting of the PATH variable. What happens if you specify the absolute path to plan in your shell script? Alternatively, make sure that the PATH variable on Jenkins is correct.
As a workaround,
I put all my scripts
plan remove-table -name PersonnelUni1To1FK -wait
plan remove-table -name HabitatUni1To1FK -wait
and many more...
in a file /home/dev/ons/oracle-kv-db.script
modified shell command for jenkins job
java -jar /root/software/kv-3.2.5/lib/kvcli.jar -host localhost -port 5000 < /home/dev/ons/oracle-kv-db.script
Now it does not need database shell explicitly.

Resources