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.
Related
When I tried to create dispatch-Model for my bot., I am getting following error - 'dispatch' is not recognized as an internal or external command, operable program or batch file.Before using dispatch command, I installed botdispatch using npm install -g botdispatch. But still I am getting this error.
Can you check your NPM folder to see if there is anything with dispatch there? Assuming you're on Windows, this is typically in AppData\Roaming\npm.
PowerShell:
dir $home\AppData\Roaming\npm
CMD:
dir %homepath%\AppData\Roaming\npm.
If dispatch (dispatch.cmd) is there, then it might be a pathing issue. Check your path variables to make sure that that path is there:
CMD:
echo %path%
PowerShell:
($env:path).Split(';')
So I was having this same issue, it turns out I needed to add the path of the NPM folder C:\Users\xxxxxx\AppData\Roaming\npm to my path for Powershell7.
$env:Path += ";C:\Users\xxxxxx\AppData\Roaming\npm"
Where xxxxxx is your userid. After adding this command to Powershell the npm modules began to work.
If you do not see this path by running this command below, then you need to add it with the one above.
($env:path).Split(';')
All of these should be ran inside of a PS7 window, I ran them with Administrator rights when I launched the Powershell 7 window.
I would like to run the following powershell commands from my C# application:
Enter-PSSession –ComputerName fedsrv01.domain.local
Start-ADSyncSyncCycle -PolicyType Delta
I found some information on the Powershell Class but struggling to achieve what I want due to my lack of experience.
https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.powershell?redirectedfrom=MSDN&view=pscore-6.2.0
This is what I have so far:
I have added the assembly and referenced system.management.automation
using (var powershell = PowerShell.Create())
{
//powershell.AddCommand("get-process");
powershell.AddCommand("Enter-PSSession -ComputerName fedsrv01.domain.local");
powershell.Invoke();
}
I get an error saying, 'The term 'Enter-PSSession -ComputerName fedsrv01.domain.local' is not recognized as the name of a cmdlet, function, script file, or operable program.
if I use: powershell.AddCommand("get-process") it executes fine.
If I launch Powershell on the same PC and enter, Enter-PSSession -ComputerName fedsrv01.domain.local it works fine.
Any assistance would be much appreciated.
Cheers,
Jono
Try compiling your application as x64. If it is compiled as x86 platform then it will be using the virtualized System32 dir so the function you require may not exist.
Powershell commands from C# 'the term is not recognizes as cmdlet'
Ok, after more research into the PowerShell class I now realise that you have to add the parameters separately using the .addparameter method.
.addcommand is just for the PowerShell commands. It now makes sense why I got the error saying the command could not be found. It was assuming the entire string was a command.
Problem solved!
Jono
I am trying to use this azure commandlets form C# application that is console job hosted in my azure Virtual machine. this job runs more then once in a day automatically but my azure commandlets are fails to execute. yesterday that works fine but after a day it is not working please help me. ☺
Note : here i am doing some administration task and i am using organizational account. but it fails to add account other commandlets goes fail to work.
Install the Azure PowerShell tools. The download link is here: https://azure.microsoft.com/en-us/documentation/articles/powershell-install-configure/#how-to-install-azure-powershell
It's renamed to Add-AzureAccount.
I'm assuming that the original question was "The term 'Login-AzureRmAccount' is not recognized as the name of a cmdlet, function, script file. I'm seeing the same thing, despite "Install-Module AzureRM" having been done (from an elevated powershell prompt) and "Get-Module AzureRM" returning a version of 4.3.1 (i.e. powershell tools being installed).
The first part of the solution for me was to also do "Install-Module Azure" (from an elevated powershell prompt) as well after which "Add-AzureAccount" worked. However a command such as "New-AzureRmResourceGroup" still failed with "Run Login-AzureRmAccount to login".
The second part of the solution was simply to reboot, after which:
Add-AzureRmAccount
New-AzureRmResourceGroup -Name wibble -Location UKSouth
worked.
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 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