Console.WriteLine from with the Nuget Package Manager Console - visual-studio-2010

If I type the following code into a typical PS window, I am able to correctly get "asdf" in the output:
[System.Console]::WriteLine("asdf")
But if I do it in the NuGet Package Manager Console nothing is output. Can anyone tell me why?

Because NPM is not a console app. It is hosted inside Visual Studio and implements the PowerShell host interfaces to allow the PowerShell engine to display output to what is likely a WPF window.
To get output in NPM use:
Write-Host "asdf"
or simply
"asdf"
If not in a cmdlet, you can do something like this:
private void WriteHost(string message)
{
var runspace = Runspace.DefaultRunspace;
var pipeline = runspace.CreatePipeline("Write-Host '" + message + "'", false);
pipeline.Invoke();
}

Related

gitlab-runner for testing windows application with GUI

We want to automatize our integration tests for a Windows-MFC-application (Visual C++) with gitlab-ci/cd. We have set up a gitlab-runner (version 14.2.0) on Windows 10 with Powershell.
In the ci/cd script, we start our application with the following Powershell command. (line breaks included for readability)
- 'foreach ($test in $TestDirectories)
{
Write-Host "Running test $test.";
$proc = Start-Process "./Release/MyApp.exe" -argumentList "-TESTMODE $test" -PassThru;
Wait-Process -Id $proc.Id;
if ($proc.ExitCode -ne 0)
{
$ESC = [char]27;
$ExitCode = $proc.ExitCode;
Write-Host "$ESC[31mTest $test failed with code $ExitCode $ESC[0m";
exit 1;
}
}'
The app is running, but its behavior is very strange. Sometimes it is crashing, sometimes the tests get stuck. I have attached the Visual Studio debugger to the process, but I was not able to find the real problem with many effort. Always some new, changing errors.
I suspect a problem, that the app is started from the gitlab-runner service without a real graphical user interface. I cannot see its windows anywhere, but the application is listed in the task manager. Is it somehow possible to start the application in a "normal" manner? Can I see its window somehow?
I have tried to register the gitlab-runner with a user account
gitlab-runner.exe install -user MyUser -password xxx
and also with the system account (standard)
gitlab-runner.exe install
Also, setting the flag "Allow interact with desktop" in the service manager did not work.
But, if I start the upper commands from normal Powershell console, everything goes well.
Do you have some suggestions, how to handle windows application with GUI in gitlab-ci-cd-scripts?
My config.toml file:
concurrent = 1
check_interval = 0
[session_server]
session_timeout = 1800
[[runners]]
name = "PEN-PC2101"
url = "https://gitlab.com"
token = "********"
executor = "shell"
shell = "powershell"
[runners.custom_build_dir]
[runners.cache]
[runners.cache.s3]
[runners.cache.gcs]
[runners.cache.azure]
A bit late...
A Windows service can no longer open a GUI, the reason is that the services are potentially running a user that is not you. They can even run even if you are not logged on.
You will have to have a software running in your session, communicating (over sockets/pipes/...) with the service and launch your executables.

Proper way of starting virtual process

With app-v 5.1 sequencer, i packaged files in
[{AppVPackageDrive}]\folder1\folder2\app.exe
and have put .appv package in network share
\\192.168.0.2\…\package.appv
Now, from client machine i want to run this app-v package.
This is what i did in powershell as Administrator:
$package = Add-AppvClientPackage -Path \\192.168.0.2\…\package.appv
Publish-AppvClientPackage $package
And this is what i did as normal user:
$p = Get-AppvClientPackage Package
Start-AppvVirtualProcess -AppvClientObject $p
Now the powershell asks me to:
Supply values for the following parameters:
FilePath:
What should i enter?
You also need to specify which EXE from that package (or outside the package) you want to launch. Get-AppvClientPackage returns only the package object.
Here is the doc on launching a virtual process:
https://support.microsoft.com/en-us/help/2848278/how-to-launch-processes-inside-the-app-v-5-0-virtualized-environment

Powershell script only works in ISE

DESCRIPTION
So I have this script, where I login to remote machine using OpenSSH in powershell script. It creates the session and executes ~Run_Project.sh and logs to the log file in the linux machine > ~/project_log&.
function StartSSHsessionAndRunProject
{
try
{
ssh.exe -i ~/id_rsa-private remoteLinuxMachine#admin '~/Run_Project.sh > ~/project_log&'
if ($?)
{
Write-Host "SSH session has been created...`n" -ForegroundColor Green
}
else
{
throw $error[0].Exception
}
}
}
EXPECTATION
As you can see I have '&' symbol at the end of the > ~/project_log& command. It supposed to return to the if statement.
PROBLEM:
This script works fine and as expected in Powershell ISE. However, when I debug or run this powershell script in another tool like Visual Studio Code, Run As Admin in Powershell command DOESN'T work. Anything before this function works fine, the script just pauses and looks like it freezes in ssh.exe line. However, it actually runs the project and the project log says it has started. Any ideas?

Multithreaded Powershell GUI Application never displays when called from CMD.exe

I have finally finished my powershell wpf application for internal use at my company, and it runs as expected when called from a powershell window, however when I try to call it from CMD, or from Visual Studio as an external tool, the GUI window never shows up.
My application functions very closely to this write up, with a few changes to the display and functions that run in the background.
Can anyone explain why my ps1 file does not display the gui when I call it from CMD, but it does when called from the powershell command window? Here is the gist of the script:
$Global:syncHash = [hashtable]::Synchronized(#{})
$newRunspace =[runspacefactory]::CreateRunspace()
$newRunspace.ApartmentState = "STA"
$newRunspace.ThreadOptions = "ReuseThread"
$newRunspace.Open()
$newRunspace.SessionStateProxy.SetVariable("syncHash",$syncHash)
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
$psCmd = [PowerShell]::Create().AddScript({
[xml]$xaml = #"
<valid wpf>
"#
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$syncHash.Window=[Windows.Markup.XamlReader]::Load( $reader )
[xml]$XAML = $xaml
$xaml.SelectNodes("//*[#*[contains(translate(name(.),'n','N'),'Name')]]") | %{
#Find all of the form types and add them as members to the synchash
$syncHash.Add($_.Name,$syncHash.Window.FindName($_.Name) )
}
## Custom functions for items in syncHash here ##
$syncHash.Window.ShowDialog() | Out-Null
$syncHash.Error = $Error
})
$psCmd.Runspace = $newRunspace
$date = $psCmd.BeginInvoke()
I am calling this script in cmd.exe "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe C:\pathToFile\file.ps1"
My Execution policy has been updated so that I can run scripts as well.
Thanks for all the help, I got it figured out. For some reason I needed to run the command with the NoExit option
powershell.exe -ExecutionPolicy Bypass -NoExit -File "pathToFile\File.ps1"

How to make powershell tell me about missing DLLs?

I use powershell as shell in Windows. When I'm trying to launch some application who's dll dependencies are missing in PATH environment variable, then nothing happens, powershell just silently returns with new command prompt.
Is there a way to make powershell fail louder, telling me what exactly is missing, like default cmd shell does?
I was having this same problem. PowerShell was setting $LASTEXITCODE code to -1073741515 (0xC0000142, 3221225794) but no output explaining what was actually wrong. When running it via cmd.exe I would get popup with something like:
The code execution cannot proceed because some.dll was not found. Reinstalling the program may fix this problem.
cygwin bash outputs errors relating to dll not found to stderr and if you run the the same via bash from PowerShell then you can see the error:
> & 'C:\tools\cygwin\bin\bash.exe' '-c' '"C:/Users/xxx/dir/main.exe"'
C:/Users/xxx/dir/main.exe: error while loading shared libraries: another.dll: cannot open shared object file: No such file or directory
This works with git bash also:
> & 'C:\Program Files\Git\bin\bash.exe' '-c' '"C:/Users/xxx/dir/main.exe"'
C:/Users/xxx/dir/main.exe: error while loading shared libraries: another.dll: cannot open shared object file: No such file or directory
Quite a hack but better than nothing.
You could echo the %ERROR% variable, which stores errors until the PowerShell window is closed.
Update: In PowerShell, you could use the Get-Error command, or look at the $Error variable.
Another way would be to use Dependancy walker, if you can use a command line option, then you should be able to use this in PowerShell.
I am afraid there is no way to get that info... But try to read
An Introduction to Error Handling in PowerShell http://blogs.msdn.com/b/kebab/archive/2013/06/09/an-introduction-to-error-handling-in-powershell.aspx
or
PowerShell Tutorial – Try Catch Finally and error handling in PowerShell
http://www.vexasoft.com/blogs/powershell/7255220-powershell-tutorial-try-catch-finally-and-error-handling-in-powershell
Try
{
$AuthorizedUsers = Get-Content \\ FileServer\HRShare\UserList.txt -ErrorAction Stop
}
Catch [System.OutOfMemoryException]
{
Restart-Computer localhost
}
Catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
Send-MailMessage -From ExpensesBot#MyCompany.Com -To WinAdmin#MyCompany.Com -Subject "HR File Read Failed!" -SmtpServer EXCH01.AD.MyCompany.Com -Body "We failed to read file $FailedItem. The error message was $ErrorMessage"
Break
}
Finally
{
$Time=Get-Date
"This script made a read attempt at $Time" | out-file c:\logs\ExpensesScript.log -append
}

Resources