PowerShell script on downloading and installing zabbix agent silently - windows

I am trying to write a script that will download a .msi and then install it silently.
This is my 1st time ever trying to use PowerShell would be amazing if someone can assist me.
Thank you very much in advanced
- name: Install Zabbix
win_shell: |
wget "https://cdn.zabbix.com/zabbix/binaries/stable/6.2/6.2.3/zabbix_agent2-6.2.3-windows-amd64-openssl.msi" -UseBasicParsing -OutFile "C:\Users\Administrator\downloads\zabbix_agent2-6.2.3-windows-amd64-openssl.msi"
Start-Process "C:\Users\Administrator\downloads\zabbix_agent2-6.2.3-windows-amd64-openssl.msi /S"

Related

Running a psm1 PowerShell script in Command Prompt (cmd.exe)?

I know nothing of PowerShell, but I wanted to install this: https://www.powershellgallery.com/packages/lolcat/
So, I start PowerShell as administrator, and:
PS C:\WINDOWS\system32> Install-Module -Name lolcat NuGet provider is required to continue
PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories. The NuGet
provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or
'C:\Users\Me\AppData\Local\PackageManagement\ProviderAssemblies'. You can also install the NuGet provider by running
'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force'. Do you want PowerShellGet to install and import
the NuGet provider now?
[Y] Yes [N] No [S] Suspend [?] Help (default is "Y"): y
Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its
InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from
'PSGallery'?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): y
PS C:\WINDOWS\system32> lolcat
Usage: lolcat [OPTION]... [FILE1[, FILE2[, ...]]]
...
Nice, it works. So first I find where is the newly installed script:
PS C:\WINDOWS\system32> (Get-Module lolcat).Path
C:\Program Files\WindowsPowerShell\Modules\lolcat\1.0.7\lolcat.psm1
Ok, so now I want to try calling this from cmd.exe:
C:\Users>PowerShell.exe -File "C:\Program Files\WindowsPowerShell\Modules\lolcat\1.0.7\lolcat.psm1"
Processing -File 'C:\Program Files\WindowsPowerShell\Modules\lolcat\1.0.7\lolcat.psm1' failed because the file does not have a '.ps1' extension. Specify a valid Windows PowerShell script file name, and then try again.
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell https://aka.ms/pscore6
Nope, does not work.
Is it possible to call this PowerShell script from cmd.exe - and if so, how?
The error is due to the fact that the -File parameter from the powershell.exe excepts a .ps1 file.
If you want to run C:\Program Files\WindowsPowerShell\Modules\lolcat\1.0.7\lolcat.psm1 from cmd, make a .ps1 script where you can write something like
Import-Module lolcat
# now you have all the functions from the lolcat module loaded into this PowerShell session
# do stuff
And then call this script from cmd.
The difference between .ps1 and .psm1 is explained here.
Eh, turned out to be less complicated then I thought - since it is a "module", just use the module name:
C:\Users>PowerShell.exe lolcat
Usage: lolcat [OPTION]... [FILE1[, FILE2[, ...]]]
...

Chocolatey installs locally but not in a docker build (but it did last week) - Why?

I am trying to use chocolatey to install packages into a windows container. I actually just wanted to change one of the packages, rebuild and push and now, suddenly, it doesn't work. When I test it on the host (run the same command as the container) it works just fine. It has some kind of an issue with that container installation since last build. I can only suspect the base image changed. But, maybe not
On the host:
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
Getting latest version of the Chocolatey package for download.
Getting Chocolatey from https://chocolatey.org/api/v2/package/chocolatey/0.10.15.
Downloading 7-Zip commandline tool prior to extraction.
Extracting C:\Users\ADMINI~1\AppData\Local\Temp\chocolatey\chocInstall\chocolatey.zip to C:\Users\ADMINI~1\AppData\Local\Temp\chocolatey\chocInstall...
Installing chocolatey on this machine
Creating ChocolateyInstall as an environment variable (targeting 'Machine')
Setting ChocolateyInstall to 'C:\ProgramData\chocolatey'
WARNING: It's very likely you will need to close and reopen your shell
before you can use choco.
Restricting write permissions to Administrators
We are setting up the Chocolatey package repository.
The packages themselves go to 'C:\ProgramData\chocolatey\lib'
(i.e. C:\ProgramData\chocolatey\lib\yourPackageName).
A shim file for the command line goes to 'C:\ProgramData\chocolatey\bin'
and points to an executable in 'C:\ProgramData\chocolatey\lib\yourPackageName'.
Creating Chocolatey folders if they do not already exist.
WARNING: You can safely ignore errors related to missing log files when
upgrading from a version of Chocolatey less than 0.9.9.
'Batch file could not be found' is also safe to ignore.
'The system cannot find the file specified' - also safe.
chocolatey.nupkg file not installed in lib.
Attempting to locate it from bootstrapper.
PATH environment variable does not have C:\ProgramData\chocolatey\bin in it. Adding...
WARNING: Not setting tab completion: Profile file does not exist at
'C:\Users\Administrator\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1'.
Chocolatey (choco.exe) is now ready.
You can call choco from anywhere, command line or powershell by typing choco.
Run choco /? for a list of functions.
You may need to shut down and restart powershell and/or consoles
first prior to using choco.
Ensuring chocolatey commands are on the path
Ensuring chocolatey.nupkg is in the lib folder
same host, but this time, in a Dockerfile
PS C:\Users\Administrator\example> cat Dockerfile
# escape=`
ARG SDK_VERSION=4.8
# Use the latest Windows Server Core image with .NET Framework 4.8.
FROM mcr.microsoft.com/dotnet/framework/sdk:${SDK_VERSION}
# Disable Healthcheck
HEALTHCHECK NONE
RUN Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
RUN choco install adoptopenjdk8openj9jre -y
RUN choco install nuget.commandline -y
RUN choco install yarn -y
RUN choco install gitversion.portable -y
RUN choco install sonarscanner-msbuild-net46 -y
CMD ["powershell.exe", "-NoLogo", "-ExecutionPolicy", "Bypass"]
PS C:\Users\Administrator\example> docker build -t example .
Sending build context to Docker daemon 4.096kB
Step 1/10 : ARG SDK_VERSION=4.8
Step 2/10 : FROM mcr.microsoft.com/dotnet/framework/sdk:${SDK_VERSION}
4.8: Pulling from dotnet/framework/sdk
65014b3c3121: Already exists
b5405b758079: Pull complete
ac56c610af03: Pull complete
d8d61c2ababf: Pull complete
0ed57babb001: Pull complete
c8a0f45b3421: Pull complete
75cb34f91825: Pull complete
1949a0bb0ffb: Pull complete
316fdf3fc0fa: Pull complete
42b0925a3e5c: Pull complete
c5e384761600: Pull complete
f65f633b9854: Pull complete
bd885be04626: Pull complete
179844769b6b: Pull complete
c037183d4738: Pull complete
Digest: sha256:0008d5d893924c10ec01b1619a009c57116f9a943fe43ca772af544c75c9a83a
Status: Downloaded newer image for mcr.microsoft.com/dotnet/framework/sdk:4.8
---> 99ad7e1e8763
Step 3/10 : HEALTHCHECK NONE
---> Running in f679ce4e5789
Removing intermediate container f679ce4e5789
---> a08984a9afe8
Step 4/10 : RUN Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
---> Running in 656ed514a137
Getting latest version of the Chocolatey package for download.
Getting Chocolatey from https://chocolatey.org/api/v2/package/chocolatey/0.10.15.
Downloading 7-Zip commandline tool prior to extraction.
Extracting C:\Users\ContainerAdministrator\AppData\Local\Temp\chocolatey\chocInstall\chocolatey.zip to C:\Users\ContainerAdministrator\AppData\Local\Temp\chocolatey\chocInstall...
Unable to unzip package using 7zip. Perhaps try setting
$env:chocolateyUseWindowsCompression = 'true' and call install again. Error:
7-Zip signalled an unknown error (code -1073741511)
At line:220 char:15
+ ... default { throw "$errorMessage 7-Zip signalled an unknown error (co ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (Unable to unzip...de -1073741
511):String) [], RuntimeException
+ FullyQualifiedErrorId : Unable to unzip package using 7zip. Perhaps try
setting $env:chocolateyUseWindowsCompression = 'true' and call install aga
in. Error: 7-Zip signalled an unknown error (code -1073741511)
The command 'powershell -Command $ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue'; Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))' returned a non-zero code: 1
PS C:\Users\Administrator\example>
There have been a few issues from TLS changes to changes to the microsoft base images that have caused me some problems with 7zip and choco over the last 3 weeks. I've got my base images working now by adding the following:
RUN $text = '[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls13 -bor [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Ssl3 -bor [Net.SecurityProtocolType]::Tls'; \
$text | Set-Content 'C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1';
RUN powershell -Command Set-ExecutionPolicy Bypass -Scope Process -Force; \
iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')); \
powershell -Command refreshenv; \
cinst -y --allow-empty-checksums 7zip.install make nuget.commandline --no-progress;
However I am still experiencing some issue in other images with the reported error: "7-Zip signalled an unknown error (code -1073741511)", however no doubt they boil down to the same thing. Again, if I exec in to the running container, I am able to manually run choco and install 7zip and even access the "7z" commands. I tried committing the container once I made the changes, but still got this issue.

Very slow RUN on docker on windows containers

I installed docker on a Windows 10 machine via chocolatey
choco install docker-desktop
Then I have built this image
FROM mcr.microsoft.com/windows/servercore:ltsc2016
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop';"]
RUN Invoke-WebRequest -UseBasicParsing https://chocolatey.org/install.ps1 | Invoke-Expression
And it runs reasonably fast
Then I try to run this one
FROM chocolatey
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop';"]
RUN cinst -y netfx-4.7.2-devpack
After downloading the .net installer the process fails with timeout.
The last thing in the log is Installing dotnetfx...
No idea why and how to debug.
Anyone has the same issue? Where do I start debugging this issue?
I dont have a definitive answer for this but my current workaround is to use the sdk base image from microsoft from https://hub.docker.com/_/microsoft-dotnet-framework-sdk/

Installin RabbitMQ server from command line

How to install RabbitMQ server https://www.rabbitmq.com/install-windows.html from command line in silent mode?
Thank you!
We've done this before by just downloading the zipped version then extracting it and running the relevant commands:
Invoke-WebRequest https://www.rabbitmq.com/releases/rabbitmq-server/v3.5.4/rabbitmq-server-windows-3.5.4.zip -OutFile C:\downloads\rabbitmq.zip
7z x C:\downloads\rabbitmq.zip -oC:\rabbitmq -aoa
# run RabbitMQ
Start-Process -FilePath C:\rabbitmq\rabbitmq_server-3.5.4\sbin\rabbitmq-server.bat
If you want it installed as a service you can use the rabbitmq-service.bat script - https://www.rabbitmq.com/rabbitmq-service.8.html
rabbitmq-service install

New-SShSession command not found in jenkins powershell

I'm using Windows Jenkins and installed the windows powershell plugin. from Jenkins I'm trying to connect to Linux instance and execute some commands in linux server.
> New-SshSession -ComputerName 10.0.0.xx -Username username-Password
> jenkins#123 Invoke-SshCommand -ComputerName 10.0.0.xx -Command "cd
> docker_CIServiceApp ; unzip prod.zip -d prod/"
But getting the error message from jenkins job as
New-SshSession : The term 'New-SshSession' 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.
The same command works in Windows Powershell terminal. What will be the issue?
The reason for it might be that the Jenkins was started before SSH.NET plugin was installed. It modifies the environment variable and it requires process restart. Try restarting Jenkins process. Alternatively, it might be that the module is not loaded, try loading it first: Import-Module SSH-Sessions.
So, apparently you have installed a package in Visual Studio solution. Visual studio takes care of package download for you (restoration). It gets downloaded into packages folder under your solution. When you have no Visual Studio, like when using Jenkins and MS Build directly, you have to download package manually - get nuget.exe and run nuget restore your_solution.sln, this will then download the package under "\packages". Now you will have to import module, using the "\packages" path, probably.
your error indicates tha the module isn't installed:
PS /home/thufir/powershell>
PS /home/thufir/powershell> Install-Module -Name SSHSessions
Untrusted repository
You are installing the modules from an untrusted repository. If you trust this
repository, change its InstallationPolicy value by running the Set-PSRepository
cmdlet. Are you sure you want to install the modules from 'PSGallery'?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help
(default is "N"):Yes
PS /home/thufir/powershell>

Resources