Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, July 14, 2020 6:35 AM
Hello All,
This is the first time I'm trying to convert powershell script to c# code. Can someone help me out please
foreach ( $csv in $csv)
{
Set-CsOnlineDialInConferencingUser -Identity $csv.UserPrincipalName -ServiceNumber $csv.ServiceNumber
}
All replies (3)
Tuesday, July 14, 2020 3:25 PM | 1 vote
The `foreach` is just a foreach statement in C#. Of course we have no way of knowing what `$csv` is. The core call is setting AD properties it appears so you can use the directory services API in .NET to do that. It just sets an AD property I wager but I don't know the implementation.
var csvItems = //Some code that gets IEnumerable<T>
foreach (var csv in csvItems)
{
//Get DirectoryEntry for the given user
//Set their property //Commit changes using CommitChanges
}
For the directory services stuff I'd start with DirectoryEntry to get the user but there may be a better way, perhaps using PrincipalContext.
Note that your PS code does not look valid as you're using $csv as both the loop variant and the item to be iterated.
Michael Taylor http://www.michaeltaylorp3.net
Wednesday, July 15, 2020 3:13 AM | 3 votes
Hi Karthick E,
First, CoolDadTx has provided a detailed explanation to convert powershell script to c# code.
And I have another suggestion and hope it could be helpful.
You can execute PowerShell scripts from C# directly instead of converting powershell to c#.
using (PowerShell PowerShellInstance = PowerShell.Create())
{
....
}
More details you can refer to this document.
Best Regards,
Daniel Zhang
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].
Thursday, July 30, 2020 8:28 AM
Hi Karthick E,
Has your problem been solved? If it is resolved, we suggest that you mark it as the answer. So it can help other people who have the same problem find a solution quickly.
Best Regards,
Daniel Zhang
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].