Trying to escape parenthesis when passing arguments in a PowerShell script - powershell

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)'

Related

Get-Content splits path with spaces in powershell

I want to get the content of a simple text file with powershell.
This is my code:
$versionPath = "$PSScriptRoot/../VERSION"
Get-Content $versionPath -Raw
I get this error:
C:\Users\Rik : The term 'C:\Users\Rik' 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
+ C:\Users\Rik van\source\repos\
+ FullyQualifiedErrorId : CommandNotFoundException
Get-Content splits the path by spaces, I tried the suggestions here
but nothing seems to work. any suggestions?
My spidey-sense says you're calling your script on the command line like this:
C:\> powershell C:\Users\Rik van\source\repos\myscript.ps1
The problem isn't the content of your script - it's the path to it.
PowerShell is treating the space as the end of the path to the script, and assumes the rest of the filename is parameters to pass to it. The only problem is it can't find a PowerShell script called C:\Users\Rik.
If you wrap the filename in quotes it'll probably just work:
C:\> powershell "C:\Users\Rik van\source\repos\myscript.ps1"

Running a small WMI Powershell Script

I'm trying to have a few scripts that I can map to run from my keyboard for quickly changing the monitor/screen brightness. After some searching on the internet, I found this script which works when I enter it into Powershell.
$monitor=#(gwmi WmiMonitorBrightnessMethods -ns root/wmi)[0]
$monitor.WmiSetBrightness(50,0)
After I saved it as a .ps1 file and tried running it from the file, powershell tells me: The term "path of the file" is not recognized as the name of a cmdlet, function... and so on.
I'm not familiar with Powershell at all, can someone help with what I need to add in order for the script to run properly?
By default you can't run a PowerShell script that is in the current directory without putting .\ in front of the script name, or calling the full path of the script.
This is a security feature.
If you are in the directory that contains the script, run it by executing in a PowerShell window:
.\yourscript.ps1
Where yourscript is the name of your script.
See here for more information: https://ss64.com/ps/syntax-run.html
You may also see this error if your script has spaces in its name. If that is the case, enclose the path in quotes:
.\'your script.ps1'

Syntax for running executables?

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"

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