AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1871608
Accepted
Manu Järvinen
Manu Järvinen
Asked: 2025-01-23 01:17:34 +0800 CST2025-01-23 01:17:34 +0800 CST 2025-01-23 01:17:34 +0800 CST

如何通过 PS1 PowerShell 脚本将带有变音符号(Ä、Ö、Å 等)的多个 Windows 11 用户添加到带有变音符号的组中?

  • 772

如何通过 PS1 PowerShell 脚本将带有变音符号(Ä、Ö、Å 等)的多个 Windows 11 用户添加到带有变音符号的组中?

powershell
  • 2 2 个回答
  • 965 Views

2 个回答

  • Voted
  1. Best Answer
    Cpt.Whale
    2025-01-23T05:52:26+08:002025-01-23T05:52:26+08:00

    默认安装的 powershell v5.1 和命令在技术上不存在umlouts 问题,因此请检查您的输入是否正确编码:

    Add-LocalGroupMember -Name 'UMLÄUTS' -Member 'Me'
    

    Powershell (5.1) 的默认安装版本默认假定脚本和文本文件采用带BOM 的 UTF-8编码,因此如果文件采用纯 UTF-8(无 BOM),您可能会遇到问题。例如:

    # wrong characters when file is UTF8
    Get-Content 'test_utf8.txt'
    umlauts: Ä Ö Å
    
    # success only when specifying encoding
    Get-Content 'test_utf8.txt' -Encoding UTF8
    umlauts: Ä Ö Å
    

    对于脚本文件本身,您可以像这样重新编码文件,它应该可以正常运行:

    Get-Content -Path '.\script_utf8.ps1' -Raw -Encoding UTF8 | 
      Set-Content -Path '.\script_utf8_bom.ps1'
    

    在这种情况下,Powershell v6 及更新版本默认使用纯 UTF-8,但请注意,运行带有 BOM 的文件时可能会遇到反向问题。

    在此处查看更多详细信息:about_Character_Encoding

    • 6
  2. Manu Järvinen
    2025-01-23T01:17:34+08:002025-01-23T01:17:34+08:00

    花了一段时间来真正测试这一点。

    包括芬兰语Windows 11的图像和脚本,因为我们在这里使用变音符号或“Ääkköset”。(尽管我讨厌非英语软件或与计算机相关的惯例,但这不是重点)

    1. 最重要的是:从 Microsoft Store下载最新的 PowerShell。不要使用 Windows 11 附带的 Windows PowerShell,它已经过时了,并且不适用于变音符号(我试过了,不行,变音符号会变得混乱,就像 Käyttäjät 变成 Käyttäjät,很烦人)。

      下载普通的 PowerShell,而不是使用(不适用于变音符号)Windows PowerShell

    2. 这里有一些入门材料。PS1 脚本中重要的是“Käyttäjät”一词。在芬兰语中,它表示用户或普通用户。我比较确定,如果您将其替换为 Windows 11 本地化版本中使用的术语,那么您应该就可以开始了。请不要通过缩短或删除脚本来编辑脚本 - 如果我再次需要它们,我会回来查看它们。尽管这条消息已经很长了。

      通过脚本创建 Windows 11 用户


    公司人员数据带变音符号.csv

    (它们是自动生成的最受欢迎的芬兰名字,与任何事物都无关。)

    GroupName;UserName;Password
    Tuotanto;Anni Koskinen;Asennus1
    Tuotanto;Kalle Nurmi;Asennus1
    Myynti;Matti Saarinen;Asennus1
    Tuotanto;Helena Koskinen;Asennus1
    Hallinto;Seppo Saarinen;Asennus1
    Hallinto;Tiina Mäkinen;Asennus1
    Myynti;Pekka Niemi;Asennus1
    Varasto;Kaisa Mäkinen;Asennus1
    Ulkoiset;Anni Ahonen;Asennus1
    Harjoittelijat;Tuomas Ahonen;Asennus1
    Harjoittelijat;Pekka Heinonen;Asennus1
    Myynti;Juhani Niemi;Asennus1
    Hallinto;Pekka Lehtonen;Asennus1
    Hallinto;Harri Saarinen;Asennus1
    Myynti;Tuomas Virtanen;Asennus1
    Myynti;Pekka Savolainen;Asennus1
    Tuotanto;Timo Hämäläinen;Asennus1
    

    添加所有用户和组.ps1

    # Define the path to the CSV file
    $csvFile = "C:\Users\Asennus\Desktop\Company_Personnel_Data_With_Umlauts.csv"  # Replace with the actual path to your CSV file
    
    # Import user data from the CSV file (specify semicolon as the delimiter)
    $users = Import-Csv -Path $csvFile -Delimiter ';'
    
    # Keep track of duplicates, created groups, and processed usernames
    $duplicateUsers = @()
    $createdGroups = @()
    $processedUsers = @()
    
    # Ensure the "Käyttäjät" group exists
    if (-not (Get-LocalGroup -Name "Käyttäjät" -ErrorAction SilentlyContinue)) {
        Write-Host "Creating group: Käyttäjät"
        New-LocalGroup -Name "Käyttäjät" -Description "Default user group"
        $createdGroups += "Käyttäjät"
    }
    
    # Check and create groups from the CSV if necessary
    foreach ($group in ($users.GroupName | Sort-Object -Unique)) {
        if (-not (Get-LocalGroup -Name $group -ErrorAction SilentlyContinue)) {
            Write-Host "Creating group: $group"
            New-LocalGroup -Name $group -Description "Group created via script"
            $createdGroups += $group
        }
    }
    
    # Loop through each user in the CSV file
    foreach ($user in $users) {
        # Check if the username has already been processed
        if ($processedUsers -contains $user.UserName) {
            $duplicateUsers += $user.UserName
            continue
        }
    
        # Mark the username as processed
        $processedUsers += $user.UserName
    
        # Check if the user already exists
        $existingUser = Get-LocalUser -Name $user.UserName -ErrorAction SilentlyContinue
        if ($existingUser) {
            # If the user exists, log them as duplicate and skip creation
            $duplicateUsers += $user.UserName
        } else {
            # Create the user
            New-LocalUser -Name $user.UserName -Password (ConvertTo-SecureString $user.Password -AsPlainText -Force) -FullName $user.UserName -Description "User created via script"
        }
    
        # Add user to the "Käyttäjät" group if not already a member
        if (-not (Get-LocalGroupMember -Group "Käyttäjät" -Member $user.UserName -ErrorAction SilentlyContinue)) {
            Add-LocalGroupMember -Group "Käyttäjät" -Member $user.UserName -ErrorAction SilentlyContinue
        }
    
        # Remove the user from all other non-"Käyttäjät" groups
        $existingGroups = Get-LocalGroup | Where-Object {
            (Get-LocalGroupMember -Group $_.Name -ErrorAction SilentlyContinue | Where-Object { $_.Name -eq $user.UserName }) -and
            ($_.Name -ne "Käyttäjät")
        }
    
        foreach ($existingGroup in $existingGroups) {
            Remove-LocalGroupMember -Group $existingGroup.Name -Member $user.UserName -ErrorAction SilentlyContinue
        }
    
        # Add user to the specified group from the CSV
        Add-LocalGroupMember -Group $user.GroupName -Member $user.UserName -ErrorAction SilentlyContinue
    }
    
    # Print summary
    Write-Host "`n--- Script Summary ---"
    if ($createdGroups.Count -gt 0) {
        Write-Host "The following groups were created:" -ForegroundColor Green
        $createdGroups | ForEach-Object { Write-Host "- $_" }
    } else {
        Write-Host "No new groups were created." -ForegroundColor Yellow
    }
    
    if ($duplicateUsers.Count -gt 0) {
        Write-Host "`nThe following duplicate usernames in the CSV were skipped (manual handling required):" -ForegroundColor Yellow
        $duplicateUsers | ForEach-Object { Write-Host "- $_" }
    } else {
        Write-Host "`nNo duplicate usernames were found in the CSV." -ForegroundColor Green
    }
    
    Write-Host "`nAll assignments completed successfully!" -ForegroundColor Green
    

    删除所有通过脚本创建的用户和组.ps1

    # Remove Users with Description "User created via script"
    Write-Host "Searching for users with description 'User created via script'..." -ForegroundColor Yellow
    $localUsers = Get-LocalUser
    $usersToRemove = $localUsers | Where-Object { $_.Description -eq "User created via script" }
    
    if ($usersToRemove.Count -gt 0) {
        Write-Host "The following users will be removed:" -ForegroundColor Yellow
        $usersToRemove | ForEach-Object { Write-Host "- $($_.Name)" }
    
        # Confirm deletion
        $userConfirmation = Read-Host "Type 'YES' to confirm deletion of these users"
        if ($userConfirmation -eq "YES") {
            $usersToRemove | ForEach-Object {
                Write-Host "Removing user: $($_.Name)" -ForegroundColor Red
                Remove-LocalUser -Name $_.Name -ErrorAction SilentlyContinue
            }
            Write-Host "All users with the description 'User created via script' have been removed." -ForegroundColor Green
        } else {
            Write-Host "Operation cancelled. No users were removed." -ForegroundColor Cyan
        }
    } else {
        Write-Host "No users found with the description 'User created via script'." -ForegroundColor Green
    }
    
    # Remove Groups with Description "Group created via script"
    Write-Host "`nSearching for groups with description 'Group created via script'..." -ForegroundColor Yellow
    $localGroups = Get-LocalGroup
    $groupsToRemove = $localGroups | Where-Object { $_.Description -eq "Group created via script" }
    
    if ($groupsToRemove.Count -gt 0) {
        Write-Host "The following groups will be removed:" -ForegroundColor Yellow
        $groupsToRemove | ForEach-Object { Write-Host "- $($_.Name)" }
    
        # Confirm deletion
        $groupConfirmation = Read-Host "Type 'YES' to confirm deletion of these groups"
        if ($groupConfirmation -eq "YES") {
            $groupsToRemove | ForEach-Object {
                Write-Host "Removing group: $($_.Name)" -ForegroundColor Red
                Remove-LocalGroup -Name $_.Name -ErrorAction SilentlyContinue
            }
            Write-Host "All groups with the description 'Group created via script' have been removed." -ForegroundColor Green
        } else {
            Write-Host "Operation cancelled. No groups were removed." -ForegroundColor Cyan
        }
    } else {
        Write-Host "No groups found with the description 'Group created via script'." -ForegroundColor Green
    }
    
    • 5

相关问题

  • 如何将变量字符串放入powershell中的数组?

  • Powershell 和正则表达式:Notepad++“保存时备份”文件列表。编辑名称,按上次写入时间排序

  • 将前景颜色添加到 Powershell 配置文件?

  • 禁用后无法启用 Microsoft Print to PDF

  • 我可以让这个 PowerShell 脚本接受逗号吗?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Vickel Firefox 不再允许粘贴到 WhatsApp 网页中? 2023-08-18 05:04:35 +0800 CST
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve