如果用户名中间有空格,如何使用powershell将用户添加到本地管理员组? [英] How to add user to local administrator group using powershell when the user name has space in the middle?

查看:222
本文介绍了如果用户名中间有空格,如何使用powershell将用户添加到本地管理员组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





当用户名中间有空格时,我无法将用户添加到本地管理员组。我正在从CSV文件中读取用户名值。是否需要CSV文件或Powershell脚本中的更改?



感谢Adnvance。



我尝试过:



Hi,

I am not able to add users to local administrator group when the username has space in the middle. I am reading the user name values from CSV file. Is the change need in CSV file or in Powershell script?

Thanks in Adnvance.

What I have tried:

foreach($Computer in $servers) {
    #Write-Host "We can now use the server name [$Computer] in our code"
    $Status=$null
    $Comment = $null
    $ComputerName= $servers.ComputerNM[$count]
    $Usrgroup = $servers.UserGroup  #"Here we get username"
    $ObjName = $servers.UserGroup[$count]
    $count++
    #Write-Host "ComputerName :" $ComputerName
    #Write-Host "Username :" $ObjName
    #Write-Host ("{0}. Working on {1}" -f $count, $ComputerName)
    if(Test-Connection -ComputerName $ComputerName -Count 1 -Quiet) {
        Write-Verbose "$ComputerName : Online"
        try {
            $GroupObj = [ADSI]"WinNT://$ComputerName/Administrators"
            $GroupObj.Add("WinNT://$ObjDomain/$ObjName")
            $Status = "Success"
            $Comment = "Added $ObjName $ObjectType to Local administrators group on [$ComputerName]"
            Write-Verbose "Successfully added $ObjName $ObjectType to $ComputerName"
        } catch {
            $Status = "Failed"
            $Comment = $ObjectType  + "[$ObjName] - " + $_.toString().replace("`n","").replace("`r","")
            Write-Verbose "Failed to add $ObjName $ObjectType to $ComputerName"
        }
        Add-Content -Path $ResultsFile -Value ("{0},{1},{2}" -f $ComputerName,$Status,$Comment )   

    } else {
        Write-Warning "$ComputerName : Offline"
        Add-Content -Path $ResultsFile -Value ("{0},{1}" -f $ComputerName,"Offline")
    }







---这是我的csv格式---



ComputerNM ||用户组

========================

dev ||哈利

dev || James Horner --->这个没有添加




---This is my csv format---

ComputerNM || UserGroup
========================
dev || Harry
dev || James Horner --->this one not adding

推荐答案

计算机在


服务器){
#Write -Host我们现在可以在我们的代码中使用服务器名称
servers) { #Write-Host "We can now use the server name


计算机


这篇关于如果用户名中间有空格,如何使用powershell将用户添加到本地管理员组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆