Powershell in c# and permissions - powershell

I am running PowerShell scripts from a Windows service running as SYSTEM account. To increase rights I am letting users select a user that I later impersonate the whole thread as using LogonUser method.
This works so that I can access network drives and when I try to print the current user in PowerShell that works too.
But I have a case when it does not work:
One example is a user trying to load Exchange snapin and run cmdlet: Get-MailboxPermission
Then he get error: The term 'Get-MailboxPermission' 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. The term 'Get-MailboxPermission' 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 am guessing that this is permissions issue and I wonder if there is anything else that needs to be changed to RunSpace etc to get the increased permissions? Any other ideas?

Which PowerShell version?
Looks like you don't have the exchange snapin added. Try running this command before:
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin
or, via code, in the runspace initialization, use this code:
RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
PSSnapInException snapInException = null;
PSSnapInInfo info = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out snapInException);
Runspace myRunSpace = RunspaceFactory.CreateRunspace(rsConfig);
myRunSpace.Open(rsConfig);
read this article for additional info:
http://msdn.microsoft.com/en-us/library/exchange/bb332449(v=exchg.80).aspx

Related

Running PsExec.exe on Remote PC

I have a PowerShell script that configures WinRM on our remote servers when ran on the local user's account. However, I need to upload the script to TFS and have each host run the script themselves.
The script checks if WinRM is configured. If it is not, then it checks if the C:\PSTools\PsExec.exe path exists. If the path exists, it needs to configure WinRM using:
$configure = (C:\PSTools\PsExec.exe \\$hostname -s winrm.cmd -q 2> $null)
However, the above results in the error
The term 'C:\PSTools\PsExec.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.
I know the file is there, so I am not sure what I am doing wrong here. Other examples I've seen use either psexec alone or ./PsExec.exe, but none of those work.
Any advise appreciated, thanks.
While this doesn't answer why psexec.exe isn't working (although I suspect it's because the file isn't actually there), you can use WMI to enable WinRM instead:
([wmiclass]"\\$hostname\root\cimv2:win32_process").
Create('powershell "Enable-PSRemoting -Force -SkipNetworkProfileCheck"')
and to restart your service remotely (even though Enable-PSRemoting handles this):
$svc = [wmi]"\\$hostname\root\cimv2:win32_service.Name='winrm'"
$svc.StopService()
$svc.StartService()

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/

'quser' is not recognized as the name of a cmdlet, function, script file, or operable program

When using quser in powershell script to get information about user sessions on a remote desktop sesison host, an error occured which says The term 'quser' is not recognized as the name of a cmdlet, function, script file, or operable program. The code is something like this,
$hostObject = [System.Net.Dns]::GetHostByAddress($ip) | Select-Object HostName
$hostName = $hostObject.HostName
$hostInformation = quser /server:$hostName
How to make powershell recognize quser?
I hope you are trying to run this command on a server and not a workstation. I have tried to run on from my laptop and it didn't work but always works when I run it on a server.

Can't submit Data Analytics job in PowerShell Portal

Why can't I run the Submit-AzureRmDataLakeAnalyticsJob cmdlet in a PowerShell runbook via the Aure web portal? I get the following error message:
Submit-AzureRmDataLakeAnalyticsJob : The term 'Submit-AzureRmDataLakeAnalyticsJob' 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'm able to run this command with no errors in the PowerShell ISE.
Can this type of command not be run in a runbook or something? Maybe I'm just not understanding PowerShell
I seem to have solved the issue. I had to import some AzureRM.* modules via the Module Gallery on the portal.

Powershell Webjob fails to run

I have created a Powershell Webjob for use in the Azure Logic App. The code is trying to generate a signed URI so the end user can download the file when its available. To do that I created a simple Powershell Webjob.
New-AzureStorageContext : The term 'New-AzureStorageContext' 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.
CommandNotFoundException New-AzureStorageBlobSASToken : The term 'New-AzureStorageBlobSASToken' 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 know these are part of the Azure Powershell. But is there an alternative for them to run? Or would I need to resolve to a separate console application?
After a lot of google searching, I found that Azure Powershell jobs do not support Azure commands. They have a plan to allow for that, but currently you cannot run Azure PowerShell Jobs on WebApps Webjob.