在PowerShell上使用Splatting的正确语法是什么?

时间:2018-03-27 06:01:38

标签: powershell powershell-v2.0 powershell-v3.0

好的伙计们,我已经编辑了我的用户创建脚本,现在它几乎是完美的,除了一件我无法弄清楚的事情

如何让哈希表读取" $ GivenName $姓氏的变量" 并创建用户的名称,因为现在它创建的用户没有Name参数

或者我需要在csv文件中为$ GivenName和$ surname创建一个标题

    cls

#get the csv file
$filepath = import-csv "C:\users.csv" 

#set the variable for the uers
$newusers = $filepath


#set Passwords for new users 
$securepassword = ConvertTo-SecureString "BlahBlah" -AsPlainText -Force


foreach ($user in $newusers) {

#get user information

    $User_Creation_Settings = @{

    Name                  = "$GivenName $Surnam"
    GivenName             = $user.GivenName
    Surname               = $user.Surnam
    UserPrincipalName     = $user.UserPrincipalName
    SamAccountName        = $user.SamAccountName
    Path                  = $user.Path
    ScriptPath            = $user.ScriptPath
    ChangePasswordAtLogon = $false
    Department            = $user.Department
    DisplayName           = $user.DisplayName
    Description           = $user.Description
    Title                 = $user.'Job Title'
    AccountPassword       = $securepassword
    Enabled               = $true

}

New-ADUser @User_Creation_Settings



#Get Groups information
$group1 = $user.Group1
$group2 = $user.Group2
$group3 = $user.Group3
$group4 = $user.Group4


#Add the users in to Groups

 Add-ADPrincipalGroupMembership -Identity $user.SamAccountName -MemberOf $user.group1
 Add-ADPrincipalGroupMembership -Identity $user.SamAccountName -MemberOf $user.group2
 Add-ADPrincipalGroupMembership -Identity $user.SamAccountName -MemberOf $user.group3
Add-ADPrincipalGroupMembership -Identity $user.SamAccountName -MemberOf $user.group4



Write-Host "`n"
Write-Host "The account for $GivenName $Surnam created in $OuPath successfully "


}
pause

所以我已经弄清楚了,并且我发布了解决方案

为了解决我所描述的问题,我不得不让Powershell认为"名字"和"姓氏"是一个整体变量,在中间分开,这就是为什么我使用括号与" $"在他们面前签名,Power Shell会认为你正在使用循环中的$ user,并且会弹出" Popup" CSV文件中的标题。

这是整个脚本

    cls

#get the csv file
$filepath = import-csv "C:\users.csv" 

#set the variable for the uers
$newusers = $filepath


#set Passwords for new users 
$securepassword = "BlahBlah"


foreach ($user in $newusers) {

#get user information

    $User_Creation_Settings = @{

    Name                  = "$($user.'First Name') $($user.Lastname)"
    GivenName             = $user.'First Name'
    Surname               = $user.Lastname
    UserPrincipalName     = $user.UserPrincipalName
    SamAccountName        = $user.SamAccountName
    Path                  = $user.Path
    ScriptPath            = $user.ScriptPath
    ChangePasswordAtLogon = $false
    Department            = $user.Department
    DisplayName           = $user.DisplayName
    Description           = $user.Description
    Title                 = $user.'Job Title'
    AccountPassword       = ConvertTo-SecureString $securepassword -AsPlainText -Force
    Enabled               = $true

}

New-ADUser @User_Creation_Settings



#Get Groups information
$group1 = $user.Group1
$group2 = $user.Group2
$group3 = $user.Group3
$group4 = $user.Group4


#Add the users in to Groups

 Add-ADPrincipalGroupMembership -Identity $user.SamAccountName -MemberOf $user.group1
 Add-ADPrincipalGroupMembership -Identity $user.SamAccountName -MemberOf $user.group2
 add-ADPrincipalGroupMembership -Identity $user.SamAccountName -MemberOf $user.group3
 Add-ADPrincipalGroupMembership -Identity $user.SamAccountName -MemberOf $user.group4



Write-Host "`n"
Write-Host "The account for $($user.'First Name') $($user.Lastname) created in $($user.Path) successfully "


}
pause

所以在这种情况下:"$($user.'First Name') $($user.Lastname)"

powershell的行为就像你输入了一个完整的变量一样。

2 个答案:

答案 0 :(得分:0)

简短的回答是两者都很好,因为两者都是在PowerShell中创建哈希表的有效方法。有关详细信息,请查看about_Splatting

就个人而言,我使用单引号围绕字符串并保留变量,以便PowerShell不认为我正在尝试调用函数。如上所述,这里不需要这样做,坦率地说,这可能会使您的代码看起来比它需要的更加混乱。

需要注意的一些事项:

Name      = $firstname $lastname    # invalid because of the space
'class'   = win32_logicaldisk       # invalid: PowerShell will look for the function win32_logicaldisk
GivenName = '$firstname'            # because single quotes are used, PowerShell will not replace the variable; GivenName will literally be $firstname

答案 1 :(得分:0)

上面的splatting都很好,颜色变化的原因在第一个Hashtable "$firstname $lastname"是一个字符串,正如你在引号中提到的那样,Editor表示棕色的字符串和黑色的变量。 / p>

在第二个Hashtable中,您对所有键和值使用了单引号,并再次将这些引号理解并由编辑器表示为字符串。

无论如何都没有害处。