I have this powershell script (test1.ps1) calling another powershell script(test2.ps1) to do the job.
Both scriptfiles are in the same folder
test1.ps1
echo "from test1.ps1"
.\test2.ps1
test2.ps1
echo "from test2.ps1"
When I invoke test1.ps1 in C# by creating runspace, adding commands to pipeline and invoking it, I get an error message saying
"The term '.\test2.ps1' 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."
Both the scripts might be in the same folder, but .\test2.ps1 will look for test2.ps1 in the same folder as the calling application, which is the C# app.
Have this in test.ps1:
$scriptDir = Split-Path -parent $MyInvocation.MyCommand.Path
.$scriptdir\test2.ps1
Related
The problem is about sending command to Powershell,
My C# application sends commands one by one in order to get right directory to execute some files but even I downloaded all the necessary packages application throws an error
An exception of type 'System.Management.Automation.CommandNotFoundException' in 'cd C:\\Users\\User1\\Documents\\pathToFileforExecute\\ ' is not recognized as the name of a cmdlet, function, script file, or operable program.
My NuGet Packages ;
My code is like
ps.AddCommand("Set-ExecutionPolicy").AddParameter("ExecutionPolicy", "Unrestricted");
ps.Invoke();
ps.AddCommand("cd C:\\Users\\User1\\Documents\\pathToFileforExecute\\");
ps.AddCommand("./executeTheFile");
ps.AddCommand("pwd");
Collection<System.Management.Automation.PSObject> results = ps.Invoke();
Please help me to solve problem I found this feed but it didn't work for me https://github.com/PowerShell/PowerShell/issues/8119
I tried deleting the System.Management.Automation because I read this package shouldn't use by itself.
You are using AddCommand without the AddParameter option. The path is a parameter.
Try the following code:
ps.AddCommand("Set-Location").AddParameter("Path", C:\\Users\\User1\\Documents\\pathToFileforExecute\\);
Or you may use the AddScript method instead of AddCommand like in this post.
Note that the cd command is an alias for Set-Location in PowerShell.
I'm running PowerShell script in C# code using this method.
PowerShell psExec1 = PowerShell.Create();
string script = File.ReadAllText(downloadPathScript1);
psExec1.AddScript(script);
Collection<PSObject> results2;
results2 = psExec1.Invoke();
In downloadPathScript1 I'm giving file path of .ps1 file Even after adding correct command or script I'm getting this exception.
The term 'Get-Item C:\Program Files\OneDriveLib.dll | Unblock-File' 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.
When I run same script in PowerShell it is working as expected. I'm trying to run this script/commands. What could be the cause of this exception?
Unblock-File -Path "C:\ODTool - Copy\OneDriveLib.dll"
Import-Module "C:\ODTool - Copy\OneDriveLib.dll"
$Status = Get-ODstatus -ByPath "C:\Users\OneDrive\Documents\Test.txt"
I am new in creating pipelines and I need to create one to build my unity project and C# scripts. I try this:
https://dinomite-studios.github.io/unity-azure-pipelines-tasks/hosted-agent.html (in this try I have not Unity Active License serial key and because of that I remove this task, but my second PowerShell script doesn't pass) for this I get this error:
Starting: PowerShell Script
==============================================================================
Task : PowerShell
Description : Run a PowerShell script on Linux, macOS, or Windows
Version : 2.170.1
Author : Microsoft Corporation
Help : https://learn.microsoft.com/azure/devops/pipelines/tasks/utility/powershell
==============================================================================
Generating script.
========================== Starting Command Output ===========================
"C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a\_temp\2178f08e-dbac-43f8-a053-1a2420d6c47c.ps1'"
Find-UnitySetupInstaller : The term 'Find-UnitySetupInstaller' 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.
At D:\a\_temp\2178f08e-dbac-43f8-a053-1a2420d6c47c.ps1:3 char:41
+ ... tall-UnitySetupInstance -Installers (Find-UnitySetupInstaller -Versio ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Find-UnitySetupInstaller:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : CommandNotFoundException
##[error]PowerShell exited with code '1'.
Finishing: PowerShell Script
and this:
https://medium.com/medialesson/continuous-integration-for-unity-3d-projects-using-azure-pipelines-e61ddf64ad79 for this I get this error:
Starting: Unity Build Android
==============================================================================
Task : Unity Build
Description : Build a Unity project and get the exported output files.
Version : 3.1.1
Author : Dinomite Studios
Help : Builds a Unity project to supported build target platforms. [More Information](https://github.com/Dinomite-Studios/unity-azure-pipelines-tasks)
==============================================================================
Determining Unity editor version for project at D:\a\1\s\virtualterminal-realwear\VirtualTerminal
Success, Unity editor version found 2019.3.9f1, alpha=false, beta=false
Unable to locate executable file: 'C:\Program Files\Unity\Hub\Editor\2019.3.9f1\Editor\Unity.exe'. Please verify either the file path exists or the file can be found within a directory specified by the PATH environment variable. Also verify the file has a valid extension for an executable file.
##[error]Unable to locate executable file: 'C:\Program Files\Unity\Hub\Editor\2019.3.9f1\Editor\Unity.exe'. Please verify either the file path exists or the file can be found within a directory specified by the PATH environment variable. Also verify the file has a valid extension for an executable file.
Finishing: Unity Build Android
and I try with only "VisualStudioBuild" Task, but nothing doesn't work.
Can someone tell me how to do it right?
Find-UnitySetupInstaller : The term 'Find-UnitySetupInstaller' is not
recognized as the name of a cmdlet, function, script file, or operable
program.
For the first error your encountered, if you check the log of first Powershell task, you will find one warning message: WARNING: User declined to install module (UnitySetup). In another word, the module UnitySetup is not installed successfully even the task is pass with green. Our Azure devops system refuse to install this module.
That's why you encountered the cmdlet xxx is not recognized error. That's because the parent module UnitySetup has not installed successfully yet.
To resolve this issue, Need to append -Force parameter after the Install-module command in first Powershell task:
Install-Module UnitySetup -AllowPrerelease -Scope CurrentUser -Force
##[error]Unable to locate executable file: 'C:\Program Files\Unity\Hub\Editor\2019.3.9f1\Editor\Unity.exe'.
According to the second log you shared, you are using Hosted agent, right? And seems also you didn't add Unity Get Project Version task and Powershell tasks to install Unity tool into environment since you were following the pipeline definition sample of this blog.
If you review that blog carefully, you will find that the blog author has pre-installed the self agent on local machine manually before he configured the pipeline in Azure devops, along with Unity Hub installed on the agent.
In short, the Unity.exe has been exists in author's building environment, so the installing Unity steps with Powershell scripts omitted here. BUT our Hosted agent has not installed this executable file in our machine, which means you must install it by yourself when you are using Hosted agent.
Per my opinion, I strongly suggest you to follow the second blog you mentioned to configure your pipeline which can save your build execution time:
1) Install self agent first.
2) Install Unity.exe and needed unity version.
3) Configure pipeline.
I want to run specific code with C# windows application on cmd(command prompt) for sign jar file.
I used this code
System.Diagnostics.Process.Start(#"cmd", #"/K ""c:\program Files\Java\jdk1.6.0_23\bin\jarsigner.exe"" -keystore filepath.p12 filepath.jar ""alias_name""");
I encountered this error while I executing this code
Error:
'c:\program' is not recognized as an internal or external command,
operable program or batch file.
How can I solve this problem ?
From the comments above, i understand this solves the issue:
System.Diagnostics.Process.Start(#"c:\program Files\Java\jdk1.6.0_23\bin\jarsigner.exe",
#"-keystore filepath.p12 filepath.jar ""alias_name""");
Happy signing. ;-)
I have a shared path (like //servername/c$/batches/) where all my batch files are located now I am writing an web application in C# to run .bat files from that application. I know way to do it when I have a physical path.
But here I dont have a physical path. Is it possible to do it.
EDIT# 1
I execute my bat files just by double clicking on them or open the cmd progam on the physical server and then navigate to the drive and execute the bat file.
EDIT #2
when I put UNC path the get the following error
I getting an error myprogram.exe is not recognized as an internal or external command operable program or batch file. 9009
Batch files don't support UNC paths as their "current directory". There's a hackish work around of doing:
pushd "%~dp0"
your batch stuff
popd
%~dp0 expands to the current (d)rive/(p)ath/(0)batchfilename
example:
ok. a Simple batch file:
pushd %~dp0
echo "Hello from batch land"
echo %~dp0
popd
put that on a server somewhere, and try to run it via a unc path:
C:\> \\server\share\test.bat
You'll get as output:
C:\>pushd \\server\share\
Z:\>echo Hello from batch land
Hello from batch land
Z:\>echo \\server\share\
\\server\share\
Z:\>popd
C:\>
Weird, but it works.
That's called a UNC path.
You can use it just like any other path.
However, the user that your ASP.Net code is running as must have read access to the network share.
Apparently, you do have a current-directory issue.
The .bat file is trying to run myprogram.exe from the current directory.
You can make a wrapper batch file on your local machine that maps the network share:
pushd \\server\c$\dir
call filename.bat
popd
You can put this wrapper file anywhere, then call it from your code.