VS Code, Powershell and using user modules - powershell

Good day.
First of all, sorry about my English.
If you add the use of a custom module in the code
PowerShell VSCode
using module. \ modules \ lineNumber.psm1
it is loaded at the first start of the script and judging by what I see, the subsequent call of the script with "using module. \ modules \ lineNumber.psm1" does not load it.
Features arise when you make changes to this module, they are not perceived by the script that called the module until you restart the terminal.
Actually the question is, how to configure VS Code so that every script launch it does in a new terminal session?
Or any other workaround for this feature.
testmodule.psm1
class foo {
static [string] rv () {
return "b"
}
}
console:
PS C:\Repository\alHaos\sandbox> cd .\modules\
PS C:\Repository\alHaos\sandbox\modules> using module .\testmodule.psm1
PS C:\Repository\alHaos\sandbox\modules> [foo]::rv()
b
# here i change the line 'return "b"' in the file .\testmodule.psm1 to "return "c""
PS C:\Repository\alHaos\sandbox\modules> using module .\testmodule.psm1
PS C:\Repository\alHaos\sandbox\modules> [foo]::rv()
b
# but here still 'b'
then i restart console and gеt 'c'
PS C:\Users\alhaos> cd C:\Repository\alHaos\sandbox\modules\
PS C:\Repository\alHaos\sandbox\modules> using module .\testmodule.psm1
PS C:\Repository\alHaos\sandbox\modules> [foo]::rv()
c

To restart the PowerShell session each script run in Visual Studio Code, you can configure Create Temporary Integrated Console setting in PowerShell extension:
"powershell.debugging.createTemporaryIntegratedConsole": true

Related

Error running Metricbeat.exe commands in powershell

I am trying to install Metricbeat on a Windows 10 machine so we can start monitoring it. When I open Powershell and run the following commands:
PS > .\metricbeat.exe modules list
I get the error
I copied that command as is from the Metricbeat documentation. I have seen videos on youtube of people running similar commands successfully. Please, why am I getting that error and what can I do to get my metricbeat.exe powershell commands to work?
You're copying the command TOO literally.
you've entered in the prompt PS > .\metricbeat.exe modules list
where you should have entered .\metricbeat.exe modules list
the latter executes modules list action against an application named "metricbeat.exe" located in .\ which indicates the current directory.
the former executes a redirection > of the output of an application named PS or get-process with input of .\metricbeat.exe modules and an argument of list.
wherever you copied this command from intended "PS >" to represent the beginning of the prompt and you don't need to include it.
Just like the error says... :P

No stdout from executables after pinvoke of go c-shared dll via Windows Powershell

Here's a bizarre one - After pinvoke of a function from a golang c-shared dll via Powershell, subsequent executions of executables (e.g. hostname.exe, ipconfig, etc) show no output to stdout. This only happens when powershell is the one invoking the dll functions (python on Windows does not reproduce this issue).
What did I do?
I cross-compiled the simplest c-shared dll I could for Windows with one exported function (HelloWorld). I then invoked the dll from Powershell.
The go code was:
package main
import "C"
import (
"fmt"
)
//export HelloWorld
func HelloWorld() {
fmt.Println("Hello World")
}
func main() {}
The build command (from linux) used was:
GOARCH=amd64 GOOS=windows CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc go build -v -buildmode=c-shared -o helloworld.dll .
I then copied the resultant .dll to Windows and invoked the HelloWorld() function using the following Powershell script:
$signature = #"
[DllImport("c:\\cshared\\helloworld.dll")]
public static extern void HelloWorld();
"#
Add-Type -MemberDefinition $signature -Name World -Namespace Hello
[Hello.World]::HelloWorld()
What did I expect to see?
I expected the dll's HelloWorld() function to be invoked successfully (causing the text "Hello World" to be written to the console) and then I expected to be able to use my powershell prompt to execute further console commands such as "hostname" or "ipconfig" etc.
What did I see instead?
This is really odd. The HelloWorld() function is indeed executed successfully and I see the text "Hello World" on the console. However, after the function has been invoked, I am no longer able to see any console output from cmd.exe commands in my powershell window. For example, calling "hostname" just returns with no output and the same happens with "ipconfig".
e.g. initially hostname.exe works and provides output, but after executing the cshared dll I am unable to see any further output from hostname..exe on the console:
PS C:\cshared> hostname
nick-host
PS C:\cshared> hostname
nick-host
PS C:\cshared> .\helloworld.ps1
Hello World
PS C:\cshared> hostname
PS C:\cshared> ipconfig
PS C:\cshared> hostname
If I pipe the output of hostname.exe to a file (e.g. "hostname > out.txt") then I can confirm that the output is present and the command is succeeding (same happens if you pipe the executable commands to "Out-Host" cmdlet)....so it seems like executing the c-shared dll somehow causes stdout to be redirected or not be shown properly? or something about the encoding of the PS session to break?
I tried removing the fmt.Println from the go code to see if that helps, but the issues is still the same. I am also only seeing this with Powershell (if I do the same exact experiment but invoke the helloworld.dll from python on Windows, I do not see any issue in the shell afterwards).
Any ideas?
This has been identified by the console owner for Windows as a bug in golang itself. The go issue thread is here: https://github.com/golang/go/issues/44876

How would I make a custom Powershell Command?

I would like to make a powershell command, such as startcode that should go to a different folder, and run a python file. I want this command to be accessible from ANY directory that my powershell terminal is in. I made a powershell program with this so far:
function startcode() {
cd ~
cd Blah blah blah
echo 'Starting!!'
python hello.py
}
How would I make the command startcode available everywhere?

Is it possible to stop Julia from using Powershell on Windows 7?

My company uses CYCLANCE and CYCLANCEProtect is preventing me to run Pkg.build(). Is it possible to stop Julia from using Powershell on Windows 7 and use cmd instead?
Here is an example:
julia> Pkg.build("Gumbo")
INFO: Building Gumbo
CylancePROTECT Script Control has blocked access to this PowerShell script.
CylancePROTECT Script Control has blocked access to this PowerShell script.
====================================================[ ERROR: Gumbo ]=======
You could try to use cURL for downloading which is also available on windows (here are some installation instructions). Julia allows to override the download function by defining a function with the same name:
function Base.download(url::AbstractString, filename::AbstractString)
run(`curl -L -f -o $filename $url`)
filename
end
Once you paste this code in the julia REPL, julia should use this cURL download function (instead of PowerShell). If you want to make this permanent you can put this code in a file called .juliarc.jl (in the home directory, i.e. the output of homedir()).

interactive powershell from Cygwin

I cannot run PowerShell.exe interactively in a Cygwin rxvt or mintty terminal. Seems any session using a /dev/tty? or /dev/pts? pseudo terminal device. An instance using the junky windows console device /dev/console or /dev/cons? will work.
cygstart /bin/bash -li
launches the console version in the cruddy Windows Command Prompt which is the only place I can get an interactive PowerShell.
Works. Rxvt doesnt:
Nor does mintty:
I've tried all the echo -e | powershell.exe and powershell.exe </dev/null
I'm assuming when I see answers on Stackoverflow on this they are using Console's ... or am I missing something?
Why I cannot run PowerShell 2 from Cygwin? seems to run fine, just gets powershell v3 when he wants v2 ... wish I had that problem.
I've developed a powershell wrapper to call powershell scripts and commands from a Cygwin terminal session but cannot get the interactive option to work (if you give the wrapper no script or commands then you want to go interactive). see https://bitbucket.org/jbianchi/powershell/wiki/ for info on the wrapper script. It works for most powershell.exe calls and even acts like a "she-bang" if used in the first line of the ps1 script.
Today, typing powershell at a Cygwin bash prompt just works.
If you need to run powershell inside cygwin/babun, follow https://code.google.com/p/mintty/issues/detail?id=56#c64 . Bascilly, downloard or compile https://github.com/rprichard/winpty, copy it to your $PATH and then run
console.exe powershell
This also works with batch scripts that invoke powershell inside.
The solution I've found is to use http://sergeybelous.com/ (main site) Proxy32 proxywinconsole.exe program. If this program is in the path, my poweshell.bash wrapper will call it which will let you work interactively with PowerShell.
First Install cygwin in your system.
After that type bash on powershell terminal and you can access cygwin terminal.
PS C:\Users\username\1and1> bash
username#LWMT-14R25Q2:/mnt/c/Users/username/1and1$
I'm afraid I can't answer your question, but maybe I can help you a little further on your way with this:
I believe this is related to this issue, as discussed on the MinTTY Issue #56.
It goes into great detail as to how common cmd.exe shell applications work and interact, so much as they detail how common unix applications using their TTY abstraction layer work differently than Windows command line applications.
Script for running powershell on Cygwin minty:
blahblah#blahblahbin $ cat pwrshl
#!/usr/bin/bash if [[ ! -f "$1" ]] then
echo "Usage: $0 <PowerShellScriptFile>"
exit fi
echo "\n" | powershell -Command "& {$(<$1)}"