powershell - 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/>
}
解决方案
正如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()
推荐阅读
- html - 我需要帮助来创建徽章,如图像中所示,如使用 css 的云或气泡类型的徽章
- python - 加载使用 joblib/pickle 保存的 ML 模型时出现问题
- webpack - googleapi需要什么加载器?
- twilio - 没有名为“twilio”的模块
- python - 如何在 owlready2 中使用 differentFrom 和 sameAs?
- sql - 带有表达式的nodejs mariadb查询
- html - 两列,图像左内容右 - 调整图像大小而不是内容
- python - 对api服务器python的HTTPS请求
- mysql - VB。网络mysql odbc
- xcode - 当您开始使用 Xcode 时,如何从 Xcode 解决“架构 x86_64 的重复符号”?