Cannot install Visual Studio from PowerShell script - windows

I'm trying to install visual studio through PowerShell, works fine on local computer, but I keep getting errors when I run it on our AWS windows server 2012R2. I've attached my code and error below. Thank you
powershell script
error
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$here = pwd
$software = "Microsoft Visual Studio Installer";
$installed = (Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where { $_.DisplayName -eq $software }) -ne $null
#If VSCode was not installed before, it will be download required files and install it.
If(-Not $installed)
{
Write-Host "'$software' is NOT installed.";
wget https://aka.ms/vs/17/release/vs_community.exe -outfile “vs.exe”
.\vs.exe install --quiet --norestart
}
#If VSCode was installed before, it will try to update it to the newer version, if available.
#If no updates available, it will do nothing.
else
{
Write-Host "'$software' is installed."
if ( Test-Path -Path $here\vs.exe )
{
.\vs.exe update --quiet --norestart
}
else {
wget https://aka.ms/vs/17/release/vs_community.exe -outfile "vs.exe"
.\vs.exe update --quiet --norestart
}
}

You have used the option -outfile vs.exe but this is not how you tell wget to rename the downloaded file. What this actually does is the following:
downloads the file to vs_community.exe (because that's the filename in the original URL)
writes a log file to utfile
ignores the vs.exe parameter
The wget option to direct the downloaded content to a named file is actually -O, not -outfile (which is the equivalent of -o utfile, which writes the log to utfile).
To specify the correct output filename, use:
-O vs.exe
Or simply execute wget https://aka.ms/vs/17/release/vs_community.exe and then execute the downloaded file, which is named vs_community.exe.

Related

Plastic in Docker windows containers

I want to use plastic SCM in Docker windows containers.
I searched in Plastic and found Linux containers docker file
SO I want to try my own docker file for that I need to know the command to download the SCM installation file using PowerShell and install it without the GUI (arguments to pass for installation) via Powershell
This is the script I'm currently using for my windows image. I will warn you that it does take ~5-10 minutes to install during the build process, but everything works great besides that. It works pretty simply, it creates a temp folder and uses the URL for the download to download the installer there, then runs the installer, and finally deletes the temp folder.
#This installs plastic
$tempFolder = "C:\Temp"
$plasticURL = "https://www.plasticscm.com/download/downloadinstaller/10.0.16.5882/plasticscm/windows/client"
$installerName = "plasticinstalling.exe"
New-Item $tempFolder -ItemType Directory -Force -ErrorAction Stop | Out-Null
$installerLocation = (Join-Path -Path $tempFolder -ChildPath $installerName -ErrorAction Stop)
Invoke-WebRequest -UseBasicParsing -Uri $plasticURL -OutFile $installerLocation -ErrorAction Stop
Start-Process -FilePath $InstallerLocation -ArgumentList "--mode","unattended" -NoNewWindow -Wait -PassThru
Remove-Item -Recurse $tempFolder -Force -ErrorAction Ignore
Then, in my docker file I just call the script:
RUN powershell -Command C:\Scripts\installPlastic.ps1
Hope this helped and feel free to reach out with more questions.

Debugging through docker-compose stopped working

Created a small .Net 5.0 application in Visual Studio 2019 and added docker-compose container orchestration support to it using linux containers. I set the docker-compose project as the default startup application in Visual Studio, and everything was going great for two days until I decided to alter my configuration so my container would have a stable port number instead of it randomly changing.
I'm not sure what I did but but Visual Studio doesn't attach to the container anymore, and the browser window that normally opens when the container starts no longer does. The container is running but the app doesn't respond to the new port number I'm trying or the older port number either.
The code builds fine in VS, the Dockerfile builds successfully, and the docker-compose up command runs in Visual Studio just fine.
I have portainer running in my docker-compose file and I can see that the container is running but it is not producing any logs. I logged into to my running container using portainer and installed the procps package like so: apt-get update; apt-get -y install procps. When I run ps I see that my app is not actually running.
root#46c62bf07df3:/app# ps l
F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND
4 0 2195 0 20 0 3868 3280 do_wai Ss pts/2 0:00 bash
0 0 2216 2195 20 0 7556 1212 - R+ pts/2 0:00 ps l
Update: This continued to happen ever time a cow farted so I had to keep plugging away at the problem. I finally found this report that says its a known internal issue when docker-compose v2 is enabled.
The fix is to disable V2 using docker-compose disable-v2.
You can verify it's disabled with docker-compose --version.
They claim a real fix is in the works mid Oct-2021 and will be release soon. Also they claim Visual Studio 2022 preview already has the fix in it.
https://developercommunity2.visualstudio.com/t/Debugging-docker-compose-VS-cant-attac/1551330?entry=problem&ref=native&refTime=1636020527854&refUserId=09da1758-2dd4-4352-bba5-ea1f5e163268
I also wound up writing this powershell script to clean out all the garbage VS was caching so nothing stuck around.
$ErrorActionPreference = 'Stop'
# delete various folders from the root folder
if ((Test-Path -Path .vs)) {
Remove-Item .vs -recurse -force -Verbose
}
if ((Test-Path -Path obj)) {
Remove-Item obj -recurse -Verbose
}
if ((Test-Path -Path bin)) {
Remove-Item bin -recurse -Verbose
}
# delete various folders recursively
$currentfolder = Get-Location
Get-ChildItem -Path $currentfolder -Directory -Include bin, obj -Recurse | Remove-Item -Force -recurse -Verbose
Get-ChildItem -Path $currentfolder -File -Include docker-compose.dcproj.user -Recurse | Remove-Item -Force -recurse -Verbose
Get-ChildItem -Path $currentfolder -Directory -Recurse -Attributes H | Where-Object { $_.Name.Contains('.vs') } | Remove-Item -Force -recurse -Verbose
# delete all volumes except those with the keep label
#docker volume prune --filter "label!=keep" -f
Original answer
Figured out how to get it working again, but I still don't know why it happens and it appears to randomly fubar now and then.
I wound up creating another temp starter application and adding container orchestration to it so I could see if it would run and also
compare the files with my real application. The new temp app indeed ran successfully in docker-compose so I started looking at differences in various files
between the two apps. All of them were minor diffs, and even after I updated my real application to match the temporary app's files it still wouldn't run.
I noticed that in Visual Studio 2019 you can actually see details about your running docker containers, and also noticed that it was showing me both the temp
app and my real app that I cared about. So I went through all the container details for both and narrowed in on a label called com.microsoft.visualstudio.debuggee.arguments
that was clearly missing something in my real app's container.
The temp application had that label set to :
--additionalProbingPath /root/.nuget/packages "/app/bin/Debug/net5.0/Test.dll"
but my real app instead had this, which is missing the path to the assembly needed to start/debug my application.
--additionalProbingPath /root/.nuget/packages ""
Visual Studio creates a file obj\Docker\docker-compose.vs.debug.g.yml and passes it to docker-compose when you debug, and that label is missing the magic sauce to get my app up and running.
I don't know why Visual Studio is fubaring this file but it continues to randomly happen.
These are the steps that finally got it working.
Close all instances of Visual Studio
Manually delete the following folders
.vs
bin
obj
Manually delete all .user files in the project.
Open Docker Desktop and remove the docker-compose stack created by your project.
Remove the docker images for your project(s)
Load your project and try to run it again.
Visual Studio should regenerate the file mentioned above with the correct bits this time and should attach to the container.

Docker + Windows Container: Best practice to work with PowerShell

Recently I've been investing some time to dive from Docker on Linux with bash (usually using "eux" options) into Windows container with Powershell to gain the same experience in Windows.
However, I have a lot of trouble getting there. Let's begin with the troubling Dockerfile (which will likely be the base for most Windows containers using PowerShell):
FROM mcr.microsoft.com/windows/servercore:20H2-amd64
SHELL ["powershell.exe", "-NoLogo", "-NoProfile"]
# Set ExecutionPolicy = Bypass permanently
RUN Invoke-Command -ErrorAction Stop -ScriptBlock { `
Get-ExecutionPolicy -List ; `
Set-ExecutionPolicy -ExecutionPolicy Bypass ; `
Get-ExecutionPolicy -List `
}
# Install chocolatey
RUN Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
# Enable global confirmation and install general build tools
RUN Invoke-Command -ErrorAction Stop -ScriptBlock { `
$ErrorActionPreference='Stop' ; `
choco feature enable -n=allowGlobalConfirmation ; `
choco install --no-progress --no-color -y cmake --installargs 'ADD_CMAKE_TO_PATH=System' ; `
choco install --no-progress --no-color -y mingw ; `
choco install --no-progress --no-color -y make `
}
What I personally miss the most is the possibility to chain multiple commands, so I can avoid the creation of superfluous image layers and container restarts.
However, if one of the commands fails, the command won't stop even that I told Invoke-Command to stop on errors and within the script block I set the ErrorActionPreference variable.
Hope the main problem here is not mainly linked to the missing knowledge in PowerShell, but how PowerShell works in Windows containers.
Thanks!

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.

How to run jq from gitbash in windows?

I have gitbash in Windows. I am trying to run jq but its giving me error.
$ ./jq-win64.exe
jq
parse error: Invalid numeric literal at line 2, column 0
Intention: I want to use jq to parse json.
Easiest solution and always latest version:
run this curl in your gitbash:
curl -L -o /usr/bin/jq.exe https://github.com/stedolan/jq/releases/latest/download/jq-win64.exe
or manually save the jq-win64.exe in link above as jq.exe to your /usr/bin (which is in your git bash installation folder)
(if you are behind a proxy add the -x proxyhost:port)
Using jq-win64.exe from github.com/stedolan/jq/releases, I get
vonc#voncav MINGW64 /d/prgs/dl
$ ./jq-win64.exe --version
jq-1.6
vonc#voncav MINGW64 /d/prgs/dl
$ echo '{"foo": 0}' | ./jq-win64.exe .
{
"foo": 0
}
So it does work, but it then depends on the json document you are parsing with it.
If that json document is not well-formed, that would generate the error you see.
In your bash session, you can define (or add to your ~/.bashrc) an alias:
alias jq=/path/to/jq-win64.exe
That way, you don't need to use ./jq, but directly jq.
$ echo '{"foo": 0}' | jq
In my case:
vonc#voncav:/$ alias jq=/mnt/d/dwnl/jq-win64.exe
vonc#voncav:/$ echo '{"foo": 0}' | jq
{
"foo": 0
}
I hate answers that say you need another to use another tool to download, but using https://chocolatey.org/ made this incredibly simple for me.
From an elevated command-prompt, run:
choco install jq
jq magically works from bash going forward.
Instead of using chocolatey (with elevated rights) you could also use scoop.sh:
scoop install jq
Below steps worked for me in git bash on windows 10
curl -L -o jq-win64.exe https://github.com/stedolan/jq/releases/latest/download/jq-win64.exe
export PATH=$PATH:"/C/Users/devops/Downloads/jq-win64.exe
jq --version
jq-1.6
For windows Powershell && cmd
Download executable -> jq 1+ executables for 64-bit
Create folder in Program Files -> JQ
Copy .exe into program folder
Set environment variable to that folder
Restart terminal
Set alias
Set-Alias -Name jq -Value .\jq-win64.exe
Run command
echo '{"foo": 0}' | jq
Output
{
"foo": 0
}
enjoy!
I just downloaded the binary to %HOMEPATH%/bin/jq-win64 and it worked right away via jq
In my case, I have fixed it in Windows10 while practicing a scenario of "Techworld with Nana" course, as per the below steps:
1) open "cmd" prompt (Run as administrator)
2) curl -L -o /usr/bin/jq-win64.exe
Download it here
3) You can add C:\Program Files\Git\usr\bin path to your environment variables. If you follow this step, ignore the below steps.
[OR]
3) Go to the directory where jq is downloaded: C:\Program Files\Git\usr\bin and copy the file to C:\ drive
4) Add the path C:\ to "path" environment variable. This PC-->Properties-->Advanced system settings --> Environment Variables -->Edit-->New. Then add the path (where you copied jq.exe, i.e., C:) and Save it.
5) Now open a new "cmd" prompt, jq --version

Resources