另一种为powershell

时间:2017-03-05 23:28:26

标签: c# powershell azure-active-directory

请你帮我系统地循环一下。我无法想到更好的东西,但我相信还有很多其他方法可以做到这一点。当我看起来像下面那样,我在前面有一个星星的两个循环中出错。我没有任何错误,它只显示UPN,并以某种方式不起作用:

if (upnoption1RadioButton.Checked == true && specifiedCheckBox.Checked == false)
{

    UPN = firstName.ToLower() + "." + lastName.ToLower() + "@" + domain;

    string takeOne = "New-MsolUser -UserPrincipalName "" + UPN + "" -DisplayName "" + firstName + " " + lastName + "" -FirstName "" + firstName + "" -LastName "" + lastName + "" -UsageLocation "" + "NZ"" + " -LicenseAssignment "" + accountSkuId + ":" + license + "" -ForceChangePassword $False";

    powershellOutput = executeCommand.GetPowerShellInfo(takeOne, "Password");
}

else if (upoption2RadioButton.Checked == true && specifiedCheckBox.Checked == false)
{
    UPN = firstName.ToLower() + lastName.ToLower() + "@" + domain;

    string takeOne = "New-MsolUser -UserPrincipalName "" + UPN + "" -DisplayName "" + firstName + " " + lastName + "" -FirstName "" + firstName + "" -LastName "" + lastName + "" -UsageLocation "" + "NZ"" + " -LicenseAssignment "" + accountSkuId + ":" + license + "" -ForceChangePassword $False";

    powershellOutput = executeCommand.GetPowerShellInfo(takeOne, "Password");
}

else if ((selectedListValue == "DONOTASSIGNLICENSE") && (upnoption1RadioButton.Checked == true) && (specifiedCheckBox.Checked == true))
{
    UPN = firstName.ToLower() + "." + lastName.ToLower() + "@" + domain;
    string takeTwo = "New-MsolUser -UserPrincipalName "" + UPN + "" -DisplayName "" + firstName + " " + lastName + "" -FirstName "" + firstName + "" -LastName "" + lastName + "" -UsageLocation "" + "NZ"" + " -Password "" + passwordValue + "" -ForceChangePassword $False";

    powershellOutput = executeCommand.GetPowerShellInfo(takeTwo, "Password");

}

else if ((selectedListValue == "DONOTASSIGNLICENSE") && (upoption2RadioButton.Checked == true) && (specifiedCheckBox.Checked == true))
{
    UPN = firstName.ToLower() + lastName.ToLower() + "@" + domain;
    string takeTwo = "New-MsolUser -UserPrincipalName "" + UPN + "" -DisplayName "" + firstName + " " + lastName + "" -FirstName "" + firstName + "" -LastName "" + lastName + "" -UsageLocation "" + "NZ"" + " -Password "" + passwordValue + "" -ForceChangePassword $False";

    powershellOutput = executeCommand.GetPowerShellInfo(takeTwo, "Password");

}

****               else if ((selectedListValue == "DONOTASSIGNLICENSE") && (upnoption1RadioButton.Checked == true) && (specifiedCheckBox.Checked == false))
{
    UPN = firstName.ToLower() + "." + lastName.ToLower() + "@" + domain;
    string takeOne = "New-MsolUser -UserPrincipalName "" + UPN + "" -DisplayName "" + firstName + " " + lastName + "" -FirstName "" + firstName + "" -LastName "" + lastName + "" -UsageLocation "" + "NZ" -ForceChangePassword $False";

    powershellOutput = executeCommand.GetPowerShellInfo(takeOne, "Password");

}

****            else if ((selectedListValue == "DONOTASSIGNLICENSE") && (upoption2RadioButton.Checked == true) && (specifiedCheckBox.Checked == false))
{
    UPN = firstName.ToLower() + lastName.ToLower() + "@" + domain;
    string takeOne = "New-MsolUser -UserPrincipalName "" + UPN + "" -DisplayName "" + firstName + " " + lastName + "" -FirstName "" + firstName + "" -LastName "" + lastName + "" -UsageLocation "" + "NZ" -ForceChangePassword $False";

    powershellOutput = executeCommand.GetPowerShellInfo(takeOne, "Password");

}

else if (upnoption1RadioButton.Checked == true && specifiedCheckBox.Checked == true)
{
    UPN = firstName.ToLower() + "." + lastName.ToLower() + "@" + domain;

    string takeOne = "New-MsolUser -UserPrincipalName "" + UPN + "" -DisplayName "" + firstName + " " + lastName + "" -FirstName "" + firstName + "" -LastName "" + lastName + "" -UsageLocation "" + "NZ"" + " -LicenseAssignment "" + accountSkuId + ":" + license + "" -ForceChangePassword $False" + " -Password "" + passwordValue + """;


    powershellOutput = executeCommand.GetPowerShellInfo(takeOne, "Password");

}

else if (upoption2RadioButton.Checked == true && specifiedCheckBox.Checked == true)
{
    UPN = firstName.ToLower() + lastName.ToLower() + "@" + domain;

    string takeOne = "New-MsolUser -UserPrincipalName "" + UPN + "" -DisplayName "" + firstName + " " + lastName + "" -FirstName "" + firstName + "" -LastName "" + lastName + "" -UsageLocation "" + "NZ"" + " -LicenseAssignment "" + accountSkuId + ":" + license + "" -ForceChangePassword $False" + " -Password "" + passwordValue + """;

    powershellOutput = executeCommand.GetPowerShellInfo(takeOne, "Password");

}

outputTextBox.Text += UPN + ": " + powershellOutput;
Thread.Sleep(3000);

}

这里的一些帮助将非常感激。

2 个答案:

答案 0 :(得分:0)

在试图理解你的问题之后,似乎你需要帮助处理你的if语句(?)

我建议您尝试使用Switch语句。

$a = 5
switch ($a) 
{ 
    1 {"The color is red."} 
    2 {"The color is blue."} 
    3 {"The color is green."} 
    4 {"The color is yellow."} 
    5 {"The color is orange."} 
    6 {"The color is purple."} 
    7 {"The color is pink."}
    8 {"The color is brown."} 
    default {"The color could not be determined."}
}

这样可以节省空间并使其更具可读性。

这也只是来自Microsoft

的示例代码

答案 1 :(得分:0)

这只是伪代码,但我相信你可以用这个函数替换整个混乱:

private string PowershellOutput(string firstName, string lastName, string domain, string selectedListValue, string accountSkuId, string license, string passwordValue)
{
    string upn = firstName.ToLower() + (upnoption1RadioButton.Checked ? "." : "") + lastName.ToLower() + "@" + domain;

    string powershellCommand = string.Format(@"New-MsolUser -UserPrincipalName ""{0}"" -DisplayName ""{1} {2}"" -FirstName ""{3}"" -LastName ""{4}"" -UsageLocation ""NZ"" -ForceChangePassword $False"
        + (selectedListValue != "DONOTASSIGNLICENSE" ? string.Format(@" -LicenseAssignment ""{0}:{1}""", accountSkuId, license) : " ")
        + (specifiedCheckBox.Checked ? string.Format(@" -Password ""{0}""", passwordValue) : "")
    , upn, firstName, lastName, firstName, lastName);

    return upn + ": " + executeCommand.GetPowerShellInfo(powershellCommand, "Password");
}

除了是否具有-Password或-LicenseAssignment外,所有实例的字符串都相同。您在selectedListValue!=“DONOTASSIGNLICENSE”时执行LicenseAssignment,并在指定CheckBox.Checked时执行密码设置。

此外,你的UPN的格式对于所有这些格式都是相同的,除了有一个点或没有点,这只是基于它是否是UPN1。

另一个注意事项是UPN有2个选项。所以它是UPN1或者不是。所以你不需要检查UPN2。如果它不是UPN1,那么我们就知道它是UPN2。不要过度优化未来。

我是如何做到的

我所做的是整理你的条件。有三个条件。 DONOTASSIGNLICENSE,UPN1 / UPN2和“指定”。我几乎只是重写了你的条件,所以所有条款都有相同顺序的所有条件,然后在我的脑海中按“排序条件”。当它出现在另一边时,我可以看到文本的模式,并且你复制了大量的工作。