Executing PowerShell scripts via python - python-2.7

I have this PowerShell script and I have manually executed this
powershell -Command "C:\shellcommand.ps1"
I am writing it as subprocess.check_output("powershell -Command 'C:\shellcommand.ps1'")
I am getting result as C:\\shellcommand.ps1\r\n.
Actually I am trying to run a batch file from the script. Manually it is running.
Could you please help me how I can able to call this in python

Parameter -Command is for cmdlets, and -File is for scripts.
Try This:
powershell -ExecutionPolicy ByPass -File"C:\shellcommand.ps1"
I think it will work for you.

Related

Install msi file using powershell script

I am new in power shell. i need to install file using powershell. My msi file location is C:\Amazon\AWSCLIV2.
I have tried with the powershell script
Start-Process C:\Amazon\AWSCLIV2.msi
msiexec /i "C:\Amazon\AWSCLIV2.msi" /qn+
When i executing the above script gets installation windows. ie; click to next to install(Displayed window) the above msi file(i need to remove this window). but actually i want, installation should works automatically when executing powershell script.
So i need to execute installation using powershell script.
Can you try this:
$arguments = "/i `"C:\Amazon\AWSCLIV2.msi`" /quiet"
Start-Process msiexec.exe -ArgumentList $arguments -Wait

Unable to get AWS powershell comands working in Windows-10

I want to run some AWS commands using PowerShell scripts in windows. I went ahead and ran this below command.
Install-Module AWSPowerShell
The module got installed without any error. But I'm getting below error when I try to do "Set-AWSCredentials"
Can anyone please help me understand what I'm doing wrong here?
I think you should try to import the module installed. Almost you can use the following command Get-Module to check if the module is imported and installed in the current session of powershell execution.
Import-Module AWSPowerShell
Get-Module -ListAvailable
adding -Force solved it for me where "Import-Module AWSPowerShell" still showed no effect.
Import-Module AWSPowerShell -Force

Setup git via windows docker file

I write Dockerfile which is based on windowsnanoserver. I need to add to this image git. In order to achieve it I did the following:
RUN Invoke-WebRequest 'https://github.com/git-for-windows/git/releases/download/v2.12.2.windows.2/Git-2.12.2.2-64-bit.exe'
RUN Invoke-Expression "c:\Git-2.12.2.2-64-bit.exe"
But when I execute this lines via docker build, I receive following error message:
Invoke-Expression : The term 'c:\Git-2.12.2.2-64-bit.exe' 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.
I realize that this error message indicates that due to console nature of windows docker images I'll not be able to execute GUI installers. Unfortunately git doesn't have console installer. Chocolatey works fine under windowsservercore image but doesn't work at windowsnanoserver. In order to install git for windowsnanoserver I have idea to repeat in Dockerfile commands from chocolatey git installer which is fine for me, but still I'd like to know is there any simpler way to install git on windowsnanoserver?
I've solved issue with GUI through usage of MinGit and by putting information about mingit into environment/path variable. I've used following approach:
RUN Invoke-WebRequest 'https://github.com/git-for-windows/git/releases/download/v2.12.2.windows.2/MinGit-2.12.2.2-64-bit.zip' -OutFile MinGit.zip
RUN Expand-Archive c:\MinGit.zip -DestinationPath c:\MinGit; \
$env:PATH = $env:PATH + ';C:\MinGit\cmd\;C:\MinGit\cmd'; \
Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\' -Name Path -Value $env:PATH
You are correct, both Windows and Linux containers generally focus on running headless applications (i.e. without GUI).
It sounds like you want to create a container image based on the nanoserver image that has git?
Chocolatey is a great idea.
If you give me the broader context of your goals I can help you further.
Cheers :)
Installing to the docker image using Chocolatey worked for me as per this image: ehong
I addeded these lines to my Dockerfile:
ENV ChocolateyUseWindowsCompression false
RUN powershell Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force
RUN powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
RUN choco install git.install -y --no-progress
Call the git.setup.exe installation file with the parameters /? to list all possible switches.
To run a silent installation:
git.setup.exe /VERYSILENT /NORESTART /NOCANCEL /SP- /CLOSEAPPLICATIONS /RESTARTAPPLICATIONS
To do a customized installation:
run manually git installation with the parameter /SAVEINF="filename"
e.g:. git-2.xx.exe /SAVEINF="filename"
And then to repeat the installation with /LOADINF="filename"
e.g.: git.setup.exe /VERYSILENT /NORESTART /NOCANCEL /SP- /CLOSEAPPLICATIONS /RESTARTAPPLICATIONS /LOADINF="filename"
It's documented on:
Git: Silent-or-Unattended-Installation
You can download and use the Git Thumbdrive edition:
https://git-scm.com/download/win
look for the link under:
Git for Windows Portable ("thumbdrive edition")
E.G.: https://github.com/git-for-windows/git/releases/download/v2.23.0.windows.1/PortableGit-2.23.0-64-bit.7z.exe
Based on the answer of #Mariusz, the following lines install git into Windows image
# copy inf file
COPY resources/git-install.inf c:\git-install.inf
# get Git install file
RUN Invoke-WebRequest 'https://github.com/git-for-windows/git/releases/download/v2.30.1.windows.1/Git-2.30.1-64-bit.exe' -OutFile 'git.exe'; `
# install Git
Start-Process "c:\git.exe" -ArgumentList '/SP-', '/VERYSILENT', '/NORESTART', '/NOCANCEL', '/CLOSEAPPLICATIONS', '/RESTARTAPPLICATIONS', '/LOADINF=git-install.inf' -Wait -NoNewWindow; `
# delete files
Remove-Item -Force git-install.inf; `
Remove-Item -Force git.exe;

aws-cli 1.2.10 cron script fails

I have a crontab that fires a PHP script that runs the AWS CLI command "aws ec2 create-snapshot".
When I run the script via the command line the php script completes successfully with the aws command returning a JSON string to PHP. But when I setup a crontab to run the php script the aws command doesn't return anything.
The crontab is running as the same user as when I run the PHP script on the command line myself, so I am a bit stumped?
I had the same problem with running a ruby script (ruby script.rb).
I replace ruby by its full path (/sources/ruby-2.0.0-p195/ruby) and it worked.
in you case, replace "aws" by its full path. to find it:
find / -name "aws"
The reason it's necessary to specify the full path to the aws command is because cron by default runs with a very limited environment. I ran into this problem as well, and debugged it by adding this to the cron script:
set | sort > /tmp/environment.txt
I then ran the script via cron and via command line (renaming the environment file between runs) and compared them. This led me to see that I needed to set both the PATH and the AWS_DEFAULT_REGION environment variables. After doing this the script worked just fine.

Run Powershell script as a service, watching log files and reporting via email

I have some log files that I am watching, I need to do this using PowerShell since its a windows server. the command I am using to do this is:
Get-Content System.log -wait | {$_-match "some regex"}
I am trying to run this in a script and make it email me if it found any changes to the log file that follow the regex i specify. This is easily doable using bash and cron. I am not sure what's the equivalent in PowerShell. and what return code I need to look for to know if it found anything?
have you tried
Get-Content System.log -wait | {$_-match "some regex"} | foreach { send-email -message $_ }
I don't know if the result of the where will be piped until get-content -wait exits but give it a go.
As for running it as a service you can call a powershell script from FireDaemon or SvrAny but you could also use a schedule task to keep restarting the script.