Way of importing data

Hello to all,
Hope you can help me :slight_smile:

I have created a new PowerShell script with a GUI that work fine.
The purpose of the script is to give Help-Desk personnel the ability to add User names to a specific collection in Configuration manager.

According to the script, the help desk man need to enter user names inside the text box with comma separated value only (,) like this:
Domain\User1,Domain\User2

Is there a way to change that and only enter user names in a list like this:
Domain\user1
Domain\user2
If i try this way, it fails.

from script:
$users = $textBox.Text -split “,”

Thank you so much for your time and help.

New-PSDrive -Name Logfolder -PSProvider "FileSystem" -Root "\\network\RestartComputers"
$logfilepath = "logfolder:RestartComputers.txt"
$datetime = get-date -Format "yyyy-MM-dd HH:mm:ss"
$Logmessage = "[$datetime] Starting process"
$Logmessage | out-file -filepath $logfilepath -append

# Connect to the Configuration Manager site
$scriptPath = $PSScriptRoot
$SiteCode = "aaa"
$SiteServer = "servername.com"
Import-Module -name "$scriptPath\Module\ConfigurationManager.psd1"
if((Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue) -eq $null) {
New-PSDrive -name $SiteCode -psprovider CMSite  -Root $SiteServer
}
Set-Location "$($SiteCode):\"
Add-Type -AssemblyName System.Windows.Forms

# Create the form
$form = New-Object System.Windows.Forms.Form
$form.Text = "Restart machines tool"
$form.Size = New-Object System.Drawing.Size(300, 220)
$form.StartPosition = "CenterScreen"

# Create a label
$label = New-Object System.Windows.Forms.Label
$label.Text = "Enter usernames (comma-separated):"
$label.Location = New-Object System.Drawing.Point(10, 10)
$label.AutoSize = $true

# Create a text box
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Multiline = $true
$textBox.ScrollBars = "Vertical"
$textBox.Size = New-Object System.Drawing.Size(250, 100)
$textBox.Location = New-Object System.Drawing.Point(10, 40)

# Create a button
$button = New-Object System.Windows.Forms.Button
$button.Text = "Add Users"
$button.Location = New-Object System.Drawing.Point(10, 150)
$button1 = New-Object System.Windows.Forms.Button
$button1.Text = "Close"
$button1.Location = New-Object System.Drawing.Point(190, 150)

$button2 = New-Object System.Windows.Forms.Button
$button2.Text = "OpenLog"
$button2.Location = New-Object System.Drawing.Point(100, 150)

# Add controls to the form
$form.Controls.Add($label)
$form.Controls.Add($textBox)
$form.Controls.Add($button)
$form.Controls.Add($button1)
$form.Controls.Add($button2)
$form.CancelButton = $button1

# Define button click event handler
$button.Add_Click({
$users = $textBox.Text -split ","
$collectionID = "aaa3535235" # Update with the actual Collection ID

# Add each user to the collection
foreach ($user in $users) {
$userObject = Get-CMUser -Name $user
 
Add-CMUserCollectionDirectMembershipRule -CollectionID $collectionID -ResourceID $userObject.ResourceID -PassThru

    $commandResult = $?
    $Logmessage = "[$datetime] $user added to collection with result: $commandResult"
    $Logmessage | Out-File -FilePath $logfilepath -Append
      $textbox.Text = ""
}
})

 # Define button2 click event handler
$button2.Add_Click({
cd logfolder:
.\RestartComputers.txt
})

# Show the form
$form.ShowDialog()

Allowing users to enter free text is always a bad idea in my opinion. Especially when it is about to pick one or more items from already existing lists. The better option would be to offer a (already pre-limitted) choice the user can choose from. Depending on how many users you have to provide from how many possible choices a GridView could be an elegant way to achieve this.

With the “-OutputMode” set to “Multiple” the help desk users could pick one or more users from the list holding the <Ctrl> key just like you do in Excel for example when you select more than one cell.

1 Like

Thank you for answering.
This Help Desk team gets the list of users in that way, without commas between the names and they just copy/paste it into my PowerShell gui.
Usually, they get around 30 defferent users each time.
That is why I am looking for another way it to work.

Thanks again
Amir

Where and how do they get it? Wouldn’t be better to read it from the source they get it from?

Didn’t you say they enter it with comma?

Not exactly.
I said that the script is only working when users are entered with comma.
I just want to know what PowerShell code i need to change in order to it to work when the users names are pasted like this:
domain\user1
domain\user2

Amir

You would need to split either on a NewLine/LineBreak character or a Cariage Return character or both depending where the copied data come from.

This might help:

If these are based on departments or some other AD attributes you know you can just build dynamic rules directly in SCCM

1 Like