Uploading to Teams with C# and Powershell - c#

I'm writing a form that will upload any file in a folder to a Microsoft Teams share. My code already manage to upload the files, but keep showing me the log in form of microsoft 365 for each file, instead asking only the first time.
this is the code:
private void Upload()
{
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
PowerShell psinstance = PowerShell.Create();
//fill some vars and connect (once, hopefully)
psinstance.AddScript("$SharepointURL = '" + LINK-TO-THE-DESIRED-TEAM + "'");
psinstance.AddScript("$OutPath = '" + SOURCE-DIRECTORY + "'");
psinstance.AddScript("Connect-PnPOnline -SPOManagementShell -url $SharepointURL -cleartokencache");
//upload each file in the directory
psinstance.AddScript("$Files = Get-ChildItem -path $OutPath");
psinstance.AddScript("foreach ($File in $Files){ Add-PnPFile -Folder '" + DESIRED-CHANNEL-DIRECTORY + "' -Path $File.FullName }");
//execute the script
Collection <PSObject> results = psinstance.Invoke();
//get possible errors
Collection<ErrorRecord> Errors = psinstance.Streams.Error.ReadAll();
runspace.Close();
}
The same powershell code, if executed in the powershell console, works like a sharm. This is driving me crazy, can you help me?

Related

How to add user in IIS Manager for FTP Site using PowerShell commands in C#?

I want to add users for my FTP site using C# web api so, I have tried this link but when I am using these commands using Powershell then, these commands successfully creating a new user. Irrespective to this when I want to automate this using below code
string scriptText = #"[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.Web.Management')" + "\n" +
"[Microsoft.Web.Management.Server.ManagementAuthentication]::CreateUser('" + UserName + "','" + Password + "')" + "\n" +
"[Microsoft.Web.Management.Server.ManagementAuthorization]::Grant('" + UserName + "','" + FtpSiteName + "', $FALSE)";
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);
pipeline.Commands.Add("Out-String");
Collection<PSObject> results = pipeline.Invoke();
runspace.Close();
string strCmdText = "C:/Windows/System32/inetsrv/appcmd.exe set config " +
FtpSiteName + " - section:system.ftpServer / security / authorization / +'[accessType='Allow',users='" + UserName + "',permissions='Read, Write']' / commit:apphost";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
then, I am receiving an error at Collection<PSObject> results = pipeline.Invoke(); which is
{Exception calling "CreateUser" with "2" argument(s): "Filename: \\?\C:\Program Files\IIS Express\config\administration.config
Error: The configuration section 'system.webServer/management/authentication' cannot be read because it is missing a section declaration
"} System.Management.Automation.ErrorRecord
According to my observation, my C# code is editing this file (C:\Program Files\IIS Express\config\administration.config) where it could not find a relavent section whereas IIS Manager requires changes in this file (C:\Windows\System32\inetsrv\config\administration.config) for creating new user.
If anything is unclear then, please let me know. Thanks.

.Net core console application Stop-WebAppPool Remote connect via WinRM using Microsoft.PowerShell.SDK

I need help to solve problem to auto-mate deploy .Net Core web app Api to a server.
As you know is impossible to overwrite .dll if you not stop AppPool before , and there is no solution for that in IIS .
Actually by using PowerShell I can perform a script to do what I need the script is showed below:
PowerShell script
Actually I need a console application to performe same work , I found that I can use Microsoft.PowerShell.SDK to implement a solution.
public static void RunC()
{
string us = "xxxxxxxxxxxxx"; //User
string pw = "xxxxxxxxxxxxxxxx";//Passwprd
string sv = "xxx.x.xx.xxx";//Server
string apppoolname = "xxxxxxxxxxxxx";
StringBuilder script = new StringBuilder();
//Creazione script PS
script.Append("$password = ConvertTo-SecureString \"" + pw + "\" -AsPlainText -Force" + Environment.NewLine);
script.Append("$user = \"" + us + "\"" + Environment.NewLine);
script.Append("$cred = New-Object System.Management.Automation.PSCredential ($user,$password)" + Environment.NewLine);
script.Append("Enter-PSSession -ComputerName \"" + sv + "\" -Credential $cred" + Environment.NewLine);
script.Append("Import-Module webadministration");
script.Append("Stop-WebAppPool \"" + apppoolname + "\"");
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(script.ToString());
//pipeline.Commands.Add("Out-String");
Collection<PSObject> results = pipeline.Invoke();
StringBuilder sb = new StringBuilder();
foreach (PSObject pSObject in results)
{
sb.AppendLine(pSObject.ToString());
}
Console.WriteLine(sb.ToString());
}
But I got error show in the image , seams Module is not lodaded or something similar ..
some one can help me in some way?
Thank you :-)
I dont see the error in the image, but if it is a issue caused by the WebAdministration not being imported this should fix it:
#Requires -Modules WebAdministration
Place that at the begging of the script and it will try to import the module if it's not available already in the session. Microsoft documentation:
Specifies PowerShell modules that the script requires. Enter the module name and an optional version number.
If the required modules aren't in the current session, PowerShell imports them. If the modules can't be imported, PowerShell throws a terminating error.
source
Hope it helps, good luck.

Null results after execution Powershell script in Visual Studio C#

I'm trying to get some data from Azure Active Directory using C# code with reference to System.Management.Automation. I've got no errors with code execution, just null results and no output to textfile. Does anyone have this problem before or maybe I missed something? Thank you!
public void RunScriptTest()
{
string username = "Username";
string password = "Password";
List<String> listResults = new List<String>();
PowerShell powershell = PowerShell.Create();
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
powershell.Runspace = runspace;
powershell.AddScript("Install-Module -Name AzureAD -Force; \n");
powershell.AddScript("Import-Module -Name AzureAD -Verbose \n");
powershell.AddScript("$username = \"" + username + "\"; \n" +
"$password = convertTo-securestring '" + password + "' -AsPlainText -Force; \n" +
"$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $password; \n" +
"Connect-AzureAD - Credential $cred; \n");
powershell.AddScript("Get-AzureADUser | Out-File -FilePath " + #"C:\TestResults\1.txt");
Collection<PSObject> results = powershell.Invoke();
runspace.Close();
StringBuilder stringBuilder = new StringBuilder();
foreach(PSObject obj in results)
{
listresults.Add(obj.ToString());
}
}
Sequencing .AddScript() calls without intervening .AddStatement() calls makes only the last .AddScript() call effective - all previous calls are ignored.
In order to examine errors that may have occurred during execution via .Invoke(), you must access the powershell.Streams.Error stream.
Therefore, the immediate fix is to replace your powershell.AddScript(...) calls with $powershell.AddStatement().AddScript(...)
Note that your PowerShellCode appears designed not to produce any output, so there's no point in trying to populate listresults.

Execute Powershell Script in C# Issue

I have a problem with a Powershell script in VS (C#).
Summary: I build a little tool for specific client actions for Microsoft System Center.
The following runs perfectly:
if (MachPolBox.IsChecked ?? true)
{
using (PowerShell PowerShellInstance = PowerShell.Create())
{
PowerShellInstance.AddScript("Invoke-WMIMethod -ComputerName " + ComputerBox.Text + " -Namespace root\\ccm -Class SMS_CLIENT -Name TriggerSchedule " + MachinePolicy);
PowerShellInstance.Invoke();
MessageBlock.Foreground = Brushes.White;
MessageBlock.Text = "running...";
if (PowerShellInstance.HadErrors)
{
MessageBlock.Foreground = Brushes.Red;
MessageBlock.Text = "Fehler... Programm als Administrator ausgeführt? Computername richtig?";
}
else
{
MessageBlock.Foreground = Brushes.White;
MessageBlock.Text = "Erfolgreich";
}
}
}
One of the actions will trigger an evaluation of the user policy. Problem is: running the script remotely will not trigger the actions for the logged in user on the client.
Here is a workaround I found in PowerShell:
$sid = ( get-wmiobject -query "SELECT UserSID FROM CCM_UserLogonEvents WHERE LogoffTime = NULL" -namespace "ROOT\ccm").UserSID.replace('-','_');
$sched=([wmi]"root\ccm\Policy\$sid\ActualConfig:CCM_Scheduler_ScheduledMessage.ScheduledMessageID='{00000000-0000-0000-0000-000000000026}'");
$sched.Triggers=#('SimpleInterval;Minutes=1;MaxRandomDelayMinutes=0');
$sched.Put()
Now I have problems to parse the script. When tell Powershell to run the script directly, with Invoke, it runs perfect (locally). But I don't want to have the script to persist in the application directory.
So I try to run the script like the first one:
PowerShellInstance.AddScript("$sid = (get-wmiobject -query \"SELECT UserSID FROM CCM_UserLogonEvents WHERE LogoffTime = NULL\" -namespace \"ROOT\\ccm\").UserSID.replace('-','_'); $sched=([wmi]\"root\\ccm\\Policy\\$sid\\ActualConfig: CCM_Scheduler_ScheduledMessage.ScheduledMessageID = '{00000000-0000-0000-0000-000000000026}'\"); $sched.Triggers=#('SimpleInterval;Minutes=1;MaxRandomDelayMinutes=0'); $sched.Put()");
but it will not run (probably a syntax error).
I'm very new to VS and C# ; probably your trained eyes see more. :)
Thanks in advance, Chris
PS: here is the tutorial that I used
#wp78de: Thanks for your answer. But that alone doesn't did the trick. BUT, Verbatim String helps me significant.
I've done it now. Now I can trigger the UserPolicy for the current logged on user.
Code is:
if (UserPolBox.IsChecked ?? true)
{
using (PowerShell PowerShellInstance = PowerShell.Create())
{
var com = #"$sid = (Get-WmiObject -Computername '" + ComputerBox.Text + #"' -query ""SELECT UserSID FROM CCM_UserLogonEvents WHERE LogoffTime = NULL"" -namespace ""ROOT\ccm"").UserSID.replace('-','_');";
var sched = #"$sched = ([wmi]""\\" + ComputerBox.Text + #"\root\ccm\Policy\$sid\ActualConfig:CCM_Scheduler_ScheduledMessage.ScheduledMessageID='{00000000-0000-0000-0000-000000000026}'"");";
var triggers = #"$sched.Triggers=#('SimpleInterval;Minutes=1;MaxRandomDelayMinutes=0');";
var put = #"$sched.Put()";
PowerShellInstance.AddScript(com + sched + triggers + put);
PowerShellInstance.Streams.Error.Clear();
PowerShellInstance.Streams.Warning.Clear();
var result = PowerShellInstance.Invoke();
MessageBox.Show(PowerShellInstance.Streams.Error.Count().ToString() + " error counts");
foreach (var errorRecord in PowerShellInstance.Streams.Error)
{
MessageBox.Show(errorRecord.ToString() + "first - error");
}
}
}
Try to use a verbatim string instead:
PowerShellInstance.AddScript(#"$sid = ( get-wmiobject -query "SELECT UserSID FROM CCM_UserLogonEvents WHERE LogoffTime = NULL" -namespace "ROOT\ccm").UserSID.replace('-','_');
$sched=([wmi]"root\ccm\Policy\$sid\ActualConfig:CCM_Scheduler_ScheduledMessage.ScheduledMessageID='{00000000-0000-0000-0000-000000000026}'");
$sched.Triggers=#('SimpleInterval;Minutes=1;MaxRandomDelayMinutes=0');
$sched.Put()");

Call PowerShell script file with parameters in C#

Really struggling with this. I have tried various different way, but nothing seems to work.
-using addScript: I get an error telling me that I can't call parameters this way an should use a UI like ISE ?!
-using FilePath parameter, I can't find the right way to pass the arguments (trouble binding)
This is the latest version I tried, and is lifting no errors, but the script is not executed, nothing happens...
Help would be much appreciated.
runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
pipeline = runspace.CreatePipeline();
string script =
#"{param($merchantName, $appType, $gruntDirectory, $merchantInstanceDirectory, $editorConnectionString) "+
_config.MerchantInstance.Directory + #"\Generate_And_Compile_LESS.ps1"
+ " –merchantName $merchantName"
+ " –appType $appType"
+ " –gruntDirectory $gruntDirectory"
+ " -merchantInstanceDirectory $merchantInstanceDirectory"
+ " -editorConnectionString $editorConnectionString }";
Command compileCommand = new Command("Invoke-Command");
compileCommand.Parameters.Add("Scriptblock", ScriptBlock.Create(script));
var args = new List<string>();
args.Add(merchantName);
args.Add(appType.GetHashCode().ToString());
args.Add("'" + _config.Grunt.Directory + "'");
args.Add("'" + _config.MerchantInstance.Directory + "'");
args.Add("'" + _connectionStrings.AppConnectionString + "'");
compileCommand.Parameters.Add("ArgumentList", String.Join(",", args));
pipeline.Commands.Add(compileCommand);
Collection<PSObject> results = pipeline.Invoke();
You can use this code, which I personally just tested.
static void Main(string[] args)
{
PowerShell ps = PowerShell.Create();
ps.AddScript(#"c:\test\test.ps1").AddParameter("param1", "paramvalue1");
ps.Invoke();
}
Here is my test script, located in c:\test\test.ps1.
[CmdletBinding()]
param (
[string] $param1
)
Set-Content -Path $PSScriptRoot\test.txt -Value $param1;
FYI, make sure that you launch 32-bit (x86) PowerShell, and set the execution policy to Unrestricted. Visual Studio is a 32-bit process, and invokes the 32-bit PowerShell engine by default.

Categories

Resources