Syntax for running executables? - powershell

I am trying to run this from the PowerShell 3 ISE:
&"C:\inetpub\htpasswd.exe -bc C:\inetpub\wwwroot\xyz\password\passMD5.txt sm88555 sm88999"
but get this error:
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.
I think PowerShell stops evaluating this correctly after the first space?

iex - Invoke-Expression I use when & fails
$htPassword = "C:\inetpub\htpasswd.exe"
$htParams = "C:\inetpub\wwwroot\xyz\password\passMD5.txt sm88555 sm88999"
Invoke-Expression -Command "$htPassword $htParams"
myeval handles both quite well by joel-b-fant

The call operator doesn't interpret entire commandlines/expressions. That is what Invoke-Expression is for. Separate the arguments from the command (and from each other) if you want to use the call operator:
& "C:\inetpub\htpasswd.exe" -bc "C:\inetpub\wwwroot\xyz\password\passMD5.txt" "sm88555" "sm88999"

Related

Trying to escape parenthesis when passing arguments in a PowerShell script

I made a PowerShell script that will send a toast notification via/arguments. In the script, I use the param function in the beginning of the script something like....this
param($UserID)
Now I open a command prompt to load a PowerShell script and adding these arguments
powershell C:\file\in\directory\PowershellScript.ps1 -UserID "Mikey (TEST)"
When I sent the prompt over to PowerShell, it gave me this red error that says
TEST : The term 'TEST' 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 line:1 char:64
Assumingly the parentheses ( ) could be the culprit as PowerShell is thinking of loading a module of some sort instead of applying as a string value. I even tried escaping the parentheses using the backslash \(TEST\) but that didn't work either as it said TEST\ : The term 'TEST\' is not recognized as the name of a cmdlet, function, script file, or operable program..
Is there something I'm missing or something part of a script I should add to?
Use single quotes inside the ps command:
powershell C:\file\in\directory\PowershellScript.ps1 -UserID 'Mikey (TEST)'

Error in running CTRL+C Powershell script in Powershell.exe

I want to run a Powershell code in Powershell.exe.
When I type in
Copy-Item c:\test\my_file.txt C:\Test\testPSHELL
it works fine.
When I copy it and then paste it, I get
^V
which is normal. But after excecuting I get error:
The term '▬' 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 line:1 char:1
Dont you know where can be the problem? I am getting upset :D
The reason you got that error is that Ctrl-C is the interrupt command for PowerShell. If you have a long running command that you want to interrupt before it finishes, pressing Ctrl-C will kill the command (This is the same for Linux shells like bash, zsh, etc.)

Passing Arguments between script files

I need to pass a few arguments from one script in file to another one. I load path to my current script file to variable and add name, arguments of other script that I want to call.
Here is the sample of calling and passing argument that I got in Script1.ps1:
Param([string]$argument)
$thisScript = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
.($thisScript+'\anotherScript.ps1 -passedArgument '+$argument)
Here is the part of the script Script2.ps1 that I'm calling:
Param([string]$passedArgument)
$passedArgument = "do some work with it HERE"
When I start the first script like this
C:\Users\user1\Desktop\Script1.ps1 -argument datatopass
it writes the error
The term 'C:\Users\user1\Desktop\Script2.ps1 -passedArgument datatopass' 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 try use the script manually like this
C:\Users\user1\Desktop\Script2.ps1 -passedArgument datatopass
it works fine and doesn't report any error with wrong path or name.
I don't know where the problem is, and I couldn't find anything about this error.
You dont have to concat the passedArgument with its value to a string. Try:
& (Join-Path $thisScript 'anotherScript.ps1') -passedArgument $argument

passing a directory into a powershell script

I am passing a directory into a powershell script. I have done this before, but apparently I haven't done it with a directory that has spaces in it: For example...
C:\Program Files (x86)\Microsoft SDks\Windows\v7.0A\bin\
Well, simple enough right, just surround it with paren's right? Wrong... When it is in my actual script it is throwing the following anoying exception:
The term 'x86' 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 include
d, verify that the path is correct and try again.
At line:1 char:275
This is being passed into my script as a parameter btw, hence why it is throwing the error at line 1.
Any advice on passing directories as paramters into powershell scripts?
Try putting into into quotes, single or double will work.
You can also throw it into a variable first.

Running Powershell script from SSIS with OnTap cmdlets throws error, but not when run from PS Cmd Line?

We are trying to run a Poweshell Script that uses the OnTap PS Modules, from SSIS, when we do, an error is issued:
Error: The term 'Connect-NaController' 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.
But when we run the same script from the Powershell Command Line, then the script runs just fine. So I think the scripts are fine.
So I'm wondering if the security context is different or we have to do something more explicitly in our call from SSIS?
When we call the script from SSIS we use: -ExecutionPolicy ByPass
Thanks!
In SSIS i had to set the FULL PATH of the script instead of ./scriptfile