I want to trigger a site design from powershell. It works here but when I try to run it through c# I get this error:
Cannot convert value "param" to type
"Microsoft.Online.SharePoint.PowerShell.SPOSiteDesignPipeBind". Error:
"Guid should contain 32 digits with 4 dashes
(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
I have tested several versions of the addScript string but none have worked. This is the code I'm trying to run. And it works fine in powershell.
PowerShellInstance.AddScript("Invoke-SPOSiteDesign -Identity param($paramSiteDesignId) -WebUrl param($paramUrl)");
PowerShellInstance.AddParameter("paramSiteDesignId", "176d2af0-1772-41b2-9ad7-acfceefc8851");
PowerShellInstance.AddParameter("paramUrl", "https://TenantName.sharepoint.com/sites/TMVTest13");
Any help pointing me in the right direction is very appreciated.
I found a way doing it directly in C# with Tenant.ApplySiteDesign. It works great see link for more info https://laurakokkarinen.com/the-ultimate-guide-to-sharepoint-site-designs-and-site-scripts/#applying-site-designs-programmatically
As you can see on learn.microsoft.com, AddParameter doesn't work by replacing placeholders the way you are doing it, but instead works by adding parameter/value pairs to the given command as parameters.
Your code probably results in something like this:
Invoke-SPOSiteDesign -Identity param($paramSiteDesignId) -WebUrl param($paramUrl) -paramSiteDesignId 176d2af0-1772-41b2-9ad7-acfceefc8851 -paramUrl https://TenantName.sharepoint.com/sites/TMVTest13
According to the documentation, this should do what you want:
PowerShellInstance = PowerShellInstance.AddScript("Invoke-SPOSiteDesign");
PowerShellInstance = PowerShellInstance.AddParameter("Identity", "176d2af0-1772-41b2-9ad7-acfceefc8851");
PowerShellInstance = PowerShellInstance.AddParameter("WebUrl", "https://TenantName.sharepoint.com/sites/TMVTest13");```
Related
I'm getting a, possibly misleading, error when i try to execute powershell from my c# app using powershell.
The error, as in the title, suggests that i'm missing the Identity parameter, but it isn't missing.
I tried debugging through, and confirming that the parameter is added to the Command object, before invoking.
var x = ps.AddScript("Remove-CsOnlineVoiceRoutingPolicy")
.AddParameter("Identity", "DK")
.AddParameter("-Force");
x.Invoke();
I'm running powershell 7.2, and using System.Management.Automation.Powershell version 7.2.1.0
Any ideas as to why this happens ?
I've tried both parameters with and without the dash, making no difference.
The error was using AddScript in addition with addParameter.
When using add scripts the params should be inline in the script, if you want to use addPArameter, it should be following the AddCommand.
dashes in the parameter name in AddParameter() seems to be completely ignored.
a working example would look like this.
var x = ps.AddCommand"Remove-CsOnlineVoiceRoutingPolicy")
.AddParameter("Identity", "DK")
.AddParameter("Force");
x.Invoke();
Really frustrating because it seems to be so close to solution but can't get the last piece working.
I need to get CPU usage using C#. PerformanceCounter is out the question because it takes forever to load the first time. So trying to use PowerShell (System.Management.Automation.dll) to execute what looks like a simple line:
(Get-CimInstance Win32_Processor).LoadPercentage
This is C#:
var cpuUsage = powerShell.AddCommand("Get-CimInstance").AddArgument("Win32_Processor").AddCommand("LoadPercentage").Invoke();
So you can see I'm trying to pipe LoadPercentage command but it won't work.
System.Management.Automation.CommandNotFoundException: 'The term
'LoadPercentage' 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 rest of code works.
Can anyone please spot the issue here?
Thank you in advance!
The issue here is that LoadPercentage is a property of the object, not a command. If you capture the result of the command and iterate through it's members, you should find what you're looking for:
var results = PowerShell.Create()
.AddCommand("Get-CimInstance")
.AddArgument("Win32_Processor")
.Invoke();
foreach (var result in results)
{
Console.WriteLine(result.Members["LoadPercentage"]?.Value);
}
We're pretty new to Ruby and very new to IronRuby so please bear with me. We're in C# trying to do something very simple. I've got a ruby script called doExtract.rb and I need to pass it a file called myfile.txt. We've copied all the files required into the /bin folder of the build and they run correctly when called via the command line.
var rubyRuntime = Ruby.CreateRuntime();
var rubyEngine = rubyRuntime.GetEngine("rb");
String fullPath = String.Format("{0} {1}", "doExtract.rb", "myfile.txt");
rubyEngine.ExecuteFile(fullPath);
gives me an error of "Illegal characters in path"
I've searched high & low on the t'interwebs and to no avail.
We've tried adding the search paths to the rubyEngine and using a full path to the myfile.txt but still get the error. If we call a simple ruby script with no parameters then it works fine. We've also tried with escaped slashed both backwards and forwards in the myfile.txt. I'm sure it'd something really stupid that we're not doing !
Any suggestions where we're going wrong ?
Thanks
I wish to create multiple mailcontacts (external Contacts) in the GAL in Microsoft Online by running Powershell command from C#. The code below works, but is very slow and takes about 15-20 min to run for 400 mailcontacts.
foreach(EmailAdressVM emailAddressVM in emailList.emailAddresses1)
{
//Create New MailContact.
Pipeline pplNewMailContact = runspace.CreatePipeline();
Command cmdNewMailContact = new Command("New-MailContact");
cmdNewMailContact.Parameters.Add("Name", emailAddressVM.sExternalEmailAddress);
cmdNewMailContact.Parameters.Add("Displayname", emailAddressVM.sFullName.Trim());
cmdNewMailContact.Parameters.Add("Lastname", emailAddressVM.sLastName.Trim());
cmdNewMailContact.Parameters.Add("Firstname", emailAddressVM.sFirstName.Trim());
cmdNewMailContact.Parameters.Add("ExternalEmailAddress", emailAddressVM.sExternalEmailAddress.Trim());
pplNewMailContact.Commands.Add(cmdNewMailContact);
pplNewMailContact.Invoke();
pplNewMailContact.Stop();
pplNewMailContact.Dispose();
}
I am guessing that this is slow since I create a new Pipeline for every new mailcontact that is added and there has to be a more eficient way of doing this since running...
import-csv <filename> | ForEach {
new-mailcontact -name $_.emailaddress -displayname $_.FullName -lastname $_.lastname -firstname $_.firstname -externalemailaddress $_.emailaddress -alias $_.alias
}
...is much faster.
I have found some references after many hours of searching the web that you can do something similar to using a CSV when running Powershell commands from C#, i.e. send a list (or array) of values to a command (in this case the "new-mailcontact" command). But, I have not found any good example of how to send more than one value to a command and I need to supply many values (for example: -name $.emailAddress -displayname $.FullName, etc.) to the "new-mailcontact" command.
Is it possible to send a list (or array) in a similar way as the "import-csv" command (when using regular powershell) and will this be faster, or is there an evan better way? Would I get better performance if I use Powershell 3 instead of 1 (as I am using now).
Please provide working sample code i C#!
Please note that I cannot save a CSV file to disk and the execute powershell from CMD since I do not have write access to disk and that I do not think that I can run an entire script remotely (since remote scripting probably is disabled on Exchange Online).
The biggest reason I would think is because for each address you are creating a new Powershell instance and you are not multithreaded.
You code looks something like this from above:
Foreach email address{
Declare a new Powershell process
Add attributes to call later
Start Powershell and pipe stuff in
Close Powershell instance
}
I think you would be better off creating the Powershell instance / pipe once and then sending each object into it. More along the lines of:
Create PS Pipe
Foreach email address{
PS.SendArguments(Email, Name, DN, etc.);
}
I am not in an environment to get something working or tested right now, so hopefully this gives you at least most of what you need...
I am trying to run Exchange cmdlets using System.Automation dll in C#.
In http://technet.microsoft.com/en-us/library/dd315325.aspx, they have said that for escaping single quotes, we basically need to append it with another single quote
For example,
[PS] C:\Windows\system32>Write-Host 'Live and let le''arn'
Live and let le'arn
However, when I try to do the same thing with my cmdlet,
New-Mailbox -Name 'user1.''.cn'
The new mailbox is actually created with name as user.''.cn. We would like it to be user.'.cn
Code to execute this cmdlet is as follows:
AutomatedRunspace.Command command = new AutomatedRunspace.Command(cmdlet.Command);
foreach (CmdletParameter param in cmdlet.GetParameters())
{
command.Parameters.Add(param.Name, param.Value);
}
pipeline.Commands.Add(command);
Is there anything we can do to correctly escape it?
When you're invoking cmdlets in C#, you need to also worry about the C# string quoting behavior. It's not clear in your question what exactly you're setting the Name parameter to in C# code. I would try this:
string nameArg = "user1.'.cn";
That is, the PowerShell API should be bypassing the parameter parsing phase since you're supplying the argument directly via the API.