不使用 Out-GridView 尝试

huangapple go评论45阅读模式
英文:

Trying NOT to use Out-GridView

问题

我使用 GetADUser 调用,它只返回指定组织单位中所有用户的 Sam 帐户名称。

我尝试在一个系统 Windows Forms 消息框中显示它们,但它不起作用。

我知道我接近成功,但我有点脑袋一片空白。有没有更好的方法来做到这一点?再次强调,我非常希望避免使用 Out-GridView。我希望有类似这样的东西:

UserTest1
UserTest2
UserTest3

我尝试了一些谷歌搜索,但它们并不真正满足我的需求。任何帮助将不胜感激。谢谢!

英文:

I use a call to GetADUser, which is just returning the Sam Account Name of all the users in an OU.

I'm trying to display them in a System Windows Forms Message Box but its not working.

I know I'm close, but I'm having a brain fart. Is there a better way of doing this? Again, I'm trying very hard to avoid using Out-GridView. I'd love to have something like this:

UserTest1
UserTest2
UserTest3

I tried a couple of google searches but they were not really what I'm trying to do. Any help would be appreciated
Thank you

答案1

得分: 0

以下是您要的翻译部分:

<!-- language-all: sh -->

如果您确实希望在此处使用 WinForms [消息框](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.messagebox):

Add-Type -AssemblyName System.Windows.Forms

创建 20 个示例用户名。

在实际代码中,请使用:

$users = (Get-ADUser -filter * -Searchbase $OU).SamAccountName

$users = 1..20 | ForEach-Object { "user" + $_ }

显示每个用户名的消息框,每个用户名占据一行。

$users -join "`n",
'Title', # 窗口标题
'OKCancel', # 按钮
'Information' # 图标
)


请注意,这仅适用于用户名的数量_有限_,即只有屏幕上_一次_可以容纳的数量,不包括显示标题栏和按钮所需的空间。
如果用户名太多,将不会全部可见,按钮也不会显示。

要避免这个问题,您需要创建一个带有(可滚动的)列表框的自定义表单。
英文:

<!-- language-all: sh -->

If you really want to use a WinForms message box for this:

Add-Type -AssemblyName System.Windows.Forms

# Create 20 sample user names.
# In your real code, use:
#   $users = (Get-ADUser -filter * -Searchbase $OU).SamAccountName
$users = 1..20 | ForEach-Object { &quot;user&quot; + $_ }

# Display a message box with each username on its own line.
[Windows.Forms.MessageBox]::Show(
  $users -join &quot;`n&quot;,
  &#39;Title&#39;,       # window title
  &#39;OKCancel&#39;,    # buttons
  &#39;Information&#39;  # icon
)

Note that this is only suitable for a limited number of usernames, namely as many as will fit on your screen at once, excluding the space needed to display the title bar and the button(s).
If there are too many, not all will be visible, and the button(s) won't show.

To avoid this problem, you'd need to create a custom form with a (scrollable) list box.

答案2

得分: 0

这是我最终完成的代码:

<-- 语言PowerShell --

Get-ADUser -Filter * -Searchbase $Var1 | Where-Object {$__.name -notlike
    "$var2*"} | ForEach-Object{[void]$Var3.Items.Add($_.SamAccountName)}

希望这对其他人有所帮助。

英文:

This is what I ended up doing:

<!-- language: powershell -->

Get-ADUser -Filter * -Searchbase $Var1 | Where-Object {$__.name -notlike
    &quot;$var2*&quot;} | ForEach-Object{[void]$Var3.Items.Add($_.SamAccountName)}

Hope this will help someone else

huangapple
  • 本文由 发表于 2023年4月7日 03:50:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定