首页 > 解决方案 > Powershell get-content 将列拆分为双列

问题描述

我需要在SFB中注册几个用户,我需要为他们各自的用户注册用户和电话分机,在TXT中我有两列用户和分机用“;”分隔,我的疑问是,如何分隔列在 foreach 命令中?用户名和电话分机是否会正确添加到代码中?


样本数据.txt

用户 | 扩大

myuser1@mycompany.com ; 3331 <br/>
myuser2@mycompany.com ; 3332 <br/>
myuser2@mycompany.com ; 3334 <br/>

-------------------------------------------------- -------------

$user = Get-Content C:\temp\skypeproject\sampleData.txt

foreach ($users in $user) {  Set-CsUser –Identity $users –EnterpriseVoiceEnabled $true
<br/> <br/>
  –OnPremLineURI tel:+3333333$extension  <br/>  <br/>
  Set-CsUserPstnSettings –Identity "$users" –HybridPSTNSite ACCESSPOOL <br/> <br/>
  Grant-CsTenantDialPlan –Identity "$users" –PolicyName Company <br/> <br/>

}

标签: powershellforeachskype

解决方案


正如vonPryz建议的那样Import-CSV,用于从文件中获取数据。您可以在文件中包含列的标题,以与其余数据相同的方式分隔,或者稍后使用 cmdlet 添加标题:

没有标题:

$path = "path\to\sampleData.txt"
$csv = Import-Csv -Path $path -Delimiter ";" -Header "user","extension"
foreach ($row in $csv) {  
    $users = $row.user 
    $extension = $row.extension
    Set-CsUser –Identity $users –EnterpriseVoiceEnabled $true –OnPremLineURI tel:+3333333$extension
    Set-CsUserPstnSettings –Identity $users –HybridPSTNSite ACCESSPOOL
    Grant-CsTenantDialPlan –Identity $users –PolicyName Company
}

文件头:

$path = "path\to\sampleData.txt"
$user = Import-Csv -Path $path -Delimiter ";"
foreach ($row in $csv) {  
    $users = $row.user 
    $extension = $row.extension
    Set-CsUser –Identity $users –EnterpriseVoiceEnabled $true –OnPremLineURI tel:+3333333$extension
    Set-CsUserPstnSettings –Identity $users –HybridPSTNSite ACCESSPOOL
    Grant-CsTenantDialPlan –Identity $users –PolicyName Company
}

文件:

user;extension
myuser1@mycompany.com;3331
myuser2@mycompany.com;3332
myuser2@mycompany.com;3334

请注意,文件中包含空格!从您的文件中删除它们,或者您需要trim输出:

$users = $row.user.trim()
$extension = $row.extension.trim()

推荐阅读