Powershell 读取 csv 并获取等于用户名的数据

Powershell read csv and get data that equals username

我正在开发一个生成 Outlook html 签名的 powershell 脚本。 现在可以使用了,但您需要手动填写所有模板单词(姓名电子邮件电话等)

我想要的是,当您 运行 powershell 脚本时,它会检查登录用户并从 CSV 文件中获取该用户的信息并将其填充到 html 模板中。

执行此操作的最佳方法是什么?

我现在得到的是:

$html_template = @"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
  etc etc etc
"@

# Simulate how ever you are querying for data.
$desktop_path = "$env:userprofile\Desktop\"
$filename = [Environment]::UserName

# Does nothing only import
$content = Import-CSV -Path D:\export.csv

# Input fields
$name= Read-Host -Prompt 'Input name (eg. Tim Tom)'
$title= Read-Host -Prompt 'Input the job title (eg. Office Manager)'
$phone= Read-Host -Prompt 'Input the last 3 telephone numbers (eg. 111)'
$email= Read-Host -Prompt 'Input email adress (eg. name@domain.nl)'

# Check if map Handtekeningen exist else force create it.
New-Item -ItemType Directory -Force -Path C:\Users$filename\AppData\Roaming\Microsoft\Handtekeningen | Out-Null

# Create html file
$html_template -f $name,$title,$phone,$email,$filename | Out-File -Force $desktop_path$filename.html

# Set file to Readonly (problem is it cannot be overwriten)
Set-ItemProperty $desktop_path$filename.html -name IsReadOnly -value $true

# Write
Write-Host "Saving signature."
Write-Host "Closing in 5 seconds."

# Sleep for 5 seconds
Start-Sleep -s 5

csv:

username;Full Name;Job Title;Telephone;Email

如果(在您发表评论后)我理解正确,您只需要从 CSV 文件中找出要使用的行即可。在这种情况下,您可以从环境变量 USERNAME 中读取用户名并进行适当的过滤:

$content = Import-CSV -Path D:\export.csv

$user = $content | where username -eq $Env:USERNAME

if (!$user -or $user.Count -gt 1) {
  throw 'No user, or multiple users found'
}

# Input fields
$name= $user.'Full Name'
$title= $user.'Job Title'
$phone= $user.Telephone
$email= $user.'Email'