Reputation: 1
New to Powershell:
I have to run a command from the CMD prompt because it will not work in Powershell. I think I understand how to run it from the CMD prompt by using "cmd /c"? I tested my script on a test machine with a simple "Hostname" command and it worked, but when I try it from the server with the actually command it does not work. I get an error "cmd : 'C:\Program' is not recognized as an internal or external command"
I'm confused how to first navigate to the correct folder to run the command in Powershell. What am I missing? Here is what I tried, but I'm getting an error and the command wont run.
I'm sure it is something simple that I am missing.
$var = (cmd /c "C:\Program Files (x86)\nCipher\nfast\bin>anonkneti xxx.xxx.xxx.17")
cmd /c "C:\Program Files (x86)\nCipher\nfast\bin>anonkneti xxx.xxx.xxx.17"
If($var.length -eq 55)
{
Write-EventLog –LogName "Application" –Source "Cipher" –EntryType "Information" –EventID 9999 -Message "Connection Successful"
Thank you kindly!
Upvotes: 0
Views: 386
Reputation: 16116
As for ...
'I have to run a command from the CMD prompt because it will not work in Powershell.'
... well, not really. You just have to make sure, you pass the command correctly.
If this line ...
'C:\Program Files (x86)\nCipher\nfast\bin>anonkneti'
.... is just executable, that expects an IPAddress as an argument, then just pass that directly.
Snippet from the reference below...
- The Call Operator &
Why: Used to treat a string as a SINGLE command. Useful for dealing with spaces.
In PowerShell V2.0, if you are running 7z.exe (7-Zip.exe) or another command that starts with a number, you have to use the command invocation operator &.
The PowerShell V3.0 parser do it now smarter, in this case you don’t need the & anymore .
Details: Runs a command, script, or script block. The call operator, also known as the "invocation operator," lets you run commands that are stored in variables and represented by strings. Because the call operator does not parse the command, it cannot interpret command parameters
# Example:
& 'C:\Program Files\Windows Media Player\wmplayer.exe' "c:\videos\my home video.avi" /fullscreen
Things can get tricky when an external command has a lot of parameters or there are spaces in the arguments or paths!
With spaces you have to nest Quotation marks and the result it is not always clear!
In this case it is better to separate everything like so:
$CMD = 'SuperApp.exe'
$arg1 = 'filename1'
$arg2 = '-someswitch'
$arg3 = 'C:\documents and settings\user\desktop\some other file.txt'
$arg4 = '-yetanotherswitch'
& $CMD $arg1 $arg2 $arg3 $arg4
# or same like that:
$AllArgs = @('filename1', '-someswitch', 'C:\documents and settings\user\desktop\some other file.txt', '-yetanotherswitch')
& 'SuperApp.exe' $AllArgs
• Using PowerShell and external commands and their parameters or switches. Running external commands, always require special consideration.
• PowerShell: Running Executables
• Solve Problems with External Command Lines in PowerShell
• Top 5 tips for running external commands in Powershell
• Using Windows PowerShell to run old command-line tools (and their weirdest parameters) https://blogs.technet.microsoft.com/josebda/2012/03/03/using-windows-powershell-to-run-old-command-line-tools-and-their-weirdest-parameters
• Execution of external commands in PowerShell done right Part2 and Part3, see also.
• Quoating specifics https://trevorsullivan.net/2016/07/20/powershell-quoting
Upvotes: 1
Reputation: 5473
You can run it like this:
$command = "cmd /c 'C:\Program Files (x86)\nCipher\nfast\bin>anonkneti xxx.xxx.xxx.17'"
Invoke-Expression -Command $command
The Invoke-Expression cmdlet invokes a command or expression on the local computer
Edit: As suggested in the comments, Invoke-Expression should not be used in production. A better alternative is the call operator &
:
& "cmd.exe" -Arguments "/c notepad.exe"
More info here
Upvotes: 0