Powershell Switch Parameter throws ParameterBindingException Exception - powershell

I have to execute the below script thru an Integration Tool Mulesoft. Unfortunately the switch parameter Confirm is throwing an error. The same script works from the powershell command line. Below is the command to execute:
Set-Location -Path 'C:\Windows\System32\WindowsPowerShell\v1.0\';powershell.exe -ExecutionPolicy RemoteSigned -noprofile -noninteractive ". 'C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; Disable-MailUser -Identity abc.com/PEOPLE/xyzpqr -Confirm:$false"
Operation in flow:
<powershell:execute-command-exception-handling-enabled doc:name="Execute command (exception handling enabled)" doc:id="79bd2451-9441-49e7-8517-3bbd6a151fae" config-ref="Powershell_Configuration" command="Set-Location -Path 'C:\Windows\System32\WindowsPowerShell\v1.0\';powershell.exe -ExecutionPolicy RemoteSigned -noprofile -noninteractive ". 'C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; Disable-MailUser -Identity abc.com/PEOPLE/xyzpqr -Confirm:$false""/>
The error is as below
Message : A positional parameter cannot be found that accepts argument '-Confirm:False'.
+ CategoryInfo : InvalidArgument: (:) [Disable-MailUser], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Disable-MailUser
Our PS version is 5.1 Any help would be greatly appreciated I tried

After trying a lot of suggestions in Stackoverflow([How to convert string to boolean in this Powershell code for Exchange Online?][1]) etc here is what worked
I did a cast
-Confirm:([System.Convert]::ToBoolean(0))
and that worked. Strangely
-Confirm:([System.Convert]::ToBoolean("False")) also didn't work.
So a cast was required. Mulesoft Powershell connector must have been playing tricks.
[1]: https://stackoverflow.com/questions/16983810/how-to-convert-string-to-boolean-in-this-powershell-code-for-exchange-online

Assuming your local machine has a Windows OS , having the code working in your local makes sense. But It won't work in Cloudhub because it uses an EC2 instance under the hood with a Linux OS.
You can find more details about it in https://docs.mulesoft.com/runtime-manager/cloudhub-architecture#cloudhub-workers

Related

Powershell doesn't running scripts even after setting execution policy

I am trying to run virtualenv script to activate it on powershell using:
.\env\Scripts\activate.ps1
but I get this error:
.\env\Scripts\activate.ps1 : File C:\Users\user\Desktop\tranning\env\Scripts\activate.ps1 cannot be loaded because running scripts is disabled on this
system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ .\env\Scripts\activate.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
I read this article and I tried to solve this by running powershell as administrator and type this command:
Set-ExecutionPolicy RemoteSigned
but unfortunately nothing change
According to this earlier asked question there are a few other things you can try:
PowerShell says "execution of scripts is disabled on this system."
To summarize from the link:
If you are running from a windows server make sure you set the execution policy on all versions of powershell that you have on the system(both x64 x86 versions of powershell)
OR
Run the following to bypass the Execution Policy:
powershell -ExecutionPolicy ByPass -File script.ps1

Run a powershell script with different credentials

I'm trying to run a powershell script to search for a network drive for a certain file. In my testing, I've found that my script works perfectly fine, however the network drive I need to search require my Domain Admin logon.
I have
Start-Process powershell.exe -Credential "domain\adminusername" -NoNewWindow -ArgumentList "Start-Process powershell.exe -Verb runAs"
as the very first line of my script, but whenever I run the script I get this error:
Start-Process : This command cannot be run due to the error: The directory
name is invalid.
At Path\to\script.ps1:1 char:1
+ Start-Process powershell.exe -Credential "domain\adminusername" -NoN ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Start-Process],
InvalidOperationException
+ FullyQualifiedErrorId :
InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
What directory name is it talking about? If I move the script to the actual network drive, I still get the same error. How do you run a script as a different user?
You could use the net use command to gain access or the new-psdrive command instead. Another option would be to start-process a cmd prompt and use runas within it. Also, you may need to include the full path of powershell.exe or add it to the path variable. %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe

Invoke-Expression: Positional parameter cannot be found that accepts argument /s

I have a .ps1 script which contains a line
Invoke-Expression -Command "C:\Builds\$BuildName /s /v`"/l*v c:\build_install.txt /qn`""<br/>
This is performing Silent installation of a product.
Now, if I try to run this command from Linux box through ssh it gives the following error:
Invoke-Expression : A positional parameter cannot be found that accepts argument '/s'.
At line:1 char:1
+ Invoke-Expression C:\NWTBuilds\Setup-NimbleNWT-x64.2.0.4.117.exe /s /v`/l*v c:\n ...
+ CategoryInfo : InvalidArgument: (:) [Invoke-Expression], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeExpressionCommand
Do you have any suggestions on this? Do I need to provide any credentials?
So I have also tried the following options:
Send the command through ssh or telnet powershell.exe -Command ...
Call the powershell Script from ssh or telnet powershell.exe -File C:\Sample.ps1
However If I ran the same Sample.ps1 from windows Powershell, silent installation is done?
Your /s is being interpreted as being part of your Invoke-Expression call. Can you try Invoke-Command, i.e.:
Invoke-Command { C:\Builds\$BuildName /s /v "/l*v c:\build_install.txt /qn" }
The error message indicates that PowerShell is trying to parse /s as the name of a parameter of Invoke-Expression rather than as part of the argument supplied to -Command, which it would not do if the /s were part of the string. This implies that the string is being terminated just before that. Check the value of $BuildName, it probably contains something that terminates the string. I'm not quite sure what that might be, because a pair of double quotes within the variable value shouldn't have that effect. At least it wouldn't at a PowerShell prompt. Maybe the ssh client is interpreting what you're typing in some way that terminates the string before /s?
In any case, I'd be willing to bet money that the answer lies in the value of $BuildName, because logically the error indicates that the string argument to -Command terminates at that point.

Powershell: Running a .msc applet as another user

I'm currently writing a powershell script that asks for a single set of admin credentials, and uses those to run relevant applications, pulled from a network-hosted CSV. When I try to run
Start-Process $tools[$userInput-1].path.toString() -credential $credential
(where $tools is returning "C:\Program Files\Microsoft\Exchange Server\V14\Bin\Exchange Management Console.msc") I get the error below
Start-Process : This command cannot be executed because the input "C:\Program Files\Microsoft\Exchange Server\V14\Bin\Exchange Management Console.msc" is an Invalid Application. Give a valid application and Run your command again.
At line:1 char:14
+ Start-Process <<<< "C:\Program Files\Microsoft\Exchange Server\V14\Bin\Exchange Management Console.msc" -credential
Get-Credential
+ CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
If I need to, I'll just write a .bat file and run that, but I'd rather avoid that whenever possible.
And the reason I'm not using Invoke-Item is because it can't take -Credential, even if the man file says otherwise.
.msc is a saved console file, the host of which is mmc, so to start this from powershell you could use syntax similar to the following:
$mmcPath = "C:\Windows\System32\mmc.exe"
$mscPath = "C:\Program Files\Microsoft\Exchange Server\V14\Bin\Exchange Management Console.msc"
Start-Process -FilePath $mmcPath -ArgumentList $mscPath

Error message installing Chocolatey in PowerShell

I'm trying to install Chocolatey to use with PowerShell.
The recommended way to install it is copy and paste the following line.
#powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('http://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%systemdrive%\chocolatey\bin
But I get the following error:
At line:1 char:13
+ #powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object ...
+ ~~~~~~~~~~
Unexpected token '-NoProfile' in expression or statement.
At line:1 char:24
+ #powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object ...
+ ~~~~~~~~~~~~~~~~
Unexpected token '-ExecutionPolicy' in expression or statement.
At line:1 char:150
+ ... nstall.ps1'))" && SET PATH=%PATH%;%systemdrive%\chocolatey\bin
+ ~~
The token '&&' is not a valid statement separator in this version.
At line:1 char:1
+ #powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object ...
+ ~~~~~~~~~~~
The splatting operator '#' cannot be used to reference variables in an expression. '#powershell' can be used only as
an argument to a command. To reference variables in an expression use '$powershell'.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
ExecutionPolicy is set as RemoteSigned and I'm running Powershell v3
I tried some apply some bits of the installation code rather than the whole line, but basically, anything after #Powershell is an unexpected token.
You must start that line from cmd.exe (a "standard" command prompt), not from PowerShell.
In PowerShell v3+ the easiest way is:
Open a PowerShell window (run as administrator)
Check the version of PowerShell is greater than 3:
$PSVersionTable.PSVersion
Enable execution of PowerShell scripts?
set-executionpolicy remotesigned
In PowerShell
iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex
I was unable to install Chocolatey on my Windows 10 64-bit OS installation. I was getting powershell not recognized as internal or external command. Finally I found the solution, so to people who ever facing the exact same problem as I did, here is the solution for you.
The reason why you get such an error is because the WindowsPowerShell path is not set. So kindly set the Path as
%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\
Go to Environment variable (see below). You see that Path variable, click on Edit and you see one more pop-up window, which shows a couple of paths there. Now click on New and copy-paste the above path. Close your CommandPrompt(admin) and open it again. Run the command given by Chocolatey, and now it starts downloading.
Here is a step-by-step guide:
Go to Control Panel → System → Advanced System Settings → Environment Variables → User variable for Users → Select Path Variable → click Edit → Click on New → paste this %SYSTEMROOT%\System32\WindowsPowerShell\v1.0\ → Click OK → you're done.