passing a directory into a powershell script - powershell

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.

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

The term 'powershell.exe' is not recognized as the name of a cmdlet in VSTS Task

All of sudden I m getting the error
"2018-08-30T06:19:30.8460321Z ##[error]The term 'powershell.exe' 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.
" while executing PS task in VSTS .
My PS task has simple Write-Host $variable statement, it worked till y'day and something went wrong, tried rebooting target machine, ensured PS is available etc.
Any debugging steps please ?
Just in case somebody gets this error message on a hosted vm. I had the same issue and it was caused by a Pipeline variable called "Path". So just be smarter than me and don't name your variables like that.
This is what you definitely should avoid:
Refer to these steps:
Check Path variable of System variables
By default, there is %SYSTEMROOT%\System32\WindowsPowerShell\v1.0\ item, if it isn’t existing, please add related path that contains PowerShell.exe to the Path variable
Restart your machine.
Check the environment variables on the machine - the "Path" variable should have the path to the Windows PowerShell directory in system32
Issue was with Inline PS script, any script error throws .
The term 'powershell.exe' 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.
Created a new release definition and it worked
So I noticed that release definitions created before is not having the same issue but if I add a new VSTS-task for PowerShell (Microsoft version) to a release definition it is unable to recognize powershell. I am wondering whether they pushed a change to the task and it broke something. My agent path directory is correct.
As an alternative, I am using a 3rd party developer's task. https://thinkrethink.net/2016/05/20/using-the-inline-powershell-vsts-task/

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"

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