PowerShell execution policy set but not recognized when invoked from .Net program - powershell

I have created a small program in Visual Studio - a form with a button to execute the program when clicked - and built the .exe.
This program is supposed to open PowerShell and run a ps1 file.
When I run it from within VS in debug mode, it works as expected running the ps1 script without error.
When I build the project and run the exe as an administrator, it briefly opens PowerShell but closes immediately.
The output file states that the execution of scripts is disabled on this system, although this is incorrect as I can run any number of PowerShell scripts manually by right clicking them and running them with PowerShell.
I have tried changing the execution policy from Signed, to AllSigned, as well as Unrestricted, but it did not help.
Here is my code:
if (File.Exists("LiveSiteTests.ps1"))
{
File.GetAttributes("LiveSiteTests.ps1");
string strCmdText = Path.Combine(Directory.GetCurrentDirectory(), "LiveSiteTests.ps1");
var process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.FileName = #"C:\windows\system32\windowspowershell\v1.0\powershell.exe";
process.StartInfo.Arguments = "\"&'" + strCmdText + "'\"";
process.Start();
string s = process.StandardOutput.ReadToEnd();
process.WaitForExit();
using (StreamWriter outfile = new StreamWriter("StandardOutput.txt", true))
{
outfile.Write(s);
}
}

Related

Automating a command line exe with powershell

We have a very old tool that our company uses that runs some basic command line scripts to deploy some text files to our servers. It works well, but the process is repetitive.
When the exe runs it does 3 prompts...
"Enter The destination Folder"
"Enter The Server"
"Enter the Password"
We have to do this for about 10 servers, and its sitting there doing a lot of copy paste. Since this process was created everything has been standardized.
So I wanted to make a powershell script that ran the exe process and basically pasted the 3 pieces of information and just loop the server name..
I THOUGHT I had the process working using SendKeys... but even though it appeared to work in every way... it never ACTUALLY did anything. So im not sure if the exe did not like send keys or what.
So then I resolved to trying to use Send Keys outside of Windows forms and launch cmd.exe and run it that way.
That way seemed to work as well, but the results were the same, the files were never updated.
If I did this process manually in powerhsell they worked fine though.
My final attempt was doing this.
try {
$MyProcess = New-Object System.Diagnostics.Process
$MyProcess.StartInfo.FileName = "c:\MyProcess.exe"
$MyProcess.StartInfo.Arguments = "arguments for process"
$MyProcess.StartInfo.UseShellExecute = $false
$MyProcess.StartInfo.RedirectStandardInput = $true
$MyProcess.Start()
$StdIn = $MyProcess.StandardInput
$StdIn.WriteLine($destinationFolder)
$StdIn.WriteLine($server)
$StdIn.WriteLine($password)
} finally {
if($StdIn) {
$StdIn.Close()
}
This process DOES update the files, however I cannot loop it, because as soon as the first exe finishes the script crashes with the error
"Cannot Read Keys when Either application does not have a console or when a console inout has been redirected"
I am looking for a way to get this to work...

Unable to run .vbs from Windows PowerShell in jenkins job

I am trying to use build-->windows power-shell option in Jenkins job and calling the following code:
cd C:\Users\username\Desktop\
cscript create.vbs
My create.vbs has the following code:
Option Explicit
Dim xlApp, xlBook
Set xlApp = CreateObject("Excel.Application")
'Remove the following line to open Excel in the background
xlApp.Visible = true
Set xlBook = xlApp.Workbooks.Open("C:\Users\username\Desktop\excelname.xlsm",0, True)
xlBook.Close
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
WScript.Echo "Finished."
WScript.Quit
But I am getting the following error in console of failed job in jenkins:
C:\Users\username\Desktop\create.vbs(8, 1)
Microsoft Excel: Microsoft Excel cannot access the file
'C:\Users\username\Desktop\excelname.xlsm'.
There are several possible reasons:
The file name or path does not exist.
The file is being used by another program.
The workbook you are trying to save has the same name as a currently open workbook.
My create.vbs and excel sheet are at the same location, none of the excel workbook is opened and I am able to run successfully cscript create.vbs through Windows PowerShell directly.
Launch a slave in cmd and label the jenins job to run on this new slave.It works! the issue was because jenkins was running as a service and was unable to access excel being a desktop application.

How to run appref-ms (clickOnce) apps via powershell

I am trying to create a PS script that would read an xml file (server name list) & then run click once app on each of these servers. In my case ,Power shell script execution will be scheduled via Task scheduler.
Now the problem is : I can't find a way to run click once app via Power shell script
Any ideas?
I run app ref files by running it through the explorer so an example would be
$fileLocation="C:\temp\program.appref-ms"
explorer.exe $fileLocation
This article documents a process to getting the file path to run the ClickOnce app
https://robindotnet.wordpress.com/2010/03/21/how-to-pass-arguments-to-an-offline-clickonce-application/
If you can translate this C#/NET code into PowerShell, it should work
StringBuilder sb = new StringBuilder();
sb.Append(Environment.GetFolderPath(Environment.SpecialFolder.Programs));
sb.Append("\\Nightbird"); //ClickOnce Publisher name (eg Nightbird)
sb.Append("\\TestRunningWithArgs.appref-ms "); //ClickOnce Product name (eg TestRunningWithArgs)
string shortcutPath = sb.ToString();
System.Diagnostics.Process.Start(shortcutPath, argsToPass);
You can run a ClickOnce application by doing a start process on iexplore.exe and passing it the URL to the ClickOnce deployment. Can you do that in PowerShell? If so, then it should work.

How can I hide the cmd console from my Matlab Exe?

I've a problem with my EXE file, generated by Matlab Development tool.
Pratically, even choosing "Windows StandAlone Mode", when I run the EXE, always appears the hated black CMD console.
How can I avoid this apparition?
Thanks for the help to everyone who Try...
There is a utility published on the Mathworks website that does just that. http://www.mathworks.com/matlabcentral/fileexchange/3909-suppress-command-window
Another solution would be to use another application to launch your MATLAB exe silently.
In C# for example you could start your MATLAB exe with the following arguments to hide the console window:
var process = new Process();
process.StartInfo.FileName = "path to your exe"
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
// ...
process.Start();

Why does my VBScript file fail when I run it from the command line?

I have a very simple VBScript file that interacts with iTunes via a COM object:
If I double-click on the .vbs file, it works fine. But if I run it from the command line, it fails:
c:\windows\SysWOW64\wscript myscript.vbs
iTunes opens, but the commands don't work, and after a short delay I get this:
ActiveX component can't create object: 'iTunes.Application'
Code: 800A01AD
The problem remains when I use cscript, and when I use the system32 version.
So, two questions:
Why is the behaviour different when double-clicking/running from the CLI?
How do I fix it, so it runs from the CLI, too?
Edited to add script:
Dim oiTunes, oTracks, oAdd
Set oiTunes = CreateObject("iTunes.Application")
Set oTracks = oiTunes.LibraryPlaylist
Set oAdd = oTracks.AddFile("D:\Users\Mark\Music\Downloaded iPlayer\Temp\temp.mp4")
Do : Loop While oAdd.InProgress = True
Set oAdd = Nothing
Set oTracks = Nothing
Set oiTunes = Nothing
The script you have shared works fine for me. Perhaps one of the CSLIDs has become corrupt - try reinstalling or repairing iTunes.