So I try to invoke a PS script from a ASPX/C# application. When I run the PS script from commandline (PS C:\scripts> ./do-barrelRoll.ps1 -s remoteComputer) it works as expected. But when I use C# it cannot connect to the remote computer (wrong user/password). Fun fact: The user and password to use are inside a config section in the PS script itself (see $SETUP variable)! What am I doing wrong?
Exact error message from ASPX (the server name is correct!):
[remoteComputer] Connecting to remote server remoteComputer failed with the following error message : The user name or password is incorrect
Relevant parts of the PS Script:
set-alias new New-Object
$passwd = $SETUP["password"] | ConvertTo-SecureString -asplaintext -force
$session = new -TypeName System.Management.Automation.PSCredential -argumentlist $SETUP["user"], $passwd
Invoke-Command –ComputerName $PARAM["s"] -Credential $session –ScriptBlock {
# Do a barrel roll
}
Relevant parts of ASPX/C# application:
using System.Management.Automation;
public ActionResult doAction(string server, string action)
{
var split = server.Split(';');
//...
var ps = PowerShell.Create();
ps.AddScript("Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned -f; C:\\scripts\\do-barrelRoll.ps1 -s " + split[1]);
Collection<PSObject> results = ps.Invoke();
// Here I retrive the results and the content of the error stream and display them
}
It works as expected at the command line most likely because your account has full script access and can run everything. The account that .NET runs under should be more constrained. You'll have to check that the account has rights to run unrestricted scripts. Check local policies and then go up from there. You most likely are not the same account that the .NET application is running as.
Related
I have the following PowerShell script that 1) installs Azure PowerShell SDK 2) logs in to Azure using the service principle and 3) creates a resource group. I am trying to call this script from C# .NET 6 but I am getting this error:
New-AzResourceGroup -name $recoveryResourceGroupName -location $locat …
| ~~~~~~~~~~~~~~~~~~~
| The term 'New-AzResourceGroup' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
I think it's not running the PowerShell script code at all.
Note that the actual script does a lot more than just creating a resource group, but this is just an example.
// The difference between CreateDefault and CreateDefault2 is that
// CreateDefault includes engine snap-ins, while CreateDefault2 does not.
var initialState = InitialSessionState.CreateDefault2();
using var ps = PowerShell.Create(initialState);
var results = await ps.AddScript(#"
[string][ValidateNotNullOrEmpty()] $applicationId = """"
[string][ValidateNotNullOrEmpty()] $secret = """"
[string][ValidateNotNullOrEmpty()] $subscriptionId = """"
[string][ValidateNotNullOrEmpty()] $tenantId = """"
# Install Azure Powershell modules
Write-Output "Installing Modules..."
if (Get-Module -ListAvailable -Name 'Az*') {
Write-Output " Az Already Installed"
}
else {
Install-Module -Name 'Az' -Scope CurrentUser -Repository PSGallery -Force
Write-Output "Installed AZ"
}
# Import Azure module
Import-Module 'Az'
# Login to azure using credentials from the KeyVault
$secretAsSecureString = ConvertTo-SecureString -String $secret -AsPlainText -Force
$pscredential = New-Object -TypeName System.Management.Automation.PSCredential($applicationId, $secretAsSecureString)
Connect-AzAccount -ServicePrincipal -Credential $pscredential -Tenant $tenantId
# Select Right subscription
Set-AzContext $subscriptionId
New-AzResourceGroup -Name 'TestRg123' -Location 'eastus2euap'
").InvokeAsync();
foreach (PSObject outputItem in results)
{
Debug.WriteLine(outputItem);
}
UPDATE #1:
I updated the script and added -AllowClubber to make Az is installed but this is what I am getting in the output:
I think Az is not getting installed and for some reason it think Az is already installed
And then
New-AzResourceGroup:
Line |
97 | New-AzResourceGroup -name $recoveryResourceGroupName -location $locat …
| ~~~~~~~~~~~~~~~~~~~
| The term 'New-AzResourceGroup' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
UPDATE #2: I modified the PowerShell script to unconditionally install the Az and I am still getting the same error that New-AzResourceGroup is not defined
Check your powershell version and try to download latest:
https://github.com/PowerShell/PowerShell
In your script you should be able to use:
Import-Module Az.Resources
#Just in case
Az Upgrade
I have got similar error when i am using command
New-AzResourceGroup
Then i have used Azure cli commands then i get the resource group created in azure
My powershell script(Updated your script):
using System.Diagnostics;
using System.Management.Automation.Runspaces;
using System.Management.Automation;
var initialState = InitialSessionState.CreateDefault2();
initialState.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Unrestricted;
using var ps = PowerShell.Create(initialState);
var results = ps.AddScript(#"
Install-PackageProvider -Name Nuget -Scope CurrentUser –Force
Install-Module –Name PowerShellGet -Scope CurrentUser –Force
Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force
Install-Module -Name Az.Resources -AllowClobber -Scope CurrentUser
# Import Azure module
Import-Module 'Az'
Import-Module 'Az.Accounts'
Import-Module 'Az.RecoveryServices'
az login
try {
az account set --subscription ""XXX"
az group create --name rithDemo --location eastus
}
catch
{
$string_err = $_ | Out-String
Write-Output ""Failed to run test $testname because $string_err""
}
").Invoke();
XXX- Subscription name
rithDemo- resource group name
In script I have first login into azure using az login. Then you will be redirected to azure login page there you can login.
Then I set the subscription using az account set command
Then I created a resource group using az group create
Also added AllowClobber in script
By this process the resource group got created.
Output:
I am trying to run a power shell script from the c#.
When running the power shell script only, it runs successfully. But , while trying to run the same script from the c# . I get the error "The term 'New-CsOnlineSession' is not recognized as the name of a cmdlet"
Here is the code:
public static void GetLyncUsers(string userName, string password)
{
using (PowerShell powerShellInstance = PowerShell.Create())
{
var script = string.Format("$Username =\"{0}\"\n" +
"$Password =\"{1}\"\n" +
"$secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force\n" +
"$cred = new-Object System.Management.Automation.PSCredential ($Username , $secpasswd)\n" +
"$CSSession = New-CsOnlineSession -Credential $cred\n" +
"Import-PSSession $CSSession -AllowClobber\n" +
"Get-CsOnlineUser", userName, password);
// use "AddScript" to add the contents of a script file to the end of the execution pipeline.
// use "AddCommand" to add individual commands/cmdlets to the end of the execution pipeline.
powerShellInstance.AddScript(script);
// use "AddParameter" to add a single parameter to the last command/script on the pipeline.
// invoke execution on the pipeline (collecting output)
Collection<PSObject> psOutput = powerShellInstance.Invoke();
// check the other output streams (for example, the error stream)
if (powerShellInstance.Streams.Error.Count > 0)
{
// I am getting this error
//The term 'New-CsOnlineSession' is not recognized as the name of a cmdlet
}
}
Is there anything i am missing? I am new to powershell in general.
Solution:
using (PowerShell powerShellInstance = PowerShell.Create())
{
// Import-Module lynconlineconnector
powershellInstance.Commands
.AddCommand("Import-Module")
.AddArgument("lynconlineconnector");
// rest of your code ....
Why?
When running an interactive session in powershell v3 and higher, the host traps CommandNotFound, and searches every module in all the known locations. If it finds the command, it automatically loads the module, and proceeds normally.
When running same script in C#, the CommandNotFound exception isn't trapped, and hence you get the error.
Related Question(s):
PowerShell - How to Import-Module in a Runspace
#PSTip Cmdlet Discovery and Module auto-loading
I ran into the same problem. You have to install the Lync/Skype For Business Online Connector as described on Technet
The setup program copies the Skype for Business Online Connector
module (and the New-CsOnlineSession cmdlet) to your local computer.
I'm attempting to execute a SQL Query from within Powershell, within C#. I have been successful in doing so with ActiveDirectory cmdlets and wanted to take it one step further.
My first issue is while the following format works with ActiveDirectory (and in the ISE) it fails in C#:
using (PowerShell pS = PowerShell.Create())
{
pS.AddCommand("import-module");
pS.AddArgument("sqlps");
pS.Invoke();
}
I've long since had the security set to Unrestricted, but the error I'm getting is:
CmdletInvocationException was unhandled
File C:\Program Files (x86)\Microsoft SQL Server\110\Tools\PowerShell\Modules\sqlps\Sqlps.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.
However, if I run like this I get no error, though a later "Get-Module -all" call shows no sign of the module:
using (PowerShell pS = PowerShell.Create())
{
pS.AddScript("Import-Module sqlps");
pS.Invoke();
}
If I then try importing the ActiveDirectory module and calling Get-Module, it shows nothing.
What's going on here?
I'm not that great with C sharp but when calling scripts from outside of powershell there is a flag when executing the program to bypass the execution policy, i.e.
powershell.exe -executionpolicy bypass -command "& '\\somepath\somescript.ps1' "
This allows remote scripts to be called, as even with unrestricted set I still found that it wanted to prompt for the execution of some scripts so for instance in the task scheduler it would simply fail to run.
Also when importing SQLPS I've also found it's useful to add the -DisableNameChecking flag, you may also want to push your location beforehand and pop it afterwards otherwise you will end up in the SQLPS PSdrive with no access to local locations if you need it.
Did you try something like this?
PowerShell ps = PowerShell.Create();
ps.AddScript("set-executionpolicy unrestricted -scope process");
ps.AddScript("import-module sqlps");
ps.AddScript("get-module sqlps");
var m = ps.Invoke();
foreach (var mm in m.Select(x => x.BaseObject as PSModuleInfo))
Console.WriteLine(new { mm.Name, mm.Version });
I had a similar issue with the sqlServer ps module. Looks like when executing from C# you need to load the modules manually into the runspace in order for this to work.
string scriptText = File.ReadAllText("yourScript.ps1");
//This is needed to use Invoke-sqlcommand in powershell. The module needs to be loaded into the runspace before executing the powershell.
InitialSessionState initial = InitialSessionState.CreateDefault();
initial.ImportPSModule(new string[] { #"SqlServer\SqlServer.psd1" });
Runspace runspace = RunspaceFactory.CreateRunspace(initial);
runspace.Open();
using (PowerShell psInstance = PowerShell.Create())
{
psInstance.Runspace = runspace;
psInstance.AddScript(scriptText);
var PSOutput = psInstance.Invoke();
}
Also add all the references located in the SqlServer.psd1. This file is usually found in "C:\Program Files\WindowsPowerShell\Modules\SqlServer". I added to folder to my solution to be able to execute on remote servers.
You need to add Microsoft.SqlServer.BatchParser.dll reference in order to execute invoke-sqlcommand from the Powershell.
You should be able to do the same for sqlps module. Rather use SqlServer as it is newer.
I have a ps1 script than runs fine when it is executed from powershell. It creates a user in Office365:
Param(
[string]$adminUser,
[string]$password,
[string]$adminSite,
[string]$userDisplayName,
[string]$userFirstName,
[string]$userLastName,
[string]$userPrincipalName,
[string]$userLicense,
[string]$userOffice,
[string]$userDepartment
)
try {
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
$executionPolicy = Get-ExecutionPolicy
Set-ExecutionPolicy RemoteSigned
$secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($adminUser,$secpasswd)
Connect-MSolService -Credential $credential
#Write-Host "Conected to MSolService ..." -ForegroundColor Green
Connect-SPOService -Url $adminSite -Credential $credential # Here fail when running from .NET
#Write-Host "Conected to SP Online ..." -ForegroundColor Green
$user = New-MsolUser -FirstName $userFirstName -LastName $userLastName -UserPrincipalName $userPrincipalName -DisplayName $userDisplayName -LicenseAssignment $userLicenseAssignment -Office $userOffice -Department $userDepartment -UsageLocation ES
}catch [Exception] {
#Write-host "An Exception ocurred. The proccess is uncompleted" -ForegroundColor Red
#Write-Host $_.Exception.Message -ForegroundColor Red
Set-ExecutionPolicy $executionPolicy
return $false
}
Set-ExecutionPolicy $executionPolicy
return $user
It works.
However, I have a C# program that executes this script in this way:
private Collection<PSObject> RunPsScriptFromFile(string psScriptPath, Dictionary<string, Object> parameters) {
if (!File.Exists(psScriptPath)) {
throw new FileNotFoundException("File not found.", psScriptPath);
}
Collection<PSObject> returnObjects = null;
using (Runspace runSpace = RunspaceFactory.CreateRunspace()) {
runSpace.Open();
RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runSpace);
Pipeline pipeLine = runSpace.CreatePipeline();
Command cmd = new Command(psScriptPath, false);
if (parameters != null && parameters.Count > 0) {
foreach (KeyValuePair<string, Object> p in parameters) {
CommandParameter cp = new CommandParameter(p.Key, p.Value);
cmd.Parameters.Add(cp);
}
}
pipeLine.Commands.Add(cmd);
returnObjects = pipeLine.Invoke();
}
return returnObjects;
}
This program works fine with others scripts, but for this one, I get the following error (at the line I've marked in the script):
The 'Connect-SPOService' command was found in the module 'Microsoft.Online.SharePoint.PowerShell', but the module could not be loaded. For more information, run 'Import-Module Microsoft.Online.SharePoint.PowerShell'.
I found a question about this, but without answer:
Error running ps1 from c# code (Office 365)
I've modified my C# code:
pipeLine.Commands.Add(cmd);
returnObjects = pipeLine.Invoke();
var error = pipeLine.Error.ReadToEnd(); // New line
The "error" var contains the following:
The current processor architecture is X86. The 'C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.PowerShell.psd1' module requires Amd64 architecture.
I've located this file and I've changed this line
# Processor architecture (None, X86, Amd64, IA64) required by this module
ProcessorArchitecture = 'Amd64'
for this one:
# Processor architecture (None, X86, Amd64, IA64) required by this module
ProcessorArchitecture = 'X86'
I don't know if it's a good solution, but it's works. I will keep looking.
Any suggestion is wellcome.
In C:\Users\<your user>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Windows PowerShell
There are two versions of powershell an x86 and a AMD64 helpfully with no suffix name.
Older versions of modules were written only in 32 bit, newer versions including the latest sharepoint online were written only in 64 bit.
If you start the environment in 32 bit it will obviously not run any 64 bit modules, and in a few edge cases you need to run the old 32 bit to run very old modules.
A better solution would be to invoke your powershell script
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -command "script.ps1"
I would guess your finding the syswow 32 bit version first.
I am looking to replace a Virtual Box solution with MS Hyper-V since I have had many problems with non-Reproducible issues in my automated test-suite using Virtual Box. I have a Windows 8.1 computer I will be using to run the tests on.
The current Virtual Box flow:
Start a VM
Reset snapshot
Use C# to transfer files to Guest OS through the network
Use Virtual Box to trigger the transferred .exe file to start automated tests.
I see people using Powershell Scripts and WMI to start and stop their Hyper-V VMs, but I don't see any way to trigger the transferred files on the Guest OS.
Am I missing an API that I can use? Otherwise how could I trigger the EXE on the guest OS programmatically?
I ended up using System.Management.Automation.PowerShell. I will share the main code chunk I used to do each step so future users can get help.
The Main Code Chunk
var ps = PowerShell.Create();
//Restore Snapshots
ps.AddCommand("Restore-VMSnapshot");
ps.AddParameter("Name", snapshot);
ps.AddParameter("VMName", vmName);
ps.AddParameter("Confirm", false);
ps.Invoke();
ps.Commands.Clear();
//Start VM
ps.AddCommand("Start-VM");
ps.AddParameter("Name", vmName);
ps.Invoke();
ps.Commands.Clear();
//Get IP
string[] ipValues = null;
do
{
ps.AddCommand("Get-VMNetworkAdapter");
ps.AddParameter("VMName", vmName);
var ips = ps.Invoke();
ps.Commands.Clear();
if (ips.Count > 0)
{
ipValues = (string[])ips[0].Members["IPAddresses"].Value;
}
} while (ipValues.Length ==0);
string ip = ipValues[0];
//Move Exe to VM
File.Copy(#"...", "\\\\" + ip + "\\Users\\Public\\Documents\\...", true);
//Run Program
ps.AddScript("$Username = '...'; $Password = '...' ;$ComputerName = '"+ip+"' ;"+
"$Script = {Start-Process C:\\Users\\Public\\Documents\\....exe} ;$secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force ;"+
"$mycreds = New-Object System.Management.Automation.PSCredential ($Username, $secpasswd) ;"+
" $Session = New-PSSession -ComputerName $ComputerName -credential $mycreds ; Invoke-Command -Session $Session -Scriptblock $Script");
var passwords = ps.Invoke();
ps.Commands.Clear();
Notes
The //GetIP section is a do{}while() cause the IP takes a while to be query-able.
There is alot of pre-work required with the host computer and VMs to make this system function, which I will not get into here as google explains those parts better than me.
The flow is designed to match another system which uses Virtual Box, so it may seems a bit inefficient.
This obviously needs to be modified to fit each situation, but should be a good starting point for Hyper-V Automation.
A very usefull PowerShell CmdLet to transfert files to VM is Copy-VMFile.
Syntax is explained here :
http://technet.microsoft.com/en-us/library/dn464282.aspx
Hope this helps !